using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
private List<string> listNames = new List<string>();
public Form1()
{
InitializeComponent();
this.comboBox1.Items.Add("ganquanfu");
this.comboBox1.Items.Add("quanfu");
this.comboBox1.Items.Add("ligang");
this.comboBox1.Items.Add("zhaojialong");
this.comboBox1.Items.Add("zhaozhen");
this.comboBox1.Items.Add("ganquanfu");
this.comboBox1.Items.Add("quanfu");
this.comboBox1.Items.Add("ligang");
this.comboBox1.Items.Add("zhaojialong");
this.comboBox1.Items.Add("zhaozhen");
this.comboBox1.Items.Add("ganquanfu");
this.comboBox1.Items.Add("quanfu");
this.comboBox1.Items.Add("ligang");
this.comboBox1.Items.Add("zhaojialong");
this.comboBox1.Items.Add("zhaozhen");
foreach (var item in comboBox1.Items)
{
listNames.Add(item.ToString());
}
//设置DropDown的高度
// this.comboBox1.DropDownHeight = 100;
//以下是事件处理函数的另一种写法,Lambda表达式书写
this.comboBox1.TextChanged += new EventHandler(comboBox1_TextChanged);
//this.comboBox1.DropDown += (a, b) => {
// if (a is ComboBox)
// {
// ComboBox sender = a as ComboBox;
// MessageBox.Show(sender.Items[0].ToString());
// }
//};
this.comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
// this.comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
// this.comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
}
void comboBox1_TextChanged(object sender, EventArgs e)
{
string search = comboBox1.Text.Trim();
if (string.IsNullOrWhiteSpace(search))
{
return;
}
comboBox1.Items.Clear();
var results = listNames.Where(t => t.Contains(search)).ToArray();
foreach (var item in results)
{
comboBox1.Items.Add(item);
}
comboBox1.DroppedDown = true;
this.Cursor = Cursors.Arrow;
comboBox1.SelectionStart = search.Length;
}
void comboBox1_DropDownClosed(object sender, EventArgs e)
{
// MessageBox.Show(this.comboBox1.DroppedDown.ToString());
}
void comboBox1_DropDown(object sender, EventArgs e)
{
// MessageBox.Show(this.comboBox1.DroppedDown.ToString());
}
private void button1_Click(object sender, EventArgs e)
{
//MessageBox.Show(this.comboBox1.DroppedDown.ToString());
this.comboBox1.DroppedDown = true;
}
private void Form1_Load(object sender, EventArgs e)
{
this.Cursor = System.Windows.Forms.Cursors.Cross;
}
}
}