?
Solved

Load Combobox from string with option

Posted on 2010-08-16
9
Medium Priority
?
322 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 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