By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,553 Members | 1,128 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,553 IT Pros & Developers. It's quick & easy.

ListBox read item on double click

P: 8
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
Nov 21 '06 #1
Share this Question
Share on Google+
3 Replies


100+
P: 267
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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
  3.  
  4. Private Const LB_ITEMFROMPOINT = &H1A9
  5. Dim ndx As Long
  6.  
  7. Public Function ListBoxHit(ListBox As ListBox, ByVal X As Single, ByVal Y As Single) As Long
  8.     ndx = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, (Y \ Screen.TwipsPerPixelY) * 65536 + (X \ Screen.TwipsPerPixelX))
  9.     If (ndx And &H10000) = &H10000 Then
  10.         ListBoxHit = -1
  11.     Else
  12.         ListBoxHit = ndx
  13.     End If
  14. End Function
  15.  
  16. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  17. Label1.Visible = False
  18. End Sub
  19.  
  20. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  21. If Button = 2 Then
  22.   ndx = ListBoxHit(List1, X, Y)
  23.   If Not ndx = -1 Then List1.Selected(ndx) = True
  24.   With Label1
  25.   .Move List1.Left + List1.Width, List1.Top + Y - 60
  26.   .Caption = " " & List1.List(ndx) & " "
  27.   .Visible = True
  28.   End With
  29. Else
  30. Label1.Visible = False
  31. End If
  32. End Sub
  33.  
  34.  
  35.  
the listbox = List1
the label=Label1
create:
visible=false
backstyle=opaque
autosize=true
borderstyle=fixed single
backcolor= any color

click with right mouse on a list.item will trigger the label to pop-up
Nov 21 '06 #2

P: 8
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.


hi

this might be a solution

Expand|Select|Wrap|Line Numbers
  1.  
  2. 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
  3.  
  4. Private Const LB_ITEMFROMPOINT = &H1A9
  5. Dim ndx As Long
  6.  
  7. Public Function ListBoxHit(ListBox As ListBox, ByVal X As Single, ByVal Y As Single) As Long
  8.     ndx = SendMessage(List1.hWnd, LB_ITEMFROMPOINT, 0, (Y \ Screen.TwipsPerPixelY) * 65536 + (X \ Screen.TwipsPerPixelX))
  9.     If (ndx And &H10000) = &H10000 Then
  10.         ListBoxHit = -1
  11.     Else
  12.         ListBoxHit = ndx
  13.     End If
  14. End Function
  15.  
  16. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  17. Label1.Visible = False
  18. End Sub
  19.  
  20. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  21. If Button = 2 Then
  22.   ndx = ListBoxHit(List1, X, Y)
  23.   If Not ndx = -1 Then List1.Selected(ndx) = True
  24.   With Label1
  25.   .Move List1.Left + List1.Width, List1.Top + Y - 60
  26.   .Caption = " " & List1.List(ndx) & " "
  27.   .Visible = True
  28.   End With
  29. Else
  30. Label1.Visible = False
  31. End If
  32. End Sub
  33.  
  34.  
  35.  
the listbox = List1
the label=Label1
create:
visible=false
backstyle=opaque
autosize=true
borderstyle=fixed single
backcolor= any color

click with right mouse on a list.item will trigger the label to pop-up
Nov 29 '06 #3

P: 8
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.




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.
Nov 29 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.