码迷,mamicode.com
首页 > 其他好文 > 详细

c#模仿百度关键词提示

时间:2014-07-30 20:58:34      阅读:231      评论:0      收藏:0      [点我收藏+]

标签: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;
			}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:示例图

|--从前向后匹配

bubuko.com,布布扣


 

|--模糊匹配

bubuko.com,布布扣


c#模仿百度关键词提示,布布扣,bubuko.com

c#模仿百度关键词提示

标签:c#   关键字提示   

原文地址:http://blog.csdn.net/hubiao_0618/article/details/38303427

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!