We help IT Professionals succeed at work.

Help with  viewing definition of acronym when placing mouse on a row of a listbox

111 Views
Last Modified: 2017-05-02
Hi,
 I have a listbox  with multiple rows containing acronyms, when I place a my cursor on a row, how do I display the definition of the row my mouse is over using VB.NET?, for example when place mouse on row containing NSC would like to see National Short Code
Thanks,

Victor
Comment
Watch Question

it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
Assuming that the acronym's definition is a part of the object that is bound the the list box row, perhaps something like:

Form1.vb -
Public Class Form1
    Private lastHoveredIndex As Integer = -1

    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.DataSource = (From i In Enumerable.Range(0, 20) Select New Acronym() With {.ID = i, .Acronym = String.Format("ACR{0}", i), .Definition = String.Format("Definition for ACR{0}", i)}).ToList()
        ListBox1.DisplayMember = "Acronym"
    End Sub

    Private Sub OnMouseMove(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseMove
        Dim newHoveredIndex = ListBox1.IndexFromPoint(e.Location)
        If (lastHoveredIndex <> newHoveredIndex) Then
            lastHoveredIndex = newHoveredIndex
            If (lastHoveredIndex > -1) Then
                ToolTip1.Active = False
                ToolTip1.SetToolTip(ListBox1, CType(ListBox1.Items(lastHoveredIndex), Acronym).Definition)
                ToolTip1.Active = True
            End If
        End If
    End Sub
End Class

Class Acronym
    Public Property ID() As Integer
    Public Property Acronym() As String
    Public Property Definition() As String
End Class

Open in new window

Form1.Designer.vb -
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    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.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container()
        Me.ListBox1 = New System.Windows.Forms.ListBox()
        Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
        Me.SuspendLayout()
        '
        'ListBox1
        '
        Me.ListBox1.FormattingEnabled = True
        Me.ListBox1.Location = New System.Drawing.Point(13, 13)
        Me.ListBox1.Name = "ListBox1"
        Me.ListBox1.Size = New System.Drawing.Size(259, 238)
        Me.ListBox1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(284, 261)
        Me.Controls.Add(Me.ListBox1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents ListBox1 As ListBox
    Friend WithEvents ToolTip1 As ToolTip
End Class

Open in new window

Which produces the following output -Capture.PNG
-saige-

Author

Commented:
Thank You.

Author

Commented:
Hello,

My apology for the late reply.

How do you create the acronym definition and bound it to the list box row?

Victor
it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

Commented:
With this:
    Private Sub OnLoad(sender As Object, e As EventArgs) Handles MyBase.Load
        ListBox1.DataSource = (From i In Enumerable.Range(0, 20) Select New Acronym() With {.ID = i, .Acronym = String.Format("ACR{0}", i), .Definition = String.Format("Definition for ACR{0}", i)}).ToList()
        ListBox1.DisplayMember = "Acronym"
    End Sub

Open in new window

Which uses this class definition for Acronym:
Class Acronym
    Public Property ID() As Integer
    Public Property Acronym() As String
    Public Property Definition() As String
End Class

Open in new window


-saige-

Author

Commented:
Hi,
How do I build the part to display the definition?

For example if mouse is over SN row to display  "Serial Number"?

Thanks,

Victor
Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.