Link to home
Start Free TrialLog in
Avatar of tinchos
tinchos

asked on

ListView Tooltip

Hi

I'm looking forward to placing a tooltip in a listview.

In order to do that, I'm hanging to the listview's MouseMove event

and my handler looks like

private void OnListViewMouseMove(object sender, MouseEventArgs e)
{
      ListViewItem lvitem = new  ListViewItem();
      lvitem = listView.GetItemAt(e.X,e.Y);

      if( lvitem != null )
            toolTip.SetToolTip( listView, "My tooltip" );
}

This shows a tooltip whose text is "My Tooltip".

Now what I want to do is to show a different text for each column.....

This would be if the mouse is over column 1, then the tooltip should be for instance "Over Column 1", if the mouse is over column 2, then the tooltip should be for instance "Over Column 2"

Any idea how to know which column I'm over (As you can see from the code, I can get the listViewItem I'm over, but don't know how to get the column I'm over)

Thanks

Tincho
Avatar of Brian Mulder
Brian Mulder
Flag of Netherlands image

there is a sample project here with a way to achieve something similar
http://www.codeproject.com/cs/miscctrl/CSharpHitTest.asp

but i'm just listening in on this q needing the same ;)
Avatar of esteban_felipe
esteban_felipe

You may use the data of the Item that raised the event. If you wanted to use the index in the tooltip:

tooTip.SetToolTip(listView,listView.Index.ToString());
Avatar of tinchos

ASKER

bruintje

Give me some time to check it out....



esteban_felipe

What I want to know is over which column I'm over
This might help.  Stripped it from other implementation --> shows dropdown box when user clicks on a cell.  Same case though...  Try it out + your code

    Private col As Integer
    Private row As Integer

    Private Const colH As Byte = 85
    Private Const colHH As Byte = 170

    Private Sub lvwSQLConditions_MouseHover(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lvwSQLConditions.MouseHover

        row = e.Y - lvwSQLConditions.Items(0).GetBounds(ItemBoundsPortion.Label).Top
        row \= lvwSQLConditions.Items(0).GetBounds(ItemBoundsPortion.Label).Height

        col = e.X - lvwSQLConditions.Items(0).GetBounds(ItemBoundsPortion.Label).Left
        col \= colH

        'hideShowCombo(False)

        Select Case col
            Case 0, 1
                col = 0
                'show tooltip
            Case 2
                col = 1
                'show tooltip
            Case 3, 4
                col = 2
             Case 5
                col = 3

        End Select

        lvwSQLConditions.Items(row).Selected = False

        If col = 6 Then Exit Sub

        Try
            If row <> 0 Then
                'check if we are allowed here
                'If lvwSQLConditions.Items(row - 1).SubItems(3).Text = "" Then
                    'hideShowCombo(False)
                    'Exit Sub
                'End If
            End If
        Catch ex As ArgumentOutOfRangeException
            'hideShowCombo(False)
        End Try
    End Sub

hec",)
Avatar of tinchos

ASKER

Sorry harris

could you please give me a hand in understanding it?

Tincho
'This is the complete implementation.
'TODO:  Put a timer on top so there is a delay on the tooltip display.  Reason for putting the row/column variables outside the procedure.



Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    Friend WithEvents ColumnHeader1 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader2 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader3 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ColumnHeader4 As System.Windows.Forms.ColumnHeader
    Friend WithEvents ToolTip1 As System.Windows.Forms.ToolTip
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Dim ListViewItem1 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col1Row1", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col2Row1"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col3Row1"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col4Row1")}, -1)
        Dim ListViewItem2 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col1Row2", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col2Row2"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col3Row2"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col4Row2")}, -1)
        Dim ListViewItem3 As System.Windows.Forms.ListViewItem = New System.Windows.Forms.ListViewItem(New System.Windows.Forms.ListViewItem.ListViewSubItem() {New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col1Row3", System.Drawing.SystemColors.WindowText, System.Drawing.SystemColors.Window, New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col2Row3"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col3Row3"), New System.Windows.Forms.ListViewItem.ListViewSubItem(Nothing, "Col4Row3")}, -1)
        Me.ListView1 = New System.Windows.Forms.ListView()
        Me.ColumnHeader1 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader2 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader3 = New System.Windows.Forms.ColumnHeader()
        Me.ColumnHeader4 = New System.Windows.Forms.ColumnHeader()
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.SuspendLayout()
        '
        'ListView1
        '
        Me.ListView1.Columns.AddRange(New System.Windows.Forms.ColumnHeader() {Me.ColumnHeader1, Me.ColumnHeader2, Me.ColumnHeader3, Me.ColumnHeader4})
        Me.ListView1.GridLines = True
        Me.ListView1.Items.AddRange(New System.Windows.Forms.ListViewItem() {ListViewItem1, ListViewItem2, ListViewItem3})
        Me.ListView1.Location = New System.Drawing.Point(40, 24)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(312, 120)
        Me.ListView1.TabIndex = 1
        Me.ListView1.View = System.Windows.Forms.View.Details
        '
        'ColumnHeader1
        '
        Me.ColumnHeader1.Text = "Col1"
        Me.ColumnHeader1.Width = 68
        '
        'ColumnHeader2
        '
        Me.ColumnHeader2.Text = "Col2"
        Me.ColumnHeader2.Width = 74
        '
        'ColumnHeader3
        '
        Me.ColumnHeader3.Text = "Col3"
        Me.ColumnHeader3.Width = 74
        '
        'ColumnHeader4
        '
        Me.ColumnHeader4.Text = "Col4"
        Me.ColumnHeader4.Width = 78
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(400, 261)
        Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.ListView1})
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private col As Integer
    Private row As Integer

    'this is the default height of the cell
    Private Const colH As Byte = 85

    Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove
        row = e.Y - ListView1.Items(0).GetBounds(ItemBoundsPortion.Label).Top
        row \= ListView1.Items(0).GetBounds(ItemBoundsPortion.Label).Height

        col = e.X - ListView1.Items(0).GetBounds(ItemBoundsPortion.Label).Left
        col \= colH

        'hideShowCombo(False)

        ToolTip1.SetToolTip(ListView1, "You are in COLUMN: " & col + 1 & ", ROW: " & row + 1)
    End Sub

    Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub
End Class




hec",)
Avatar of tinchos

ASKER

Sorry harris

could you please give me a hand in understanding it?
especially the ListView1_MouseMove method

Tincho
ASKER CERTIFIED SOLUTION
Avatar of harris_c
harris_c
Flag of Philippines image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tinchos

ASKER

Ok hec

Just a question

When determining the column in which the cursor is positioned, you'll do

        //determine *true* column regardless of column widths
        //add up all column widths until its greater than the X location of cursor
        for (col = 0; col <= colCnt - 1; col++)
          {
            tWidth = tWidth + listView1.Columns[col].Width;
            if (tWidth > e.X)
               {
                outCol = false;
                    break;
               }
          }

But..... if I have defined the width of the column as -1 (autoadjust to the largest value in that column) or -2 (autoadjust to the header width), will it work too?

Tincho
Hi Tincho,

It does.  Since the calculation of the width is taking into account the current width of the listView.

You can verify this by putting a command button into the form and adding the ff code:



            private void button1_Click(object sender, System.EventArgs e)
            {
                  for (int ctr = 0; ctr < listView1.Columns.Count ; ctr++)
                  {
                        listView1.Columns[ctr].Width += 10;
                  }
            }





hec",)
Avatar of tinchos

ASKER

Ok, let me check it and I'll let you know

Tincho
Avatar of tinchos

ASKER

Harris, I have not forgotten about this one

I'm with a lot of trouble and I just couldn't try it.......

give me a few days and I'll be closing it

Thanks

Tincho
Avatar of tinchos

ASKER

Sorry for the delay Harris, but I've been really busy and with a lot of trouble at work

Luckily, I've managed to test it today.

Thanks

Tincho
Thats alright tinchos.

Thanks too.

hec",)