读书人

怎么完成类似googlebaidu搜索栏那样

发布时间: 2012-02-14 19:19:19 作者: rapoo

如何完成类似google,baidu搜索栏那样输入一个字,就能显示之前输入过的字呢?
如何完成类似google,baidu搜索栏那样输入一个字,就能显示之前输入过的字呢?
比如,之前输入过“中国”,下次我再输入“中”,就弹出个列表里面有 “中国”。
我想再winform中实现这个功能,不知道那位能详细介绍一下是如何完成的,最好有代码或者例子 。
呵呵 先 谢过 了 !!!

[解决办法]
高级智能匹配下拉菜单控件:
http://faq.n90.cn/html/Net_jishu/zujian_kongjiankaifa/19990722/20452.html

[解决办法]
在VS2005中这个问题变得很容易实现了,比如下面的代码设置了两个控件其中的某一种方式:

TextBox控件:

this.textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
this.textBox1.AutoCompleteSource = AutoCompleteSource.RecentlyUsedList;

ComboBox控件:
this.comboBox1.AutoCompleteMode = AutoCompleteMode.Append;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.FileSystemDirectories;


[解决办法]
WEB它是保存在Cookies中,WinForm你可以保存到XML或是文本中,你用第三方控件UltraCommbox之类的应该可以达到这种效果
[解决办法]
如果在2003下的话,只能自己使用ComboBox来完成。
它要列出的列表,你需要使用文件或注册表等来保存起来。
写ComboBox的TextChanged事件来实时例出下拉列表。
你可以参考一下:http://blog.csdn.net/cocosoft/archive/2004/12/21/224278.aspx

如果在2005下的话,按上面的方法来做即可。
[解决办法]
using System;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace CaseManager
{
/// <summary>
/// Summary description for ColumnComboBox.
/// </summary>
public class ColumnComboBox : ComboBox
{
private StringList m_slSuggestions = new StringList(); //A handy class used with the suggestion features.
private Keys m_kcLastKey = Keys.Space; //Last key pressed.
private int[] m_iaColWidths = new int[1];//Used for quick access to the column widths.
public uint ColumnSpacing = 4; //Minimum spacing between columns. Don 't go crazy with this...
private CCBColumnCollection m_Cols = new CCBColumnCollection();//A class used for managing the columns.

private DataTable m_dtData = null; //Main DataTable and DataView that contain the information to be shown in the ColumnComboBox.
private DataView m_dvView = null;

//private bool m_bShowHeadings = true; //Was going to do something with this but ran out of time.
private int m_iViewColumn = 0;//Which of the columns will be shown in the text box.
private bool m_bInitItems = true; //Flags used to determine when the things need to be initialized.
private bool m_bInitDisplay = true;
private bool m_bInitSuggestionList = true;

private bool m_bTextChangedInternal = false;//Used when the text is being changed by another member of the class.
public bool DropDownOnSuggestion = true;
public bool Suggest = true; //Suggesting can be turned on or off. No need for the whole property write out.
private int m_iSelectedIndex = -1; //Used for storing the selected index without depending on the base.

private System.ComponentModel.Container components = null;

public ColumnComboBox()
{
if(components == null)
components = null;
Data = new DataTable(); //Make sure the DataTable is not blank
Init();

}
public ColumnComboBox(DataTable dtData)
{
Data = dtData;
Init();
}
private void Init()
{
this.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
}



#region Component Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// ColumnComboBox
//

}

#endregion

protected override void OnKeyDown(KeyEventArgs e)
{
try
{
if(m_bInitSuggestionList)
InitSuggestionList();
base.OnKeyDown (e);
m_kcLastKey = e.KeyCode;
}
catch(Exception ex)
{
throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnKeyDown(KeyEventArgs). ");
}
}

protected override void OnTextChanged(EventArgs e) //Doesn 't call the base so no wiring up this event for you.
{
try
{
//Run a few checks to make sure there should be any "suggesting " going on.
if(m_bTextChangedInternal)//If the text is being changed by another member of this class do nothing
{
m_bTextChangedInternal = false; //It will only be getting changed once internally, next time do something.
return;
}
if(!Suggest)
return;
if(SelectionStart < this.Text.Length)
return;
int iOffset = 0;
if((m_kcLastKey == Keys.Back) || (m_kcLastKey == Keys.Delete))//Obviously we aren 't going to find anything when they push Backspace or Delete
{
UpdateIndex();
return;
}
if(m_slSuggestions == null || this.Text.Length < 1)
return;

//Put the current text into temp storage
string sText;
sText = this.Text;
string sOriginal = sText;
sText = sText.ToUpper();
int iLength = sText.Length;
string sFound = null;
int index = 0;
//see if what is currently in the text box matches anything in the string list
for(index = 0; index < m_slSuggestions.Count; index++)
{
string sTemp = m_slSuggestions[index].ToUpper();
if(sTemp.Length > = sText.Length)
{
if(sTemp.IndexOf(sText, 0, sText.Length) > -1)
{
sFound = m_slSuggestions[index];
break;
}
}
}
if(sFound != null)
{
m_bTextChangedInternal = true;
if(DropDownOnSuggestion && !DroppedDown )
{
m_bTextChangedInternal = true;
string sTempText = Text;
this.DroppedDown = true;
Text = sTempText;
m_bTextChangedInternal = false;
}
if(this.Text != sFound)
{
this.Text += sFound.Substring(iLength);
this.SelectionStart = iLength + iOffset;
this.SelectionLength = this.Text.Length - iLength + iOffset;
m_iSelectedIndex = index;
SelectedIndex = index;
base.OnSelectedIndexChanged(new EventArgs());
}
else
{
UpdateIndex();
this.SelectionStart = iLength;
this.SelectionLength = 0;
}
}
else
{
m_bTextChangedInternal = true;
m_iSelectedIndex = -1;
SelectedIndex = -1;
Text = sOriginal;
m_bTextChangedInternal = false;
base.OnSelectedIndexChanged(new EventArgs());
this.SelectionStart = sOriginal.Length;
this.SelectionLength = 0;
}
}
catch(Exception ex)
{
throw new Exception(ex.Message + "\r\nIn ColumnComboBox.OnTextChanged(EventArgs). ");
}
}



读书人网 >C#

热点推荐