How to Drop Item into Specified Location in VB List BoxID: Q80187
|
You can drag an item and drop it into a list box by using the Visual Basic TextHeight method and the Windows API SendMessage() function to calculate where to drop the item.
There is no standard way to determine the exact position where you are
dropping an item into a Visual Basic list box when you perform a drag
and drop operation. You must calculate the position using the TextHeight
method and the Windows API SendMessage() function with the constant
LB_GETTOPINDEX.
Using TextHeight, determine the height of each row of a list box. Divide
this by the Y value that is passed as an argument in the List_DragDrop
event procedure to determine how many lines from the top of the list box
that the Drag.Icon is located over. The SendMessage constant LB_GETTOPINDEX
gives you the index of the first visible item in the list box. Adding these
two numbers shows you the index location for the insertion point -- the
spot where you want to insert the item in the list box.
'============= Global.Bas ===============
'NOTE: Enter the following Declare statement as one, single line:
Declare Function SendMessage& Lib "User" (ByVal hWnd%, ByVal wMsg%,
ByVal wParam%, lParam As Any)
Declare Function GetFocus Lib "User" () As Integer
Global Const LB_GETTOPINDEX = &H400 + 15
'============== Form1.frm ==================
Sub List1_DragDrop (Source As Control, X As Single, Y As Single)
'get the first visible index in the list box
List1.SetFocus
ListHwnd = GetFocus()
TopI& = SendMessage(ListHwnd, LB_GETTOPINDEX, 0&, 0&)
ColumnHeight = TextHeight("A ")
InsertI& = Y \ ColumnHeight
If InsertI& <= List1.ListCount Then
' Enter the following two lines on one, single line:
List1.AddItem "This is inserted @" + Format$(InsertI&
+ TopI&, "0"), InsertI& + TopI&
Else
List1.AddItem "This is inserted"
End If
End Sub
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 11, 1999