ListBox read item on double click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jdwyer05
    New Member
    • Nov 2006
    • 8

    ListBox read item on double click

    Hi,

    I have a list box where I am reading information from a MS Access Database.
    The list box populates when the form is loaded. However, sometimes the information is wider than the list box. I have added scroll bars so user can see the information. However, It would be great if they didn't need to scroll.

    I've read that listboxes cannot wrap text so, is there a way to use a double click event on a item to readthat line from the list box and display it in another text field so i can use the mutliline function? Or even better after the item is double clicked a pop up form will appear with the item

    Thanks in advance, i hope that made sense
  • albertw
    Contributor
    • Oct 2006
    • 267

    #2
    Originally posted by jdwyer05
    Hi,

    I have a list box where I am reading information from a MS Access Database.
    The list box populates when the form is loaded. However, sometimes the information is wider than the list box. I have added scroll bars so user can see the information. However, It would be great if they didn't need to scroll.

    I've read that listboxes cannot wrap text so, is there a way to use a double click event on a item to readthat line from the list box and display it in another text field so i can use the mutliline function? Or even better after the item is double clicked a pop up form will appear with the item

    Thanks in advance, i hope that made sense
    hi

    this might be a solution

    Code:
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const LB_ITEMFROMPOINT = &H1A9
    Dim ndx As Long
    
    Public Function ListBoxHit(ListBox As ListBox, ByVal X As Single, ByVal Y As Single) As Long
        ndx = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, (Y \ Screen.TwipsPerPixelY) * 65536 + (X \ Screen.TwipsPerPixelX))
        If (ndx And &H10000) = &H10000 Then
            ListBoxHit = -1
        Else
            ListBoxHit = ndx
        End If
    End Function
    
    Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Label1.Visible = False
    End Sub
    
    Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    If Button = 2 Then
      ndx = ListBoxHit(List1, X, Y)
      If Not ndx = -1 Then List1.Selected(ndx) = True
      With Label1
      .Move List1.Left + List1.Width, List1.Top + Y - 60
      .Caption = " " & List1.List(ndx) & " "
      .Visible = True
      End With
    Else
    Label1.Visible = False
    End If
    End Sub
    the listbox = List1
    the label=Label1
    create:
    visible=false
    backstyle=opaqu e
    autosize=true
    borderstyle=fix ed single
    backcolor= any color

    click with right mouse on a list.item will trigger the label to pop-up

    Comment

    • jdwyer05
      New Member
      • Nov 2006
      • 8

      #3
      albertw,

      Im still having problems reading the events, the code you provided does implement the click event. However, it will only read the first item int he list box.
      for example:

      if i have a list with the records 1,2,3,4,5 and i click on 5 it will still display the record for number 1.


      Originally posted by albertw
      hi

      this might be a solution

      Code:
      Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      
      Private Const LB_ITEMFROMPOINT = &H1A9
      Dim ndx As Long
      
      Public Function ListBoxHit(ListBox As ListBox, ByVal X As Single, ByVal Y As Single) As Long
          ndx = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, (Y \ Screen.TwipsPerPixelY) * 65536 + (X \ Screen.TwipsPerPixelX))
          If (ndx And &H10000) = &H10000 Then
              ListBoxHit = -1
          Else
              ListBoxHit = ndx
          End If
      End Function
      
      Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
      Label1.Visible = False
      End Sub
      
      Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
      If Button = 2 Then
        ndx = ListBoxHit(List1, X, Y)
        If Not ndx = -1 Then List1.Selected(ndx) = True
        With Label1
        .Move List1.Left + List1.Width, List1.Top + Y - 60
        .Caption = " " & List1.List(ndx) & " "
        .Visible = True
        End With
      Else
      Label1.Visible = False
      End If
      End Sub
      the listbox = List1
      the label=Label1
      create:
      visible=false
      backstyle=opaqu e
      autosize=true
      borderstyle=fix ed single
      backcolor= any color

      click with right mouse on a list.item will trigger the label to pop-up

      Comment

      • jdwyer05
        New Member
        • Nov 2006
        • 8

        #4
        It looks like ndx is not updating with the correct selected record. B/c if you just enter an integer value into the .Caption = " " & List1.List(ndx) & " " it will display the record corresponding to that value. So it seems to be that ndx is constantly remaining the value 0.




        Originally posted by jdwyer05
        albertw,

        Im still having problems reading the events, the code you provided does implement the click event. However, it will only read the first item int he list box.
        for example:

        if i have a list with the records 1,2,3,4,5 and i click on 5 it will still display the record for number 1.

        Comment

        Working...