您的位置:首页 > 编程语言 > Go语言

在Winfrom下实现类似百度、Google搜索自能提示功能

2010-09-01 18:11 846 查看
前记:数据源来自页面的一个ComboBox的数据源List<Contract>集合

页面放置一个TextBox(搜索框)、ListBox(显示搜索出来的数据),ListBox位置位于TextBox正下方,初始化隐藏。

TextBox--->txtSearch ListBox--->listBox1关于两个控件的事件,详见代码:

#region  自能提示
Point pList = new Point();

private void txtSearch_TextChanged(object sender, EventArgs e)
{
if (this.txtSearch.Text.Trim() != "")
{
List<Contract> source = getDataTable(this.txtSearch.Text.Trim());
BindList(source);
this.listBox1.Visible = true;
}
else
{
this.listBox1.Items.Clear();
this.listBox1.Visible = false;
}
}

private void txtSearch_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Down && this.listBox1.Visible)
{
this.listBox1.Focus();

if (this.listBox1.SelectedItems.Count > 0)
{
this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
}
if (this.listBox1.Items.Count > 0)
{
this.listBox1.SetSelected(0, true);

}
}
}

private void listBox1_MouseUp(object sender, MouseEventArgs e)
{
if (this.listBox1.SelectedItems.Count > 0)
{
this.txtSearch.Text = this.listBox1.Text;
cboChoCont.Text = this.txtSearch.Text;
this.listBox1.Visible = false;
this.txtSearch.Focus();
}
}

private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
Point m = new Point(e.X, e.Y);
int index = GetItemAt(this.listBox1, e.X, e.Y);
if (this.listBox1.SelectedItems.Count > 0 && this.listBox1.SelectedIndex != index)
{
this.listBox1.SetSelected(this.listBox1.SelectedIndex, false);
}

if (index != -1 && this.listBox1.SelectedIndex != index)
{

this.listBox1.SetSelected(index, true);
}
}

private void listBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter && this.listBox1.Visible && this.listBox1.SelectedItems.Count > 0)
{
this.txtSearch.Text = this.listBox1.SelectedItems[0].ToString();
this.cboChoCont.Text = this.txtSearch.Text;
this.listBox1.Visible = false;
this.txtSearch.Focus();
}
}

private bool GetItemAt(Point Mousep, int index)
{
int ph = this.listBox1.GetItemHeight(index) * index;
int ph1 = this.listBox1.GetItemHeight(index) * index + this.listBox1.GetItemHeight(index);

if (Mousep.Y > ph && Mousep.Y < ph1 && Mousep.X > 0 && Mousep.X < this.listBox1.Width)
{
return true;
}
else
{
return false;
}

}
private int GetItemAt(ListBox listBox, int X, int Y)
{
int index = -1;
for (int i = 0; i < listBox.Items.Count; i++)
{
System.Drawing.Rectangle r = listBox.GetItemRectangle(i);
if (r.Contains(new Point(X, Y)))
{
index = i; ;
break;
}
}
return index;
}

private void BindList(List<Contract> source)
{
this.listBox1.Items.Clear();
for (int i = 0; i < source.Count; i++)
{
this.listBox1.Items.Add(source[i].Con_Name_Serial);
}
if (source.Count < 11)
{
this.listBox1.Height = 15 * source.Count + 15;
}
else
{
this.listBox1.Height = 150;
}
this.listBox1.Visible = true;
}

private List<Contract> getDataTable(string s)
{
List<Contract> searchList = new List<Contract>();
int length = list.Count;
for (int i = 0; i < length; i++)
{
if (list[i].Con_Name_Serial.IndexOf(s) == 0)
searchList.Add(list[i]);
}
return searchList;
}

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
// Set the DrawMode property to draw fixed sized items.
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
// Draw the background of the ListBox control for each item.
e.DrawBackground();
// Define the default color of the brush as black.
Brush myBrush = Brushes.Black;
FontFamily fontFamily = new FontFamily("宋体");
System.Drawing.Font myFont = new Font(fontFamily, 9);
// Determine the color of the brush to draw each item based on the index of the item to draw.
if((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
//e.Graphics.FillRectangle(Brushes.Blue, e.Bounds);
if(e.Index > -1)
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, Brushes.White, e.Bounds, StringFormat.GenericDefault);
}
}
else
{
//e.Graphics.FillRectangle(Brushes.White, e.Bounds);
if(e.Index > -1)
{
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), myFont, myBrush, e.Bounds, StringFormat.GenericDefault);
}
}
// Draw the current item text based on the current Font and the custom brush settings.
//e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);

// If the ListBox has focus, draw a focus rectangle around the selected item.
e.DrawFocusRectangle();
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: