How to Drop Item into Specified Location in VB List BoxLast reviewed: June 21, 1995Article ID: Q80187 |
The information in this article applies to:
- Standard and Professional Editions of Microsoft Visual Basic for Windows, versions 2.0 and 3.0- Microsoft Visual Basic programming system for Windows, version 1.0
SUMMARYYou 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.
MORE INFORMATIONThere 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.
Step-by-Step Example to Demonstrate Dropping Items into List box1. Start Visual Basic or from the File menu, choose New Project (ALT, F, N)if Visual Basic is already running. Form1 is created by default.
'============= 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 Drag and drop the picture box over the list box and an item should be added to the list box. An item will be added to the list box each time you drag and drop the picture box over the list box.
|
Additional reference words: 1.00 2.00 3.00
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |