打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#实现文本框下拉框效果!(Winform)
using System;   
using System.Collections.Generic;   
using System.Text;   
using System.Windows.Forms;   
using System.Data.SqlClient;   
 
namespace NewApp   
{   
    class AutoComplete   
    {   
        List<TextBox> _CompleteObjectList = new List<TextBox>();   
        Dictionary<string, AutoCompleteStringCollection> _Source = new Dictionary<string, AutoCompleteStringCollection>();   
        SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=TestDB;Integrated Security=True");   
        public AutoComplete()   
        {   
            conn.Open();   
            SqlCommand cmd = new SqlCommand("select * from AutoComplete", conn);   
            SqlDataReader read = cmd.ExecuteReader();   
            while (read.Read())   
            {   
                string key = read["name"].ToString();   
                if (!_Source.ContainsKey(key))   
                    _Source.Add(key, new AutoCompleteStringCollection());   
                _Source[key].Add(read["str"].ToString());   
            }   
            read.Close();   
            conn.Close();   
        }   
 
        public void AddAll(Control item)   
        {   
            for (int i = 0; i < item.Controls.Count; i++)   
            {   
                Control var = item.Controls[i];   
                if (var.GetType().Equals(typeof(TextBox)))   
                {   
                    Add(var as TextBox);   
                }   
            }   
        }   
 
        public void Add(TextBox text)   
        {   
            _CompleteObjectList.Add(text);   
            text.Leave += new EventHandler(text_Leave);   
            text.AutoCompleteMode = AutoCompleteMode.SuggestAppend;   
            text.AutoCompleteSource = AutoCompleteSource.CustomSource;   
            if (!_Source.ContainsKey(text.Name))   
            {   
                _Source.Add(text.Name, new AutoCompleteStringCollection());   
            }   
            text.AutoCompleteCustomSource = _Source[text.Name];   
        }   
 
        public void Delete(TextBox text)   
        {   
            _CompleteObjectList.Remove(text);   
        }   
 
        public void DeleteAll(Control item)   
        {   
            for (int i = 0; i < item.Controls.Count; i++)   
            {   
                Control var = item.Controls[i];   
                if (var.GetType().Equals(typeof(TextBox)))   
                {   
                    Delete(var as TextBox);   
                }   
            }   
        }   
 
        public void AutoCompleteClear()   
        {   
            foreach (AutoCompleteStringCollection var in _Source.Values)   
            {                var.Clear();            }   
            SqlCommand cmd = new SqlCommand("Delete AutoComplete", conn);   
            conn.Open();   
            cmd.ExecuteNonQuery();   
            conn.Close();   
        }   
 
        void text_Leave(object sender, EventArgs e)   
        {   
            TextBox text = sender as TextBox;   
            if (text.Text == "")   
                return;   
            string key = text.Name;   
            if (!_Source.ContainsKey(key))   
            {   
                _Source.Add(key, new AutoCompleteStringCollection());   
            }   
            if (!_Source[key].Contains(text.Text))   
            {   
                SqlCommand cmd = new SqlCommand("insert into AutoComplete select '" + key.Replace("'", "''") + "', '" + text.Text.Replace("'", "''") + "'", conn);   
                conn.Open();   
                cmd.ExecuteNonQuery();   
                _Source[key].Add(text.Text);   
                conn.Close();   
            }   
        }   
    }   
}  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
VS连接SQL Server 2008,并实现登录和注册功能
VB.net数据库编程(07):SQLserver中的通讯录
C#问题向数据库添加一个datatime类型的数据
用C#将数据插入数据库,判断是否重复
SqlParameter对象的应用
VS连接SQL Server数据库,增删改查详细教程(C#代码)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服