我只想为组合框中的每个项添加工具提示.我正在使用c#.net windows应用程序.
没有选择
combobox.items [1] .tooltip();
有没有办法添加工具提示呢?
Mic*_*ens 31
这个问题实际上有几个合理的解决方案.MSDN论坛有一个ComboBox项目突出显示事件帖子,其中包含两种可能性,一种来自nobugz,另一种来自agrobler.它们中的每一个都提供了一个代码,用于子类化ComboBox,该ComboBox应该处理ComboBox下拉列表中各个项目的工具提示.Agrobler的解决方案看起来更加精致,因为他/她甚至包含了一些不错的插图,但不幸的是,至少对我来说,如何填充控件的关键ToolTipMember属性并不清楚.
这两种解决方案似乎都允许分配给各个项目的任意工具提示.更具体但更常见的情况是,当您知道可能有太长的项目无法适应ComboBox的宽度时,您只需要工具提示镜像项目的文本.在我自己的情况下,我有一个ComboBox的实例,它包含完整的文件路径,因此很容易看到内容可能超出ComboBox的宽度.
叶志新在MSDN论坛发布的Windows Dropdown问题中提供了解决这个更具体问题的解决方案,并且更加简单.我在这里完整地重现了代码.(请注意,此代码预先假定您已创建一个名为Form1的Form并连接显示的加载处理程序,并且还添加了一个名为comboBox1的ComboBox和一个工具提示处理程序toolTip1.)
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
this.comboBox1.DrawItem += new DrawItemEventHandler(comboBox1_DrawItem);
}
void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string text = this.comboBox1.GetItemText(comboBox1.Items[e.Index]);
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{ e.Graphics.DrawString(text, e.Font, br, e.Bounds); }
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{ this.toolTip1.Show(text, comboBox1, e.Bounds.Right, e.Bounds.Bottom); }
else { this.toolTip1.Hide(comboBox1); }
e.DrawFocusRectangle();
}
Run Code Online (Sandbox Code Playgroud)
虽然简单明了,这个代码就从一个故障影响(如指出了上述MSDN线程的答复):当你移动鼠标(不点击)从一个下拉列表项到下一个,只有每一个其他一个显示持久的工具提示!修复只是通过该线程上的另一个条目暗示,所以我认为在这里提供完整的,更正的代码是有用的:
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
comboBox1.DrawItem += comboBox1_DrawItem;
comboBox1.DropDownClosed += comboBox1_DropDownClosed;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
toolTip1.Hide(comboBox1);
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index < 0) { return; } // added this line thanks to Andrew's comment
string text = comboBox1.GetItemText(comboBox1.Items[e.Index]);
e.DrawBackground();
using (SolidBrush br = new SolidBrush(e.ForeColor))
{ e.Graphics.DrawString(text, e.Font, br, e.Bounds); }
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{ toolTip1.Show(text, comboBox1, e.Bounds.Right, e.Bounds.Bottom); }
e.DrawFocusRectangle();
}
Run Code Online (Sandbox Code Playgroud)
除了删除一些冗余的代码部分(例如"this"限定符)之外,主要区别在于将toolTip1.Hide调用移动到DropDownClosed事件处理程序中.从DrawItem处理程序中取出它可以消除上面提到的缺陷; 但是当你下拉关闭时你需要关闭它,否则最后显示的工具提示将保留在屏幕上.
2012.07.31附录
只是想提一下,我已经创建了一个包含这个工具提示功能的复合ComboBox,所以如果你使用我的库,你根本就没有代码可以编写.只需将ComboBoxWithTooltip拖到Visual Studio设计器上即可完成.在我的API页面上深入查看ComboBoxWithTooltip 或下载我的开源C#库以开始使用.(请注意,Andrew捕获的bug的补丁将在1.1.04版本中发布,即将发布.)
小智 7
private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
ToolTip toolTip1 = new ToolTip();
toolTip1.AutoPopDelay = 0;
toolTip1.InitialDelay = 0;
toolTip1.ReshowDelay = 0;
toolTip1.ShowAlways = true;
toolTip1.SetToolTip(this.comboBox1, comboBox1.Items[comboBox1.SelectedIndex].ToString()) ;
}
Run Code Online (Sandbox Code Playgroud)
您将需要创建自己的UserControl。
为组合框中的每个项目提供一个工具提示是一个不寻常的要求;也许你可以使用 2 列组合框来代替?
| 归档时间: |
|
| 查看次数: |
50677 次 |
| 最近记录: |