我已经搜索了各种解决方案,但没有一个给我直接的答案,或者不是用 vb.net 写的。但我的情况是,我有一个ComboBox
,其中包含用户可以选择的一些项目。我想添加简单的工具提示,以便每个用户都知道他或她在选择什么。但是,在选择某个项目之前,不会显示工具提示。我希望当鼠标悬停在每个项目上时显示工具提示。
下面是我的代码:
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 事件以组合框控件
组合框的绘制模式属性设置为所有者绘制固定
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));
Heena,您发布的代码完美无缺! 谢谢。
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);
}
}
}