2011-08-01 26 views
28
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)); 
     } 

Dodaję je jak:Pierwsze wybrana wartość combobox

ComboboxItem item = new ComboboxItem(); 
        item.Text = cd.Name; 
        item.Value = cd.ID; 
        this.comboBox1.Items.Add(item); 

Wciąż otrzymuję NullReferenceExeption i nie wiem, dlaczego. tekst wydaje się dobrze.

Odpowiedz

31

Spróbuj tego:

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));   
} 
+6

Myślę, że klasa "ComboBoxItem" jest dostępna tylko w projektach WPF. – T30

13

Otrzymujesz NullReferenceExeption powodu używasz cmb.SelectedValue który jest null. comboBox nie wie, co jest wartością niestandardowej klasy ComboboxItem, więc albo zrobić:

ComboboxItem selectedCar = (ComboboxItem)comboBox2.SelectedItem; 
int selecteVal = Convert.ToInt32(selectedCar.Value); 

Albo lepiej jest wiążąca dane mechanicznych takich jak:

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.DataSource = items; 
this.comboBox1.DisplayMember = "Text"; 
this.comboBox1.ValueMember = "Value"; 
+0

Czy w ogóle nie ma możliwości użycia komendy comboBox.SelectedValue dla niestandardowego elementu bez użycia źródła danych? Na przykład. jeśli używasz źródła danych, nie możesz usunąć ani dodać przedmiotu do elementów combobox. – user3532232

6

Miałem podobny problem, My Klasa to

public class ServerInfo 
{ 
    public string Text { get; set; } 
    public string Value { get; set; } 
    public string PortNo { get; set; } 

    public override string ToString() 
    { 
     return Text; 
    } 
} 

Ale to, co zrobiłem, rzuciłem moją klasę na właściwość SelectedItem w ComboBox. Tak więc, będę mieć wszystkie właściwości klasy wybranego elementu.

// Code above 
ServerInfo emailServer = (ServerInfo)cbServerName.SelectedItem; 

mailClient.ServerName = emailServer.Value; 
mailClient.ServerPort = emailServer.PortNo; 

Mam nadzieję, że to pomoże komuś! Pozdrawiam!

-1

Spróbuj tego:

private void cmbLineColor_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     DataRowView drv = (DataRowView)cmbLineColor.SelectedItem; 
     int selectedValue = (int)drv.Row.ItemArray[1]; 
    } 
0

problem masz z SelectedValue nie jest konwersja do liczby całkowitej. Jest to główny problem, więc użycie poniższego fragmentu kodu pomoże:

int selectedValue; 
bool parseOK = Int32.TryParse(cmb.SelectedValue.ToString(), out selectedValue);