Set the selecteditem of a combobox based on key,value pair.

后端 未结 4 861
情深已故
情深已故 2021-01-27 17:32

I have a combobox that I populate like this:

this.reqTypeInput.Items.Add(new RequestType(\"Label 1\", \"Value1\"));
this.reqTypeInput.Items.Add(new RequestType(\         


        
4条回答
  •  执念已碎
    2021-01-27 18:15

    If the request types do not change, you could store each RequestType object in a variable first, then set the SelectedItem property of the ComboBox to that variable.

    For example:

    RequestType type1 = New RequestType("Label 1", "Value 1");
    RequestType type2 = New RequestType("Label 2", "Value 2");
    
    reqTypeInput.Items.Add(type1);
    reqTypeInput.Items.Add(type2);
    

    Then, set it like this:

    reqTypeInput.SelectedItem = type2;
    

提交回复
热议问题