• winform Textbox模糊搜索实现下拉显示+提示文字


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace _04TextBox
    {
        public partial class Form1 : Form
        {
            List<string> Data = new List<string>();
    
            string Randomstr = "功夫撒黑胡椒hcbvf蜂窝qwertyuiopasdfghjklzxcvbnm法国的恢复到飞范德萨QWERTYUIOPASDFGHJKLZXCVBNM出现过热423贴①46546也有一头热刚恢复到贴3天赋如头3广泛的我让他";
    
    
            Random rd = new Random(GetRandomSeed());
    
            static int GetRandomSeed()
            {
                byte[] bytes = new byte[4];
                System.Security.Cryptography.RNGCryptoServiceProvider rng = new System.Security.Cryptography.RNGCryptoServiceProvider();
                rng.GetBytes(bytes);
                return BitConverter.ToInt32(bytes, 0);
            }
    
            public Form1()
            {
                InitializeComponent();
    
                //2000W数据,这里会有卡顿现象,这里修改为200W
                for (int i = 0; i < 2000000; i++)
                {
                    Data.Add(Randomstr.ToCharArray()[rd.Next(Randomstr.Length)].ToString()
                        + Randomstr.ToCharArray()[rd.Next(Randomstr.Length)].ToString()
                        + Randomstr.ToCharArray()[rd.Next(Randomstr.Length)].ToString()
                        + Randomstr.ToCharArray()[rd.Next(Randomstr.Length)].ToString()
                        + Randomstr.ToCharArray()[rd.Next(Randomstr.Length)].ToString());
                }
                //重点代码
                this.textBox1.AutoCompleteCustomSource.Clear();
                this.textBox1.AutoCompleteCustomSource.AddRange(Data.ToArray());
                this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
                this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
        }
    }
    
    

     提示内容,加个变量跟两个事件

    #region 实现提示文字
            Boolean textboxHasText = false;//判断输入框是否有文本 
            /// <summary>
            /// textbox获得焦点
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void textBox1_Enter(object sender, EventArgs e)
            {
                if (textboxHasText == false)
                    textBox1.Text = "";
    
                textBox1.ForeColor = Color.Black;
            }
    
            /// <summary>
            /// textbox失去焦点  
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void textBox1_Leave(object sender, EventArgs e)
            {
                if (textBox1.Text == "")
                {
                    textBox1.Text = "提示内容";
                    textBox1.ForeColor = Color.LightGray;
                    textboxHasText = false;
                }
                else
                    textboxHasText = true;
            }
            #endregion

  • 相关阅读:
    linux 查看磁盘空间大小
    Redis内存碎片率
    redis的incr和incrby命令
    redis如何清空当前缓存和所有缓存
    ArcGIS矢量数据批量合并工具
    arcgis 获得工具有多少个
    GoogleEarth二次开发难点和技巧
    ArcGIS 智能批量赋高程工具
    arcgis python支持汉字
    ArcGIS 宗地图批量打印输出
  • 原文地址:https://www.cnblogs.com/lsgsanxiao/p/7008636.html
Copyright © 2020-2023  润新知