Click here to Skip to main content
15,887,379 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys,
I bumped into another difficulty with ComboBoxes :) Will be easy one for you.

I have an application with multiple ComboBoxes. The values from these ComboBoxes are taken from an XML file which is stored on yet another computer. The values are displayed ok, but when I try to pull out the item count - I get "0".

MessageBox.Show(ComboBox_Name.Items.Count.ToString());


Am I using wrong function for this purpose?

Update:

If I try to count the items that are stored manually - I get good values:

C#
CB1.Items.Add("Item #1");
CB1.Items.Add("Item #2");
CB1.Items.Add("Item #3");
CB1.Items.Add("Item #4");
CB1.Items.Add("Item #5");

MessageBox.Show(BC1.Items.Count.ToString());
//this would give me a message showing "5"
//If I do the same with CB1 (with values populated from XML file). I get "0"



Thanks!
Posted
Updated 8-Jul-13 23:14pm
v2
Comments
praks_1 9-Jul-13 4:49am    
Check if its not refer to any other combobox means references
MK-Gii 9-Jul-13 4:56am    
What do you mean by that? If you're asking if the same list from XML file is not being used by other ComboBoxes - then yes - the same list is being used in many comboboxes. And in the example above I am referencing combobox by name - it's unique and is not repeating anywhere else (otherwise I would get an error).
praks_1 9-Jul-13 5:08am    
It won,t give u an error check what u mean by referencing or else post ur question in right manner
MK-Gii 9-Jul-13 6:18am    
Ouch...
Looks like someone has get up on the wrong side of the bed :)
Chill out bro and don't sh** your pants ;]

Probably not - but it's quite likely you are doing it at the wrong time.
If you are doing it in the same code that you are loading the items into the combobox then it is very likely that the value will be zero because the items are not loaded from the XML file immediately - it waits until the combobox is shown before loading them.

Try moving the code into a button handler, and I bet you get the right number!
 
Share this answer
 
Comments
MK-Gii 9-Jul-13 6:07am    
You're my man! Worked like a charm. I've set the counter to count it after 2 second after program starts. I owe you this!
Silly how I did not though about this...
OriginalGriff 9-Jul-13 6:12am    
:laugh:
It's not something you think about until it bites you!
It works for me
string [] languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();

// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);

// --- Handling the combobox
comboBox1.DataSource = myDataSet.Tables["Lang"].DefaultView;
comboBox1.DisplayMember = "Language";

MessageBox.Show(comboBox1.Items.Count.ToString());
If it is not helpful for you, can you post your code?
 
Share this answer
 
It works for me
string [] languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();

// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);

// --- Handling the combobox
comboBox1.DataSource = myDataSet.Tables["Lang"].DefaultView;
comboBox1.DisplayMember = "Language";

MessageBox.Show(comboBox1.Items.Count.ToString());
If it is not helpful for you, can you post your code?
 
Share this answer
 
 
Share this answer
 
Comments
MK-Gii 9-Jul-13 5:10am    
Thanks for link but it's not what I have. If I have the items stored manually - then the Count return good value. If I get the items from an XML - it gets wrong Count value.
I have updated my Question at the top with more code samples.
It works for me
string [] languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();

// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);

// --- Handling the combobox
comboBox1.DataSource = myDataSet.Tables["Lang"].DefaultView;
comboBox1.DisplayMember = "Language";

MessageBox.Show(comboBox1.Items.Count.ToString());
If it is not helpful for you, can you post your code?
 
Share this answer
 
It works for me
string [] languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();

// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);

// --- Handling the combobox
comboBox1.DataSource = myDataSet.Tables["Lang"].DefaultView;
comboBox1.DisplayMember = "Language";

MessageBox.Show(comboBox1.Items.Count.ToString());
If it is not helpful for you, can you post your code?
 
Share this answer
 
It works for me
string [] languages = new string[2];
languages[0] = "English";
languages[1] = "German";
DataSet myDataSet = new DataSet();

// --- Preparation
DataTable lTable = new DataTable("Lang");
DataColumn lName = new DataColumn("Language", typeof(string));
lTable.Columns.Add(lName);
for (int i = 0; i < languages.Length; i++)
{
DataRow lLang = lTable.NewRow();
lLang["Language"] = languages[i];
lTable.Rows.Add(lLang);
}
myDataSet.Tables.Add(lTable);

// --- Handling the combobox
comboBox1.DataSource = myDataSet.Tables["Lang"].DefaultView;
comboBox1.DisplayMember = "Language";

MessageBox.Show(comboBox1.Items.Count.ToString());
If it is not helpful for you, can you post your code?
 
Share this answer
 
Comments
MK-Gii 9-Jul-13 5:05am    
M8 - no need to post the same responce so many times... I read it and the difference is that you're not trying to get the count of ComboBox items which is getting values from XML file...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900