Solved

Load Combobox from string with option

Posted on 2010-08-16
9
309 Views
Last Modified: 2012-05-10
Hi
I need the code to do the following for windows application.
I want to have strings like A,B,C,D,E ETC.,

Now in each form, I should able to load the combobox from the string defined above but I should be able to set what I want to load like A or B or A&B and so on depending the purpose of form
0
Comment
Question by:GRChandrashekar
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:Sarika30
ID: 33444397
Please be more specific about your question. What do you want exactly.
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33444404
I'm still not clear on the logic part of it. So in say, form1, the combo box will be loaded as 'A or B or C or D or E' (as only one item in the combobox), and in say form2, it should be loaded as 'A&B&C&D&E' (again as only one item in the combobox).

If I'm wrong, please provide more details.

Arun
0
 

Author Comment

by:GRChandrashekar
ID: 33444444
Here we go
Let us assume we strings address, name and telephone number.

In form 1 combobox I want to load only address
in form2 combobox i want to load only name
in form3 combobo i want to load address and telephone number
in form 4 combobx i want to load address, name and telephone number
0
 
LVL 4

Expert Comment

by:Sarika30
ID: 33444464
Do you want to load address, name and tel no in one single combo box?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:GRChandrashekar
ID: 33444483
YES
0
 
LVL 4

Expert Comment

by:Sarika30
ID: 33444696
You will need to create a class with all the parameters that you want. For example, if you have 3 combo box
Combo1  - Name
Combo2 - Address
Combo3 - Name&Address
Then the class will be:

public class Sample
{
    public Sample()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    private String _name = string.Empty;
    private String _address = string.Empty;
    private String _nameAddress = string.Empty;

    public String Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
    public String Address
    {
        get
        {
            return _address;
        }
        set
        {
            _address = value;
        }
    }
    public String NameAddress
    {
        get
        {
            return _nameAddress;
        }
        set
        {
            _nameAddress = value;
        }
    }

}

After that, suppose you fetch the data in a dataset. This dataset contains Name and Address. Create a function that creates a list of the above class objects with the data in the dataset.:

private List<Sample> LoadData(DataSet ds)
    {
        List<Sample> lstSample = new List<Sample>();
        Sample objSample = null;
        foreach (DataRow row in ds.Tables[0].Rows)
        {
            objSample = new Sample();
            objSample.Name = row[0].ToString();
            objSample.Address = row[1].ToString();
            objSample.NameAddress = objSample.Name + objSample.Address;
            lstSample.Add(objSample);
        }
        return lstSample;
    }
0
 
LVL 4

Accepted Solution

by:
Sarika30 earned 500 total points
ID: 33444724
Now set the datasource of your combo boxes according to your need. Example:

string ConnString = "connectionstring";
        SqlConnection conn = new SqlConnection(ConnString);
        String strSql = "select Name, Address from MyTable";
        DataSet ds = new DataSet();
        SqlDataAdapter adapter = new SqlDataAdapter(strSql, conn);
        adapter.Fill(ds, "Vals");
        List<Sample> lstSample = LoadData(ds);

       //Combo Box to display Name only
        ddlName.DataSource = ds.Tables["Vals"];
        ddlName.DataTextField = "Name";
       
//Combo Box to display Address only
        ddlAddress.DataSource = ds.Tables["Vals"];
        ddlAddress.DataTextField = "Address";

//Combo Box to display Name&Address combined
        ddlNameAddress.DataSource = lstSample;
        ddlNameAddress.DataTextField = "NameAddress";

        Page.DataBind();
0
 
LVL 16

Expert Comment

by:kris_per
ID: 33444897
You can derive a new class (say MyComboBox) from ComboBox and add a Load method to it. Then add this mycombobox control to other forms and call the load method from that form (in construtor or in load or in any other place).

Even with this, you need to add MyComboBox to each form and its the form; considering this, it looks deriving new MyComboBox doesn;t add much value..So I would suggest a separate loader class for this which is simpler as following:


Add the combobox to each form
In Form's constructor or in load or in any other place, use the loader as shown in the code below:

See if this is closer to what are looking for.
public partial class Form1 : Form

{



  private ContactDetails m_contact;



  public Form5()

  {

    InitializeComponent();

    

    ComboBoxLoader.LoadContactItems(comboBox1, m_contact);

  }



  OR load when ContactDetails is set or changed



  public ContactDetails Contact

  {

    get { return m_contact; }

    set 

    {

        m_contact = value;

        ComboBoxLoader.LoadContactItems(comboBox1, m_contact);

    }

  }

}



///////////////////////////////////////



public class ComboBoxLoader

    {

        public static void LoadContactItems(ComboBox comboBox, ContactDetails contact)

        {

            if (this.Parent is Form1)

            {

                this.Items.Add(contact.Address);

            }

            else if (this.Parent is Form2)

            {

                this.Items.Add(contact.Name);

            }

            else if (this.Parent is Form3)

            {

                this.Items.Add(contact.Address);

                this.Items.Add(contact.PhoneNumber);

            }

        }

    }





public class ContactDetails

    {

        public string Name;

        public string Address;

        public string PhoneNumber;

    }

Open in new window

0
 
LVL 16

Expert Comment

by:kris_per
ID: 33445002

I have corrected some errors in the above code which is as following:
public class ComboBoxLoader

    {

        public static void LoadContactItems(ComboBox comboBox, ContactDetails contact)

        {

            comboBox.Items.Clear();



            if (comboBox.Parent is Form5)

            {

                comboBox.Items.Add(contact.Address);

            }

            else if (comboBox.Parent is Form4)

            {

                comboBox.Items.Add(contact.Name);

            }

            else if (comboBox.Parent is Form3)

            {

                comboBox.Items.Add(contact.Address);

                comboBox.Items.Add(contact.PhoneNumber);

            }

        }

    }

Open in new window

0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now