Vb.net 如何为组合框中的每个项目添加工具提示
我已经搜索了各种解决方案,但没有一个能给我一个直接的答案,也没有一个不是用vb.net编写的。但我的情况是,我有一个Vb.net 如何为组合框中的每个项目添加工具提示,vb.net,combobox,tooltip,Vb.net,Combobox,Tooltip,我已经搜索了各种解决方案,但没有一个能给我一个直接的答案,也没有一个不是用vb.net编写的。但我的情况是,我有一个组合框,其中包含一些用户可以选择的项目。我想添加简单的工具提示,让每个用户都知道他或她在选择什么。但是,在选择某个项目之前,工具提示不会显示。我希望鼠标悬停在每个项目上时显示工具提示 下面是我的代码: Private Sub VotingAgentComboBox_MouseHover(ByVal sender As System.Object, ByVal e As System
组合框
,其中包含一些用户可以选择的项目。我想添加简单的工具提示,让每个用户都知道他或她在选择什么。但是,在选择某个项目之前,工具提示不会显示。我希望鼠标悬停在每个项目上时显示工具提示
下面是我的代码:
Private Sub VotingAgentComboBox_MouseHover(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VotingAgentComboBox.MouseHover
Dim VotingAgentToolTip As New ToolTip
If VotingAgentComboBox.Text = "ISS" Then VotingAgentToolTip.SetToolTip(VotingAgentComboBox, "You selected ISS")
End Sub
试试这个。。
将工具提示控件添加到窗体,并将此代码写入组合框控件的DrawItem事件
combobox的drawmode属性设置为OwnerDrawFixed
if (e.Index == -1) { return; }
Point p = new Point(ComboBox1.Location.X + 120, ComboBox1.Location.Y + ComboBox1.Height + (30 + e.Index * 10));
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
toolTip.Show(ComboBox1.Items[e.Index].ToString(), this, p);
}
e.DrawBackground();
e.Graphics.DrawString(ComboBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, new Point(e.Bounds.X, e.Bounds.Y));
希娜,你发布的代码完美无瑕!谢谢
private void CmbUnit_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index >= 0)
{
UnitItem item = Items[e.Index] as UnitItem;
TextRenderer.DrawText(e.Graphics, item.unit_str, e.Font, e.Bounds,
e.ForeColor, TextFormatFlags.HorizontalCenter);
e.DrawFocusRectangle();
Point p = new Point(Location.X + 120, Location.Y + Height + (30 + e.Index * 10));
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
ttip.Show(item.unit_name, this, 2000);
}
}
}
它只执行所选项目,因为您正在测试所选项目:
If VotingAgentComboBox.Text
。单个项目不会获得鼠标事件,因此如果不使用子类化或usinf WndProc,工具提示将很困难。或者在CBO中使用更多描述性文本。您可以将对象存储在Items
中,而不仅仅是字符串,因此您可以编写类来存储CBO中的任何内容,并重写ToString
,为项目列表提供更长的描述性文本。感谢您的输入,我现在理解了我的问题,但是我该如何处理子类呢?这将结束子类化和Owner Draw和WndProc,以获得ItemHighlight事件,您可以使用该事件显示工具提示。这是一个奇怪的控件-有点像一个有几个部分的用户控件。从这两个链接开始,更容易提供更好的描述,这可能会对您有所帮助!1.2.