使い慣れたプロパティは Winforms にはありませんが、ComboBox
はオブジェクトを受け取るため、必要なプロパティを使用して独自のカスタム クラスを作成できます。ListControl.DisplayMember プロパティに関する MSDN ドキュメントを参考にして、例として修正しました。
それが行うことは、プロパティで呼び出さcustomComboBoxItem
れるカスタム クラスを作成することです。次に、リストを作成し、それをプロパティを DisplayMember として割り当てます。これが実行可能かどうかを確認してください。Text
Value
DataSource
ComboBox
Text
public partial class Form1 : Form
{
List<customComboBoxItem> customItem = new List<customComboBoxItem>();
public Form1()
{
InitializeComponent();
customItem.Add(new customComboBoxItem("text1", "id1"));
customItem.Add(new customComboBoxItem("text2", "id2"));
customItem.Add(new customComboBoxItem("text3", "id3"));
customItem.Add(new customComboBoxItem("text4", "id4"));
comboBox1.DataSource = customItem;
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "Value";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show( ((customComboBoxItem)comboBox1.SelectedItem).Text + " "
+ ((customComboBoxItem)comboBox1.SelectedItem).Value);
}
}
public class customComboBoxItem
{
private string text;
private string value;
public customComboBoxItem(string strText, string strValue)
{
this.text = strText;
this.value = strValue;
}
public string Text
{
get { return text; }
}
public string Value
{
get { return value; }
}
}