c#模仿百度關鍵詞提示

來源:互聯網
上載者:User

標籤:c#   關鍵字提示   

1:from表單環境:TextBox(關鍵詞文字框)、ListBox(提示框)

2:實現思路:

   2.1:以輸入的關鍵詞為條件查詢  資料庫(在查詢中以點擊率排序就加一個order by 點擊率 desc)返回多行單列資料結果集合。再一一賦值到listBox中顯示。

   2.2:在TextBox中如果偵聽鍵盤

            if(Down(小鍵盤向下))

                  則先擷取到ListBox選中的索引,如果返回-1 或者x+1的值大於ListBox.count()   則初始值=0; 否則為ListBox索引值為x+1

          else (Up(小鍵盤向上))

               則先擷取到ListBox選中的索引,如果返回-1 或者x-1的值< 0      則初始值=ListBox.count() ; 否則為ListBox索引值為x-1;

3:表單代碼如下

using System;using System.Collections.Generic;using System.Drawing;using System.Windows.Forms;using System.Threading;using System.IO;using System.IO.Ports;using System.Text.RegularExpressions;using System.Text;/// <summary>///     <author>hubiao</author>///<date>2014-07-30</date>/// </summary>namespace ee{/// <summary>/// Description of MainForm./// </summary>public partial class MainForm : Form{private List<String> dataBuffer = new List<String>();public MainForm(){InitializeComponent();//程式與from表單分離執行Control.CheckForIllegalCrossThreadCalls = false;}void Button1Click(object sender, System.EventArgs e){textBox1.Text = String.Empty;}//這裡可以從資料庫初始化值void MainFormLoad(object sender, EventArgs e){dataBuffer.Add("心態");dataBuffer.Add("信心");dataBuffer.Add("能核心");dataBuffer.Add("醉心能");dataBuffer.Add("linuax");dataBuffer.Add("linuax改變IT世界");dataBuffer.Add("java");dataBuffer.Add("java是點亮星星之火");dataBuffer.Add("linaux改變IT世界");dataBuffer.Add("java");dataBuffer.Add("java是星星");dataBuffer.Add("湖北省");dataBuffer.Add("湖北跨省");dataBuffer.Add("湖北武漢");dataBuffer.Add("鹹寧也是湖北的");dataBuffer.Add("這是核桃");dataBuffer.Add("通城屬於鹹寧的");dataBuffer.Add("今天是星期幾?");//先隱藏!listBox1.Hide();}//檢索出值bool isFjty = false;void TextBox2TextChanged(object sender, EventArgs e){TextBox tb = sender as TextBox;if(isFjty)return;String outStr = tb.Text;if(outStr==""){listBox1.Hide();return;}elselistBox1.Show();listBox1.Items.Clear();foreach(String db in dataBuffer){/*方案1:包含提示  if(db.IndexOf(outStr)!=-1){listBox1.Items.Add(db);}*//*方案2:從前向後匹配if(db.StartsWith(outStr)){listBox1.Items.Add(db);}*/}int count = listBox1.Items.Count;if(count > 0)listBox1.Height = (count+1)*13;elseTextBox2Leave(null,null);}Keys keys;void ListBox1KeyDown(object sender, KeyEventArgs e){keys = e.KeyCode;textBox1.AppendText("key="+keys);}void TextBox2KeyUp(object sender, KeyEventArgs e){Keys keys = e.KeyCode;textBox1.Text = keys.ToString();if(keys.ToString()=="Down" && listBox1.Items.Count > 0){isFjty = true;int x = listBox1.SelectedIndex;x = x==-1?0:x+1;if(x > listBox1.Items.Count-1)x = 0;listBox1.SelectedIndex = x;textBox2.Text = listBox1.Items[x].ToString();textBox2.SelectionStart = textBox2.Text.Length+1;}else if(keys.ToString()=="Up" && listBox1.Items.Count > 0){isFjty = true;int x = listBox1.SelectedIndex;int len = listBox1.Items.Count-1;x = x==-1?len:x-1;if(x < 0)x = len;listBox1.SelectedIndex = x;textBox2.Text = listBox1.Items[x].ToString();textBox2.SelectionStart = textBox2.Text.Length+1;}else{isFjty = false;}}/*按下鍵時,如果不是上下移動,則立即偵聽輸入的字元*/void TextBox2KeyDown(object sender, KeyEventArgs e){if(keys.ToString()!="Down" || keys.ToString()!="Up"){isFjty = false;}}/*滑鼠手動選擇*/void ListBox1Click(object sender, EventArgs e){textBox2.Text = listBox1.Items[listBox1.SelectedIndex].ToString();textBox2.SelectionStart = textBox2.Text.Length+1;textBox2.Focus();//手動選擇後,是否應該隱藏掉提示列表呢?//TextBox2Leave(null,null);}//關鍵輸入框失去焦點後就【隱藏提示列表】。void TextBox2Leave(object sender, EventArgs e){listBox1.Items.Clear();listBox1.Height = 0;listBox1.Hide();}}}

4:樣本圖

|--從前向後匹配



 

|--模糊比對



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.