自定义TextBox(带有自动完成输入项功能)

来源:互联网 发布:知乎 经典 编辑:程序博客网 时间:2024/05/16 16:58

使用之前需要配置自动匹配字段表的路径(txt文档,每行代表一个匹配项)

解释不够详细,转载请注明出处

  public class AutoTextBox: TextBox    {        public AutoTextBox()        {            AutoCompleteSource = AutoCompleteSource.CustomSource;            AutoCompleteMode = AutoCompleteMode.SuggestAppend;                                                 }        private string codePath;                public string CodePath        {            get { return codePath; }            set            {                 codePath = value;                if (!string.IsNullOrWhiteSpace(codePath))                {                    LoadCodeTable();                }            }        }                void LoadCodeTable()        {            List<string> list = new List<string>();            try            {                               var strs = File.ReadAllLines(codePath,Encoding.Default);                foreach (var item in strs)                {                    if (!string.IsNullOrWhiteSpace(item))                    {                        list.Add(item);                    }                }                AutoCompleteCustomSource.AddRange(list.ToArray());            }            catch (Exception ex)            {#if DEBUG                MessageBox.Show(string.Format("{0}\r\n{1}", ex.Message, ex.StackTrace));#endif            }                    }    }


0 0