I’m trying to show a ComboBox on a PropertyGrid. I don’t mean a dropdown string list because I need the index of the selected item associated with the description.
This is easy to obtain with a ComboBox control on a Form but I can’t get the same result by publishing a ComboBox type property in a PropertyGrid.
On My code below
I’m trying to get a ComboBox on a PropertyGrid.
<code> public class Wrap {
private ComboBox _Combo = new ComboBox();
public Wrap() {
_Combo.SelectedIndexChanged += new System.EventHandler(Combo_SelectedIndexChanged);
_Combo.Items.Add(new Item() { Text = "Text1", Value = "Value1" });
_Combo.Items.Add(new Item() { Text = "Text2", Value = "Value2" });
_Combo.DisplayMember = "Text";
_Combo.ValueMember = "Value";
_Combo.SelectedIndex = 1;
}
private void Combo_SelectedIndexChanged(object sender, EventArgs e) {
ComboBox cb = sender as ComboBox;
int i = cb.SelectedIndex;
if (i < 0)
return;
Item it = cb.Items[i] as Item;
string s = it.Value;
}
public ComboBox Combo {
get { return _Combo; }
set {
// Temporary test to avoid "null" (the only selection possible from propertygrid)
if (value == null)
return;
_Combo = value;
}
}
}
public class Item {
public string Value { set; get; }
public string Text { set; get; }
}
</code>
<code> public class Wrap {
private ComboBox _Combo = new ComboBox();
public Wrap() {
_Combo.SelectedIndexChanged += new System.EventHandler(Combo_SelectedIndexChanged);
_Combo.Items.Add(new Item() { Text = "Text1", Value = "Value1" });
_Combo.Items.Add(new Item() { Text = "Text2", Value = "Value2" });
_Combo.DisplayMember = "Text";
_Combo.ValueMember = "Value";
_Combo.SelectedIndex = 1;
}
private void Combo_SelectedIndexChanged(object sender, EventArgs e) {
ComboBox cb = sender as ComboBox;
int i = cb.SelectedIndex;
if (i < 0)
return;
Item it = cb.Items[i] as Item;
string s = it.Value;
}
public ComboBox Combo {
get { return _Combo; }
set {
// Temporary test to avoid "null" (the only selection possible from propertygrid)
if (value == null)
return;
_Combo = value;
}
}
}
public class Item {
public string Value { set; get; }
public string Text { set; get; }
}
</code>
public class Wrap {
private ComboBox _Combo = new ComboBox();
public Wrap() {
_Combo.SelectedIndexChanged += new System.EventHandler(Combo_SelectedIndexChanged);
_Combo.Items.Add(new Item() { Text = "Text1", Value = "Value1" });
_Combo.Items.Add(new Item() { Text = "Text2", Value = "Value2" });
_Combo.DisplayMember = "Text";
_Combo.ValueMember = "Value";
_Combo.SelectedIndex = 1;
}
private void Combo_SelectedIndexChanged(object sender, EventArgs e) {
ComboBox cb = sender as ComboBox;
int i = cb.SelectedIndex;
if (i < 0)
return;
Item it = cb.Items[i] as Item;
string s = it.Value;
}
public ComboBox Combo {
get { return _Combo; }
set {
// Temporary test to avoid "null" (the only selection possible from propertygrid)
if (value == null)
return;
_Combo = value;
}
}
}
public class Item {
public string Value { set; get; }
public string Text { set; get; }
}
New contributor
user25453807 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.