[英]Getting selected value of a combobox
public class ComboboxItem {
public string Text { get; set; }
public string Value { get; set; }
public override string ToString() { return Text; }
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedIndex = comboBox1.SelectedIndex;
int selecteVal = (int)comboBox1.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)comboBox1.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
我正在添加它們,例如:
ComboboxItem item = new ComboboxItem();
item.Text = cd.Name;
item.Value = cd.ID;
this.comboBox1.Items.Add(item);
我不斷收到 NullReferenceExeption 並且不知道為什么。 文本似乎顯示得很好。
試試這個:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmb = (ComboBox)sender;
int selectedIndex = cmb.SelectedIndex;
int selectedValue = (int)cmb.SelectedValue;
ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
由於您使用的是 null 的cmb.SelectedValue
,因此您獲得NullReferenceExeption
。 組合comboBox
不知道您的自定義類ComboboxItem
的值是多少,因此要么執行以下操作:
ComboboxItem selectedCar = (ComboboxItem)comboBox2.SelectedItem;
int selecteVal = Convert.ToInt32(selectedCar.Value);
或者更好的是使用數據綁定,如:
ComboboxItem item1 = new ComboboxItem();
item1.Text = "test";
item1.Value = "123";
ComboboxItem item2 = new ComboboxItem();
item2.Text = "test2";
item2.Value = "456";
List<ComboboxItem> items = new List<ComboboxItem> { item1, item2 };
this.comboBox1.DisplayMember = "Text";
this.comboBox1.ValueMember = "Value";
this.comboBox1.DataSource = items;
我有一個類似的錯誤,我的班級是
public class ServerInfo
{
public string Text { get; set; }
public string Value { get; set; }
public string PortNo { get; set; }
public override string ToString()
{
return Text;
}
}
但是我所做的是將我的類轉換為 ComboBox 的 SelectedItem 屬性。 因此,我將擁有所選項目的所有類屬性。
// Code above
ServerInfo emailServer = (ServerInfo)cbServerName.SelectedItem;
mailClient.ServerName = emailServer.Value;
mailClient.ServerPort = emailServer.PortNo;
我希望這可以幫助別人! 干杯!
試試這個:
private void cmbLineColor_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView drv = (DataRowView)cmbLineColor.SelectedItem;
int selectedValue = (int)drv.Row.ItemArray[1];
}
SelectedValue
的問題不是轉換為整數。 這是主要問題,因此使用以下代碼片段將對您有所幫助:
int selectedValue;
bool parseOK = Int32.TryParse(cmb.SelectedValue.ToString(), out selectedValue);
試試這個:
int selectedIndex = comboBox1.SelectedIndex;
comboBox1.SelectedItem.ToString();
int selectedValue = (int)comboBox1.Items[selectedIndex];
您必須將所選項目轉換為您的自定義類 (ComboboxItem) 試試這個:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cmb = (ComboBox)sender;
int selectedIndex = cmb.SelectedIndex;
string selectedText = this.comboBox1.Text;
string selectedValue = ((ComboboxItem)cmb.SelectedItem).Value.ToString();
ComboboxItem selectedCar = (ComboboxItem)cmb.SelectedItem;
MessageBox.Show(String.Format("Index: [{0}] CarName={1}; Value={2}", selectedIndex, selectedCar.Text, selecteVal));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.