Solved

Load Combobox from string with option

Posted on 2010-08-16
9
310 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 41
Problem to error 4 43
Vb.net dynamic formulas in runtime 11 61
Class object 2 26
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
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.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

16 Experts available now in Live!

Get 1:1 Help Now