New to C# - Need help with comboboxes

  • Thread starter Thread starter Rick Shaw
  • Start date Start date
R

Rick Shaw

Hi. I am very new to C# and needed help. Use to program in Delphi but
switching to C#. I am just starting my very first application and already
don't know what to do. Here is what I'm trying to do.

I have 2 comboboxes. 1st combobox is filled (dataset binding) with list of
companies from the form load. The 2nd combobox, contain list (filled from
dataset then bind) of employees where each employee belongs to a specific
company. So, when someone selects a company in the 1st combobox, a query
gets executed and the result (based on the company filter) gets loaded in
the 2nd combobox.
Here is the problem. When I run the program, during form load, I get errors
cuz the 1st combobox SelectedIndexChanged event fires.

Is there a way to not trigger the SelectedIndexChanged event during
formload? Also, how do I get the ValueMamber of the combobox and assign it
to a variable?

Any help will be greatly appreciated.

Thanks,

Rick..
 
You'll need to set up a form (class level) bool to
tell your events when to ignore and when not to.

Then, in each event, check the bool whether
to return out of the method prior to executing
your code.

You'll undoubtedly want this for other controls
for the same reason.

As for the value of the combobox, look
at the SelectedItem or SelectedValue.
 
Rick said:
Here is the problem. When I run the program, during form load, I get errors
cuz the 1st combobox SelectedIndexChanged event fires.

Is there a way to not trigger the SelectedIndexChanged event during
formload? Also, how do I get the ValueMamber of the combobox and assign it
to a variable?

Hi Rick,

I prefer to do the following.

* Only set your event handler for the combobox item changed event at
the end of the Form.Load event, something like this:

private void Form1_Load(object sender, EventArgs e)
{
LoadCompaniesIntoCombo();
this.comboBoxCompanies.SelectedIndexChanged += new
System.EventHandler(this.comboBoxCompanies_SelectedIndexChanged);
}

Hope this helps. Below is a complete example that shows how I usually
handle combo-boxes and UI's in general.

Pieter

class comboBoxCompaniesItem
{
public readonly string Name;
public readonly int ID;
public comboBoxCompaniesItem(string pName, int pID)
{
Name = pName;
ID = pID;
}

public override string ToString()
{
return string.Format("{0} ({1})", Name, ID);
}
}

private void Form1_Load(object sender, EventArgs e)
{
LoadCompaniesIntoCombo();
this.comboBoxCompanies.SelectedIndexChanged += new
System.EventHandler(this.comboBoxCompanies_SelectedIndexChanged);
}

private void LoadCompaniesIntoCombo()
{
comboBoxCompanies.BeginUpdate();
comboBoxCompanies.Items.Clear();
using (DataTable dt = SomeQueryingMethod())
{
foreach (DataRow dr in dt.Rows)
{
comboBoxCompanies.Items.Add(new
comboBoxCompaniesItem((string)dr[0], (int)dr[1]));
}
}
comboBoxCompanies.EndUpdate();
}

private void comboBoxCompanies_SelectedIndexChanged(object
sender, EventArgs e)
{
comboBoxCompaniesItem selectedCo =
comboBoxCompanies.SelectedItem as comboBoxCompaniesItem;
if (selectedCo == null)
{
comboBoxEmployees.Items.Clear();
return;
}

// do employee loading here
}
 
Hi Pieter. Thank you very much. I will try this out asap.

This is great.

Thanks again.

Rick..

Pieter Breed said:
Rick said:
Here is the problem. When I run the program, during form load, I get errors
cuz the 1st combobox SelectedIndexChanged event fires.

Is there a way to not trigger the SelectedIndexChanged event during
formload? Also, how do I get the ValueMamber of the combobox and assign it
to a variable?

Hi Rick,

I prefer to do the following.

* Only set your event handler for the combobox item changed event at
the end of the Form.Load event, something like this:

private void Form1_Load(object sender, EventArgs e)
{
LoadCompaniesIntoCombo();
this.comboBoxCompanies.SelectedIndexChanged += new
System.EventHandler(this.comboBoxCompanies_SelectedIndexChanged);
}

Hope this helps. Below is a complete example that shows how I usually
handle combo-boxes and UI's in general.

Pieter

class comboBoxCompaniesItem
{
public readonly string Name;
public readonly int ID;
public comboBoxCompaniesItem(string pName, int pID)
{
Name = pName;
ID = pID;
}

public override string ToString()
{
return string.Format("{0} ({1})", Name, ID);
}
}

private void Form1_Load(object sender, EventArgs e)
{
LoadCompaniesIntoCombo();
this.comboBoxCompanies.SelectedIndexChanged += new
System.EventHandler(this.comboBoxCompanies_SelectedIndexChanged);
}

private void LoadCompaniesIntoCombo()
{
comboBoxCompanies.BeginUpdate();
comboBoxCompanies.Items.Clear();
using (DataTable dt = SomeQueryingMethod())
{
foreach (DataRow dr in dt.Rows)
{
comboBoxCompanies.Items.Add(new
comboBoxCompaniesItem((string)dr[0], (int)dr[1]));
}
}
comboBoxCompanies.EndUpdate();
}

private void comboBoxCompanies_SelectedIndexChanged(object
sender, EventArgs e)
{
comboBoxCompaniesItem selectedCo =
comboBoxCompanies.SelectedItem as comboBoxCompaniesItem;
if (selectedCo == null)
{
comboBoxEmployees.Items.Clear();
return;
}

// do employee loading here
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top