Winform C# .Net中给ListBox加ToolTip提示
原创
©著作权归作者所有:来自51CTO博客作者山可可的原创作品,请联系作者获取转载授权,否则将追究法律责任
1、首先,在工具箱中将ToolTip控件拖入到Winform界面下方。
2、接着,在指定的ListBox添加:XXX_MouseMove事件(如已添加则忽略此步)。其中:XXX为您的控件名称。
3、在该事件中添加如此代码:
int index = listBoxPhotos.IndexFromPoint(e.Location);
if (index != -1 && index < listBoxPhotos.Items.Count)
{
if (toolTip1.GetToolTip(listBoxPhotos) != listBoxPhotos.Items[index].ToString())
{
toolTip1.SetToolTip(listBoxPhotos, listBoxPhotos.Items[index].ToString());
}
}
OK。
最终代码类似于:
private void listBoxPhotos_MouseMove(object sender, MouseEventArgs e)
{
int index = listBoxPhotos.IndexFromPoint(e.Location);
if (index != -1 && index < listBoxPhotos.Items.Count)
{
if (toolTip1.GetToolTip(listBoxPhotos) != listBoxPhotos.Items[index].ToString())
{
toolTip1.SetToolTip(listBoxPhotos, listBoxPhotos.Items[index].ToString());
}
}
}