比如listbox中行显示不全的时候,鼠标移动到记录上面
然后再记录的位置上显示tooltip。
我现在遇到的问题是,显示tooltip之后无法让它消失
如果在listbox的mouseleave中消失,tooltip就不停的闪烁请问如果解决这个问题
希望能有朋友给点代码,谢谢了

解决方案 »

  1.   

    tooltip不要放在鼠标下面。就是鼠标要实时响应listbox的mousemove事件
    这样处理鼠标事件就容易多了
      

  2.   

    参考一下这个:
    http://topic.csdn.net/t/20061130/10/5196217.html
      

  3.   


    Public Class Form1    Dim listbox1 As New ListBox
        Dim ToolTip1 As New ToolTip    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If Me.listbox1.SelectedItem IsNot Nothing Then ToolTip1.SetToolTip(Me.listbox1, Me.listbox1.SelectedItem.ToString)
        End Sub    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            listbox1.Items.Add("123456789abcdefghijklmnopqrstuvwxyz")
            listbox1.Items.Add("testtesttesttesttesttesttesttest-001")
            AddHandler listbox1.MouseMove, AddressOf Me.ListBox1_MouseMove
            Me.Controls.Add(listbox1)
        End SubEnd Class
      

  4.   

    谢谢各位的回答
    虽然和我问的内容不符。我的问题是,显示tooltip正常,但是无法正常的让tooltip消失
    我在mouseleave中使tooltip消失,但是却不停的闪烁。
    请问如何解决,谢谢
      

  5.   


    参考一下,不用mouseleave隐藏 tooltip.
            private void Form1_Load(object sender, EventArgs e)
            {            TreeNode rootNode = treeView1.Nodes.Add("Day of Week");            for (int count = 0; count <= 6; count++)
                {
                    DayOfWeek day = (DayOfWeek)count;
                    TreeNode childNode = rootNode.Nodes.Add(day.ToString());
                    childNode.Tag = "This day is " + day.ToString() + ".";
                }
                rootNode.ExpandAll();
            }        private void treeView1_MouseMove(object sender, MouseEventArgs e)
            {            TreeNode theNode = this.treeView1.GetNodeAt(e.X, e.Y);            if ((theNode != null))
                {
                    if (theNode.Tag != null)
                    {
                        if (theNode.Tag.ToString() != this.toolTip1.GetToolTip(this.treeView1))
                        {
                            this.toolTip1.SetToolTip(this.treeView1, theNode.Tag.ToString());
                        }
                    }
                    else
                    {
                        this.toolTip1.SetToolTip(this.treeView1, "");
                    }
                }
                else     
                {
                    this.toolTip1.SetToolTip(this.treeView1, "");
                }
            }
      

  6.   

    没明白lz意思tooltip 本身就会自己消失的……如果需要控制时间 有相关属性 AutoPopDelay ReshowDelay ==
      

  7.   

    参考
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Runtime.InteropServices;namespace WindowsApplication35
    {
        public partial class Form1 : Form
        {
            public class ToolTipListBox : System.Windows.Forms.ListBox
            {
                [StructLayout(LayoutKind.Sequential)]
                public struct SIZE
                {
                    public int cx;
                    public int cy;
                }
                [DllImport("gdi32.dll")]
                public static extern int GetTextExtentPoint32(IntPtr hdc,
                    String str, int len, ref SIZE size);            [DllImport("user32.dll")]
                public static extern IntPtr GetDC(IntPtr hWnd);            [DllImport("user32.dll")]
                public static extern int ReleaseDC(IntPtr hWnd, IntPtr hdc);
                public ToolTipListBox()
                {
                    tp.InitialDelay = 500;
                    tp.ReshowDelay = 500;
                    tp.AutoPopDelay = 3000;
                    tp.Active = true;
                }
                protected override void OnMouseMove(
                    System.Windows.Forms.MouseEventArgs e)
                {
                    /* Get the index of the mouse-hovered item */
                    int index = IndexFromPoint(e.X, e.Y);                /* Ensure that there is an item */
                    if (index != ListBox.NoMatches)
                    {
                        /* 
                          Check if the mouse has moved enough
                          distance for a new index 
                        */
                        if (LastIndex != index)
                        {
                            string s = Items[index].ToString();                        /* Get the text extent */
                            IntPtr hdc = GetDC(this.Handle);
                            SIZE size;
                            size.cx = 0;
                            size.cy = 0;
                            GetTextExtentPoint32(hdc, s, s.Length, ref size);
                            ReleaseDC(this.Handle, hdc);                        /* If it won't fit show tool-tip */
                            if (this.Width < size.cx)
                                tp.SetToolTip(this, s);                        LastIndex = index;
                        }
                    }
                }            private ToolTip tp = new ToolTip();
                private int LastIndex = -1;        }        public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                ToolTipListBox list = new ToolTipListBox();
                list.Width = 100;
                list.Height = 100;
                this.Controls.Add(list);
                list.Items.Add("jinjazz jinjazz");
                list.Items.Add("jinjazz jinjazz  jinjazz jinjazz");
            }
        }
    }
      

  8.   

    原文
    http://www.codeproject.com/KB/combobox/nishtooltiplistbox.aspx
      

  9.   

    谢谢楼上几位朋友
    可是还是tooltip还是无法正常消失(离开tooltip范围立刻消失,鼠标停留一段时间自动消失)
      

  10.   

    我看了一些代码,tooltip都不是在记录的位置上显示,而是在其他位置显示在其他位置显示tooltip是没有问题的,tooltip的自动功能都可用。
    但是如果tooltip显示在当前鼠标移动到的记录上时,鼠标移开tooltip无法正常消失
    以下是我的代码,tooltip不停的闪烁
    而是在其他位置显示        private void listBox1_MouseMove(object sender, MouseEventArgs e)
            {
                pt = e.Location;
                for (int i = 0; i < listBox1.Items.Count; ++i)
                { 
                    Rectangle rc = listBox1.GetItemRectangle(i);
                    if (rc.Contains(e.Location) == true)
                    {
                        toolTip1.Show(listBox1.GetItemText(listBox1.Items[i]), listBox1, rc.Location);
                        return;
                    }
                    else 
                    {
                        toolTip1.SetToolTip(listBox1, "");
                    }
                }
            }
      

  11.   

    无法正常的让tooltip消失 的原因是当ToolTip消息的时候又会使控件发生MouseMove事件而重新显示。所以看起来是一直显示着。其实你要做的好,就需要继承控件,然后按我给的帖子修改一下代码。
      

  12.   

    楼上的4星
    如果是你说的那样,那么当鼠标移到控件外的时候,tooltip就应该消失了。
    如果不是把tooltip放在记录上面,tooltip还是可以自动消失的
      

  13.   

    这个应该可以解决LZ的问题了吧?Public Class Form1    Dim listbox1 As New ListBox
        Dim WithEvents ToolTip1 As New ToolTip    Private Sub ListBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            Dim r As Rectangle = New Rectangle(e.X, e.Y, 0, 0)
            For i As Integer = 0 To Me.listbox1.Items.Count - 1
                If Me.listbox1.GetItemRectangle(i).IntersectsWith(r) Then
                    ToolTip1.Show(Me.listbox1.Items(i), Me.listbox1, New Point(e.X + 15, e.Y + 15), 1000)
                    ToolTip1.Active = True
                    Exit Sub
                End If
            Next
            ToolTip1.Active = False
        End Sub    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            listbox1.Items.Add("123456789abcdefghijklmnopqrstuvwxyz")
            listbox1.Items.Add("testtesttesttesttesttesttesttest-001")
            AddHandler listbox1.MouseMove, AddressOf Me.ListBox1_MouseMove
            Me.Controls.Add(listbox1)
        End SubEnd Class
      

  14.   

    感谢楼上,方法可以
    但是为什么鼠标要慢慢的移出listbox控件才好用呢
    要是鼠标移得快了,tooltip还是不消失
      

  15.   

    重金诚聘C++
    http://topic.csdn.net/u/20080227/14/8bce0844-bd15-42f0-9cda-a343d5d6601b.html?seed=2111206245