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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM