Solved

Load Combobox from string with option

Posted on 2010-08-16
9
312 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
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

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