日期:2009-06-16  浏览次数:20389 次

根据你输入的内容自动过滤出匹配的列表项,并实时显示出来,和IE地址栏类似。
程序很简单,只是很长事时间没更新blog了,用这篇小文章来更新一下。


程序原理是这样的:组件继承之combobox,并且拥有一个listbox成员,当组件的text改变时,就从combobox中过滤出匹配的列表项,并且把这些列表项加入到listbox中显示出来。
演示效果:
using System;

using System.Collections;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Windows.Forms;



namespace XPangLib

{

/// <summary>

/// AutoComboBox 的摘要说明。

/// </summary>

public class AutoComboBox : System.Windows.Forms.ComboBox

{



private System.Windows.Forms.ListBox lbMain;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private System.ComponentModel.Container components = null;



public AutoComboBox()

{

// 该调用是 Windows.Forms 窗体设计器所必需的。

InitializeComponent();



lbMain = new ListBox() ;

lbMain.Click+=new EventHandler(lbMain_Click);

lbMain.KeyDown+=new KeyEventHandler(lbMain_KeyDown);

lbMain.Visible = false ;

}

/// <summary>

/// 得到匹配给定字符串的列表

/// </summary>

private ArrayList GetFillList(string strValue)

{

ArrayList alReturn = new ArrayList() ;

int iCount = this.Items.Count ;

int iLen = strValue.Length ;



for(int i = 0 ;i < iCount ;i++)

{

string strSelItem = this.Items[i].ToString() ;



if (strSelItem.Length < iLen)

continue ;



if (strSelItem.Substring(0,iLen) == strValue)

alReturn.Add(strSelItem) ;

}



return alReturn ;

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if( components != null )

components.Dispose();

}

base.Dispose( disposing );

}



#region 组件设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器

/// 修改此方法的内容。

/// </summary>

private void InitializeComponent()

{//

// AutoComboBox

//

this.Sorted = true;

this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.AutoComboBox_KeyDown);

this.DropDown += new System.EventHandler(this.AutoComboBox_DropDown);

this.TextChanged += new System.EventHandler(this.AutoComboBox_TextChanged);

this.SelectedIndexChanged += new System.EventHandler(this.AutoComboBox_SelectedIndexChanged);

this.Leave += new System.EventHandler(this.AutoComboBox_Leave);



}

#endregion