Re: How to tell where the selected line is on a listbox

Tech-Archive recommends: Fix windows errors by optimizing your registry



That was all I needed to know. How you get the textbox off of the listbox.
Losing focus-neat trix. I usually use textbox1.visible=-1/0. But this works.
Thanks code wizard. Glad I did not port my app to notnet, and now that I
read the forums, I am glad it was not just me.

"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> wrote in message
news:OiG3ZmYdIHA.2688@xxxxxxxxxxxxxxxxxxxxxxx
"Keith A. Henderson" <keithatdvsolutions.tv> wrote in message
news:u7lmQbRdIHA.4220@xxxxxxxxxxxxxxxxxxxxxxx

OMG It works!!!!!! You are a super genius!!!!

Such excitement . . . . .

Spoke too soon- How do I remove the textbos after
the user has 'edited' the like they pressed f2 on. Seems
the visible =0 won't work...

. . . and then such a terrible let down, and so soon as well :-(

Actually the code was intended as just a "starter" for you, to point you
in the right direction. Now that you've got the basics of it off the
ground you need to modify it to yourself to suit your own exact
requirements. As soon as the user finishes editing the entry in the
TextBox you need to hide the TextBox so as to show the underlying ListBox
item which was temporarily hidden beneath it, and of course you need to
transfer the contents of the TextBox to the "edited" ListBox item. There
are many ways in which the user can signify that he has finished editing,
and you will need to take all those into account, but in this example I
have assumed that when the user presses the Enter key (while editing) it
signifies that he has finished, and so the code sets focus to a dummy
control, thereby causing the TextBox to lose focus and thereby causing the
code in its LostFocus ervent to fire, which performs the actual task of
updating the ListBox and effectively hiding the TextBox. Also, if the user
selects another control on the Form it will also cause the TextBox to lose
focus, performing the same task for us. There are other things that I'll
leave for you to code yourself (such as, for example, the user scrolling
the ListBox).

Anyway, try the following code. Paste it into a Form containign a ListBox,
a TextBox, a Command Button and a PictureBox (leave them all at their
default namnes for the time being). Pressing F2 will allow you to edit
whichever item happens to be the currently selected item in the ListBox,
and it will bring it into view if it is not already in view. Pressing F2
when there is no item currently selected in the ListBox will cause the
currently topmost visible item to be edited. Does this put the smile back
on your face? ;-)

but is it just me or you still using vb6 as well. Tried 2008.
I just didn't get it, ad my app started locking up.

Yep. I use VB6. It is the last real version of Visual Basic. That 2008
thing is an imposter, and VB6 code will not work in it ;-)

Mike

Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, _
ByVal wMsg As Long, ByVal wParam As Long, _
lParam As Any) As Long
Private Declare Function ClientToScreen Lib "user32" _
(ByVal hwnd As Long, lpPoint As POINTAPI) As Long
Private Declare Function GetClientRect Lib "user32" _
(ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Const LB_GETITEMRECT As Long = &H198

Private Sub Form_Load()
Dim n As Long
Me.KeyPreview = True
Me.ScaleMode = vbPixels
Text1.Left = -Text1.Width
Text1.BorderStyle = vbBSNone
Text1.ZOrder
For n = 0 To 99
List1.AddItem Format(n) & " some text " & Format(n)
Next n
Caption = "Press F2 to edit the item currently " & _
" selected ListBox item."
Picture1.Left = -Picture1.Width
End Sub

Private Sub Form_KeyUp(KeyCode As Integer, Shift As Integer)
Dim k As Long
If KeyCode = vbKeyF2 Then
k = List1.ListIndex
If k >= 0 Then
List1.ListIndex = -1 ' force the index
List1.ListIndex = k ' into view
Text1.Tag = CStr(k)
TextBoxOnListBox Text1, List1, List1.ListIndex
Else
List1.ListIndex = List1.TopIndex
TextBoxOnListBox Text1, List1, List1.ListIndex
End If
End If
End Sub

Private Sub TextBoxOnListBox(TB As TextBox, _
LB As ListBox, z As Long)
Dim r1 As RECT, r2 As RECT
Dim p1 As POINTAPI, p2 As POINTAPI
ClientToScreen Me.hwnd, p1
ClientToScreen LB.hwnd, p2
GetClientRect LB.hwnd, r2
SendMessage LB.hwnd, LB_GETITEMRECT, z, r1
If r1.Top >= 0 And r1.Top < r2.Bottom Then
TB.BackColor = LB.BackColor
TB.Text = LB.List(z)
TB.Move p2.X - p1.X + 1, p2.Y - p1.Y + r1.Top, _
r1.Right - r1.Left, r1.Bottom - r1.Top
TB.SelStart = Len(TB.Text)
TB.SetFocus
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
KeyAscii = 0 ' suppress the beep
Picture1.SetFocus ' force TextBox to lose the focus
End If
End Sub

Private Sub Text1_LostFocus()
List1.List(List1.ListIndex) = Text1.Text
Text1.Left = -Text1.Width
End Sub







.


Quantcast