110,534
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string searchText = textBox1.Text;
int index = comboBox1.FindString(searchText);
if (index < 0)
{
MessageBox.Show("Not Found");
return;
}
Listitem searchItem = comboBox1.Items[index] as Listitem;
MessageBox.Show(searchItem.ToString());
}
private void Form1_Load(object sender, EventArgs e)
{
List<Listitem> data = new List<Listitem>();
for (int i = 0; i < 10; i++)
data.Add(new Listitem { name = string.Format("COM{0:D}", i + 1), com = i + 1 });
comboBox1.DataSource = data;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "com";
}
}
class Listitem
{
public string name { get; set; }
public int com { get; set; }
public override string ToString()
{
return name;
}
}