winform combox 下拉搜索

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;
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值