Results 1 to 12 of 12

Thread: Tooltip show on the drop down menu in the combobox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2014
    Posts
    81

    Tooltip show on the drop down menu in the combobox

    Hi Guys, I would like to see the text highlighted in the dropdown menu of a combo box, inside a tooltip on mouse over.
    Using the classic method, I display the tooltip only the selected item after the closing of dropdown menu..
    Code:
    Private Sub ComboBox1_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.MouseHover
            Me.ToolTip_Info.SetToolTip(ComboBox1, ComboBox1.Text.ToString)
        End Sub
    What interests me is show in the tooltip, the text selected in the dropdown menu, in this way..

    Name:  CDrowMenu.png
Views: 5135
Size:  8.8 KB

    I hope in a help...Thanks.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Tooltip show on the drop down menu in the combobox

    Quote Originally Posted by mingolito View Post
    Hi Guys, I would like to see the text highlighted in the dropdown menu of a combo box, inside a tooltip on mouse over.
    Using the classic method, I display the tooltip only the selected item after the closing of dropdown menu..

    What interests me is show in the tooltip, the text selected in the dropdown menu, in this way..
    Here is how I did it for one of my apps, its a ComboBox+ToolTip-in-1 control.

    The right click version will raise an event with the index of the item you right mouse clicked in the dropdown, or something like that, its been awhile since I made these, but hopefully I commented the code enough so anyone can follow it.

    NOTE: Demo projects were made in VB10 and tested on Win7 only.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2014
    Posts
    81

    Re: Tooltip show on the drop down menu in the combobox

    Thanks for the examples, but I was looking for something more simple, you have even entered the use of a timer for the display time.
    I tried something like this developed to a listbox...
    Code:
    Private Sub ListBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListBox2.MouseMove
            Dim i As Integer = ListBox2.IndexFromPoint(e.X, e.Y)
            If i >= 0 Then ToolTip_Info.SetToolTip(ListBox2, ListBox2.Items.Item(i).ToString)
        End Sub
    But to be adjusted to a combobox, just do not work.
    Last edited by mingolito; Mar 15th, 2016 at 09:03 AM.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Tooltip show on the drop down menu in the combobox

    Quote Originally Posted by mingolito View Post
    Thanks for the examples, but I was looking for something more simple, you have even entered the use of a timer for the display time.
    There is no simple way to set a tooltip to a dropdown list of a ComboBox because the dropdown List is a window created by Windows, so the only way (I know) to do anything with that list is get its handle and then subclass it. If you find an easier/better way please share.

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Tooltip show on the drop down menu in the combobox

    This may be about as simple as it gets, the trick here is to use an owner drawn combo,..
    Code:
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Const LB_GETCURSEL As Int32 = &H188 ' message used to get item index the mouse is over in drop list.
    
        <DllImport("user32.dll")> _
        Private Shared Function WindowFromPoint(ByVal Point As Point) As IntPtr
        End Function
    
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
        End Function
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' SETUP TOOLTIP
            With ToolTip1
                .SetToolTip(ComboBox1, "TESTING 1,2,3")
                .IsBalloon = False
                .ShowAlways = True
            End With
    
            ' OWNER DRAWN
            With ComboBox1
                .DrawMode = DrawMode.OwnerDrawFixed
                .BackColor = Color.LightYellow
                .ForeColor = Color.Black
            End With
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
            Static lastindex As Integer = -1
            ' If nothing selected exit!
            If e.Index < 0 Then Return
            ' Draw the background of the ListBox control for each item.
            e.DrawBackground()
            ' Get combo that raised this event.
            Dim cb = DirectCast(sender, ComboBox)
            ' Draw item text using default colors
            Using brsh As New SolidBrush(e.ForeColor)
                e.Graphics.DrawString(cb.Items(e.Index).ToString, e.Font, brsh, e.Bounds)
            End Using
            '
            ' Get window handle under mouse
            Dim hWnd = WindowFromPoint(MousePosition)
            ' Get combo's listbox item index.
            Dim index As Int32 = SendMessage(hWnd, LB_GETCURSEL, 0, 0)
            If index > -1 AndAlso lastindex <> index Then ' only show once or will flicker, disapear, etc.
                lastindex = index
                ' Get mouse pos for setting tooltip location.
                Dim mousePos = cb.PointToClient(MousePosition)
                ' Offset location so tooltip is shown just below cursor. 
                mousePos.Offset(-2, 22) '+22 should work fine for normal sized windows cursors?
                ' Show item text in tooltip
                ToolTip1.Show(cb.Items(index).ToString, cb, mousePos.X, mousePos.Y)
            End If
            '
            ' Draw the focus rectangle if appropriate.
            e.DrawFocusRectangle()
        End Sub
    
        Private Sub ComboBox1_DropDownClosed(sender As Object, e As System.EventArgs) Handles ComboBox1.DropDownClosed
            ToolTip1.Hide(ComboBox1)
        End Sub
    
    End Class
    Last edited by Edgemeal; Mar 15th, 2016 at 01:49 PM. Reason: Clean up comments

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Oct 2014
    Posts
    81

    Re: Tooltip show on the drop down menu in the combobox

    Wow @Edgemeal...It is perfect solution Bro.. modified to my use, works great...thanks a big hug.

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Tooltip show on the drop down menu in the combobox

    Quote Originally Posted by mingolito View Post
    Wow @Edgemeal...It is perfect solution Bro.. modified to my use, works great...thanks a big hug.
    I really don't like using WindowFromPoint like that, but I was going for simple + less code.

    The right way to get the droplist handle is to use GetComboBoxInfo API similar to how I did in my controls, maybe something like,..
    Code:
     
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            ' SETUP TOOLTIP
            With ToolTip1
                .SetToolTip(ComboBox1, "TESTING 1,2,3")
                .IsBalloon = False
                .ShowAlways = True
            End With
            ' OWNER DRAWN ComboBox
            With ComboBox1
                .DrawMode = DrawMode.OwnerDrawFixed
                .BackColor = Color.LightYellow
                .ForeColor = Color.Black
            End With
        End Sub
    
    #Region "ComboBox events"
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
            Static lastindex As Integer = -1
            ' If nothing selected exit!
            If e.Index < 0 Then Return
            ' Draw the background of the ListBox control for each item.
            e.DrawBackground()
            ' Get combo that raised this event.
            Dim cb = DirectCast(sender, ComboBox)
            ' Draw item text using default colors
            Using brsh As New SolidBrush(e.ForeColor)
                e.Graphics.DrawString(cb.Items(e.Index).ToString, e.Font, brsh, e.Bounds)
            End Using
            ' Draw the focus rectangle if appropriate.
            e.DrawFocusRectangle()
            '
            ' check if list is dropped down,
            If cb.DroppedDown Then
                ' Get droplist handle.
                Dim hWnd = GetListBoxHandle(cb.Handle)
                If Not hWnd = IntPtr.Zero Then
                    ' Get droplist item index
                    Dim index As Int32 = SendMessage(hWnd, LB_GETCURSEL, 0, 0)
                    If index > -1 AndAlso lastindex <> index Then ' only show once or will flicker, disapear, etc.
                        lastindex = index
                        ' Get mouse pos for setting tooltip location.
                        Dim mousePos = cb.PointToClient(MousePosition)
                        ' Offset location so tooltip is shown just below cursor. 
                        mousePos.Offset(-2, 22) '+22 should work fine for normal sized windows cursors?
                        ' Show item text in tooltip
                        ToolTip1.Show(cb.Items(index).ToString, cb, mousePos.X, mousePos.Y)
                    End If
                End If
            End If
            '
        End Sub
    
        Private Sub ComboBox1_DropDownClosed(sender As Object, e As System.EventArgs) Handles ComboBox1.DropDownClosed
            ToolTip1.Hide(ComboBox1)
        End Sub
    
        Private Function GetListBoxHandle(hWnd As IntPtr) As IntPtr
            Dim cbi As New COMBOBOXINFO
            cbi.cbSize = Marshal.SizeOf(cbi)
            If GetComboBoxInfo(hWnd , cbi) Then
                Return cbi.hwndList
            Else
                Return IntPtr.Zero
            End If
        End Function
    
    #End Region
    
    #Region "API"
        Private Const LB_GETCURSEL As Int32 = &H188 ' message used to get item index the mouse is over in drop list.
    
        <DllImport("user32.dll")> _
        Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) As Int32
        End Function
    
        ' - GetComboBoxInfo...
        Private Enum ComboBoxButtonState As Int32
            STATE_SYSTEM_NONE = 0
            STATE_SYSTEM_INVISIBLE = &H8000
            STATE_SYSTEM_PRESSED = &H8
        End Enum
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure RECT
            Public Left, Top, Right, Bottom As Int32
        End Structure
    
        <StructLayout(LayoutKind.Sequential)> _
        Private Structure COMBOBOXINFO
            Public cbSize As Int32
            Public rcItem As RECT
            Public rcButton As RECT
            Public buttonState As ComboBoxButtonState
            Public hwndCombo As IntPtr
            Public hwndEdit As IntPtr
            Public hwndList As IntPtr
        End Structure
    
        <DllImport("user32.dll")> _
        Private Shared Function GetComboBoxInfo(ByVal hWnd As IntPtr, ByRef pcbi As COMBOBOXINFO) As Boolean
        End Function
    #End Region
    
    End Class
    EDIT: I didn't test what happens if the list isn't dropped down so added a check for that too, can never be too safe.
    Last edited by Edgemeal; Mar 15th, 2016 at 05:15 PM.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Oct 2014
    Posts
    81

    Re: Tooltip show on the drop down menu in the combobox

    Hi @Edgemeal I tested the first solution that you posted and it works perfectly, in vb2013, there is no problem with the dropped-down list, tried the second option it behaves the same way, no problem, I see no difference.

  9. #9
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Tooltip show on the drop down menu in the combobox

    Quote Originally Posted by mingolito View Post
    Hi @Edgemeal I tested the first solution that you posted and it works perfectly, in vb2013, there is no problem with the dropped-down list, tried the second option it behaves the same way, no problem, I see no difference.
    The one using WindowFromPoint may work but its really more like a hack, it just assumes the users mouse is over the droplist, whereas with GetComboBoxInfo there are no assumptions.

  10. #10
    New Member
    Join Date
    Oct 2020
    Posts
    1

    Re: Tooltip show on the drop down menu in the combobox

    Hello, this looks like a great solution, but it doesn't work fully for me (Win10). When I implemented it over an existing combobox, then in dropdown menu I can see only "System.Data.DataRowView" for each item in my combobox. The tooltip is again "System.Data.DataRowView". If I click on any of the items there, then in combobox the correct member with real name will be displayed. What could I be doing wrong?

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Tooltip show on the drop down menu in the combobox

    The way to do it, is to create a class that inherits from NativeWindow... I’ll find the example...

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Tooltip show on the drop down menu in the combobox

    Last edited by .paul.; Oct 16th, 2020 at 07:28 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width