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;
}else
listBox1.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;
else
TextBox2Leave(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:示例圖
|--從前向後匹配
|--模糊匹配