Solved

Load Combobox from string with option

Posted on 2010-08-16
9
314 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Expert Comment

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

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

756 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