473,465 Members | 4,818 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

New to C# - Need help with comboboxes

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..
Jun 4 '06 #1
3 1956
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.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
Earn money answering .NET questions
http://www.eggheadcafe.com/forums/merit.asp

"Rick Shaw" <ri*****************@hotmail.com> wrote in message
news:%2*****************@TK2MSFTNGP05.phx.gbl...
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..

Jun 5 '06 #2
Rick Shaw wrote:
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_Selecte dIndexChanged);
}

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_Selecte dIndexChanged);
}

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
}

Jun 5 '06 #3
Hi Pieter. Thank you very much. I will try this out asap.

This is great.

Thanks again.

Rick..

"Pieter Breed" <pi**********@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Rick Shaw wrote:
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_Selecte dIndexChanged);
}

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_Selecte dIndexChanged);
}

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
}

Jun 5 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Marcin Borkowski | last post by:
Hello I need some small help in the Borland C++ Builder. I need to do something with string in format like "var1|var2|var3|var4|" and add it to some ComboBox (Items -> Add("var1"), Items ->...
9
by: Susan Bricker | last post by:
Greetings. I am having trouble populating text data that represents data in my table. Here's the setup: There is a People Table (name, address, phone, ...) peopleID = autonumber key There...
1
by: Stijn Goris | last post by:
Hi all, I have 3 Comboboxes i want to bind with 3 different DatTables. I use this.cbxProductCategory.DataSource = myDataTable; this.cbxProductCategory.DisplayMember = "PTCA_DESCR";...
17
by: Hazz | last post by:
In this sample code of ownerdraw drawmode, why does the '(ComboBox) sender' line of code need to be there in this event handler? Isn't cboFont passed via the managed heap, not the stack, into this...
6
by: Sam | last post by:
Hi, I have a datagrid which has (amongst other stuff) 2 comboboxes columns. So far so good. The trick is that when I select a value in my first column, it must updates the items of the combobox in...
0
by: EiEiO | last post by:
Hello all. I am hoping I could get some suggestions on moving forward. My Challenge: Importing data from one table to another where the field names almost never match. My Form has a list...
0
by: mjsterz | last post by:
I've been working with VB .NET for less than a year and this is the first time I've posted on one of these groups, so let me apologize beforehand if I'm being unclear, not posting my issue...
2
by: ashishtarlekar | last post by:
Hi all, I have to add comboboxes in the first row of datagridview for mapping the database fields. For e.g. I have to show Id, FirstName, LastName in each comboboxes in the first row. if i select...
1
by: Alex | last post by:
Hi, I have a strange question... I have a series of combo boxes being populated by the same dataset, and I've noticed if one is changed then all change as well. I'm using Visual Basic 2005 and...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.