private void BtnOK_Click(object sender, EventArgs e)
{
string[] arr = new string[4];
ListViewItem itm;
for (int i = 0; i < 1; i++)
{
arr[0] = "Ready";
arr[1] = "";
arr[2] = Properties.Settings.Default.AddNewDownloadsUrl;
arr[3] = Properties.Settings.Default.AddNewDownloadsSaveto;
itm = new ListViewItem(arr);
listView1.Items.Add(itm);
links.Add(Properties.Settings.Default.AddNewDownloadsUrl);
ListViewExtended.ListViewHelper.colorReady(listView1, Color.Red);
}
}
我希望当用户将鼠标光标移动到其中一个arr上时,它会在其上显示一个文本。例如,在arr [3]上,当用户将鼠标光标移动到listView上时,显示Properties.Settings.Default.AddNewDownloadsSaveto文本。 AddNewDownloadsSaveto是一个字符串。
答案 0 :(得分:0)
这对我来说非常合适
创建了两个事件:listView1_MouseMove和Form1_MouseMove
ToolTip mTooltip;
Point mLastPos = new Point(-1, -1);
ListViewHitTestInfo info;
ListViewItem.ListViewSubItem lastSubItem;
private void listView1_MouseMove(object sender, MouseEventArgs e)
{
info = listView1.HitTest(e.X, e.Y);
if (mTooltip == null)
mTooltip = new ToolTip();
if (mLastPos != e.Location)
{
if (info.Item != null && info.SubItem != null)
{
if (info.SubItem != lastSubItem)
{
mTooltip.ToolTipTitle = info.Item.Text;
mTooltip.Show(info.SubItem.Text, info.Item.ListView, e.X, e.Y, 20000);
lastSubItem = info.SubItem;
}
}
else
{
mTooltip.SetToolTip(listView1, string.Empty);
}
}
mLastPos = e.Location;
}
Form1中:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (listView1 != null)
{
if (!listView1.Bounds.Contains(e.X, e.Y) && mTooltip != null)
{
mTooltip.SetToolTip(listView1, "");
lastSubItem = null;
}
}
}
答案 1 :(得分:0)
这可以使用ListView
的{{1}}方法来实现。该方法使用当前鼠标位置检查指针是否超过(子)项。要将其与HitTest
一起使用,您可以执行以下操作(在我的测试表单上,我ToolTip
名为ListView
,listView1
名为ToolTip
):< / p>
toolTip1
的{{1}}和ListView
个活动MouseMove
检查指针是否超过MouseLeave
。如果是,请参阅subitem的文字并显示MouseMove
。SubItem
时,ToolTip
参见测试代码:
ToolTip
不幸的是,由于用户移动鼠标时不断重绘,ListView
会闪烁。要(尝试)避免这种情况,您可以对指针当前所在的private void Form1_Load(object sender, EventArgs e)
{
//subscribe to events - when mouse leaves listview and when mouse is moved over listview
listView1.MouseLeave += ListView1_MouseLeave;
listView1.MouseMove += ListView1_MouseMove;
}
private void ListView1_MouseMove(object sender, MouseEventArgs e)
{
//using current location, check what is under mouse
var info = listView1.HitTest(e.Location);
//if any subitem is selected
if (info.SubItem != null)
{
//set tooltip's new location to the bottom right of mouse pointer
Point calculatedLocation = new Point(e.X + 20, e.Y + 20);
//show tooltip - with subitem text, on this window, on calculated location and hold it there for 3 seconds.
toolTip1.Show(info.SubItem.Text, listView1, calculatedLocation, 3000);
}
}
private void ListView1_MouseLeave(object sender, EventArgs e)
{
//hide tooltip
this.toolTip1.Hide(this);
}
进行变量保持引用。当鼠标显示ToolTip
显示ListViewSubItem
时,但当指针移过它时,请勿重绘。当用户移动到另一个ListViewSubItem
时,再次显示/绘制ToolTip
。这可以这样实现:
ListViewSubItem
但是,如果像这样使用,ToolTip
将不会随鼠标指针移动。 :)
我希望我能告诉你如何解决你的问题。如果您还有其他问题,请随时提出。