winform下的智能提示框

winform下的智能提示框

  最近在搞winform的程序,接触到有些可能以后还会用到的功能,所以写到博客园里去,第一可以加深自己的印象,第二可以在以后再遇到同样问题的时候忘记了可以马上回来看看,第三希望可以帮到有同样有需要的人。

  首先给大家上图看一下效果:

这个也类似百度搜索框的吧,哈哈。

  个人感觉,winform下的智能提示比在web下实现容易多了,因为winform本来就是异步的。好,少废话了,上代码:

        //产品智能提示绑定
        private void BindProduct()
        {
            try
            {
                DataSet ds = TradeToolsService.GetProducts();
                if (ds != null)
                {
                    var ac = new AutoCompleteStringCollection();
                    DataTable dt = ds.Tables[0];
                    int j = dt.Rows.Count;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        ac.Add(dt.Rows[i]["Product_Name"] + "");
                    }
                    this.textBoxProductName.AutoCompleteMode = AutoCompleteMode.Suggest;
                    this.textBoxProductName.AutoCompleteSource = AutoCompleteSource.CustomSource;
                    this.textBoxProductName.AutoCompleteCustomSource = ac;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

  

  OK,就这么简单,在页面加载时候调用这个方法就好了,记得把数据换成你想要的数据哦。

 

 

  

原文地址:https://www.cnblogs.com/zknu/p/3191558.html