1. 程式人生 > >winform Textbox模糊搜索實現下拉顯示+提示文字

winform Textbox模糊搜索實現下拉顯示+提示文字

iop class ddr ast ati 兩個 ram component ext

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

技術分享

winform Textbox模糊搜索實現下拉顯示+提示文字