[英]Add tooltip for combobox items
我正在使用onDraw函数为组合框项目添加工具提示,并且工作正常。 但是我的组合框是拆分器形式。 因此,当您移动拆分器时,这可以强制执行OnDrawItem事件。 发生这种情况时,它会重新提示并显示最后一个工具提示。 如何在拆分器表单上处理这种类型的combox?
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Data;
namespace MyForm
{
public class MyComboBox : ComboBox
{
private ToolTip toolTip;
private String toolTipMember;
private List<String> toolTipString;
private DataTable myDataSource;
public MyComboBox()
: base()
{
toolTipString = new List<String>();
}
public String ToolTipMember
{
set
{
this.toolTipMember = value;
if (myDataSource != null)
initTootTip(this.toolTipMember);
}
}
public DataTable MyDataSource
{
set
{
myDataSource = value;
this.DataSource = myDataSource;
}
}
private void initTootTip(string toolTipMember)
{
toolTip = new ToolTip();
if ((this.DataSource != null) && (toolTipMember != null))
{
foreach (DataRow row in myDataSource.Rows)
{
toolTipString.Add(row[toolTipMember].ToString());
}
}
}
protected override void OnDataSourceChanged(EventArgs e)
{
base.OnDataSourceChanged(e);
if (myDataSource != null)
initTootTip(toolTipMember);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
string text = this.GetItemText(Items[e.Index]);
base.OnDrawItem(e);
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{
e.Graphics.DrawString(text, e.Font, br, e.Bounds);
}
int index = e.Index;
//Console.WriteLine("state : " + e.State.ToString());
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
try
{
text = toolTipString[index];
this.toolTip.Show(text,
this, e.Bounds.Right, e.Bounds.Bottom);
//Console.WriteLine("show : " + text);
}
catch (Exception)
{
}
}
e.DrawFocusRectangle();
}
protected override void OnDropDownClosed(EventArgs e)
{
base.OnDropDownClosed(e);
toolTip.Hide(this);
}
}
}
**UsethisclassandaddMyComboBoxComponent<br>
withproperties**
MyComboBoxcomboBox1
DataTabletable=newDataTable()
table.Columns.Add("value_member",typeof(int))
table.Columns.Add("display_member",typeof(string))
table.Columns.Add("tooltip_member",typeof(string))
comboBox1.MyDataSource=table
comboBox1.DisplayMember="display_member"
comboBox1.ValueMember="value_member"
comboBox1.ToolTipMember="tooltip_member"
comboBox1.DrawMode=DrawMode.OwnerDrawFixed
comboBox1.SelectedIndex=-1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.