private void AddDatingForm_Load(object sender, EventArgs e)
{
NameValueCollection nvcol = new NameValueCollection();
nvcol.Add("UserPhone", "");
string url = string.Format("{0}{1}", datingInterfaceUrl, MethodNames.GetUserInfo2);
json = HttpWebResponseUtility.HttpPost(nvcol, url);
var userInfo = JsonHelper.DeserializeObject<Response<List<UserObj>>>(json);
if (userInfo.isError != "True")
{
comboBox1.Items.AddRange(userInfo.Result.ToArray());
comboBox1.DisplayMember = "USERNO";
comboBox1.ValueMember = "USERID";
userList = userInfo.Result; ;
comboBox1.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
comboBox1.SelectedIndex = -1;
comboBox1.TextChanged += comboBox1_TextChanged;
}
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox combo = sender as ComboBox;
if (combo.SelectedIndex == -1)
{
txtName.Text = "";
txtUserOu.Text = "";
return;
}
else
{
UserObj tv = combo.SelectedItem as UserObj;
if (tv != null)
{
txtName.Text = tv.USERNAME;
txtUserOu.Text = tv.OU;
}
else
{
txtName.Text = "";
txtUserOu.Text = "";
}
}
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
ComboBox comboBox = sender as ComboBox;
if (comboBox.SelectedIndex == -1)
{
comboBox.TextChanged -= comboBox1_TextChanged;
comboBox.SelectedIndexChanged -= comboBox1_SelectedIndexChanged;
string search = comboBox.Text.Trim();
var newSource = userList.Where(p => p.USERNO.Contains(search)).ToList();
comboBox.DataSource = newSource;
comboBox.SelectedIndex = -1;
comboBox.DropDownHeight = 200;
comboBox.DroppedDown = true;
comboBox.Text = search;
comboBox.SelectionStart = search.Length;
Cursor = Cursors.Default;
comboBox.TextChanged += comboBox1_TextChanged;
comboBox.SelectedIndexChanged += comboBox1_SelectedIndexChanged;
}
}
重写combobox
public partial class ComboBoxEx : ComboBox
{
public event EventHandler NoSelectItem;
public ComboBoxEx()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
protected override void WndProc(ref Message m)
{
//Console.WriteLine(m.LParam);
//if (this.SelectedIndex == 0 && this.Items.Count == 0)
// return;
try
{
base.WndProc(ref m);
}
catch (ArgumentOutOfRangeException e)
{
if (this.SelectedIndex == 0 && this.Items.Count == 0)
{
if (NoSelectItem != null)
NoSelectItem(this, new EventArgs());
return;
}
else
throw;
}
}
}