Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

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

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
Avatar of it_saige
it_saige
Flag of United States of America image

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 -User generated image
-saige-
Avatar of Victor  Charles

ASKER

Thank You.
Hello,

My apology for the late reply.

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

Victor
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-
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
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer