从ComboBox获取项目计数和引用 [英] Getting item count and reference from ComboBox

查看:184
本文介绍了从ComboBox获取项目计数和引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我遇到了ComboBoxes的另一个难题:)对你来说很容易。



我有一个应用程序有多个ComboBoxes。这些ComboBox的值取自存储在另一台计算机上的XML文件。值显示正常,但是当我尝试拉出项目数时 - 我得到0。



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:

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!

推荐答案

可能不是 - 但很可能你是在错误的时间做的。

如果你在同一个代码中这样做,你将项目加载到然后很可能该值很小,因为项目没有立即从XML文件中加载 - 它等到组合框显示之前才加载它们。



尝试将代码移动到按钮处理程序中,我打赌你得到正确的号码!
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!




检查出来:

http://www.c-sharpcorner.com/uploadfile/mahesh/combobox -in-C-Sharp / [ ^ ]


它对我有用

string [] languages = new string [2];

languages [0] =English;

languages [1] =German;

DataSet myDataSet = new DataSet(); < br $>


// ---准备工作

DataTable lTable = new DataTable(Lang);

DataColumn lName = new DataColumn(Language,typeof(string));

lTable.Columns.Add(lName);

for(int i = 0;我< languages.Length; i ++)

{

DataRow lLang = lTable.NewRow();

lLang [Language] = languages [i];

lTable.Rows.Add(lLang);

}

myDataSet.Tables.Add(lTable);



// ---处理组合框

comboBox1.DataSource = myDataSet.Tables [Lang]。DefaultView;

comboBox1.DisplayMember = 语言;



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

如果它对你没用,你可以发布你的代码吗?
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?


这篇关于从ComboBox获取项目计数和引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆