Microsoft KB Archive/80187

= How to Drop Item into Specified Location in VB List Box =

Article ID: 80187

Article Last Modified on 12/12/2003

-

APPLIES TO


 * Microsoft Visual Basic 2.0 Standard Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 2.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 1.0 Standard Edition

-



This article was previously published under Q80187



SUMMARY
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.



MORE INFORMATION
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.

Step-by-Step Example to Demonstrate Dropping Items into List box
 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. Add a Picture control (Picture1) to Form1 and set its DragMode property to Automatic. Then add a List box (List1) to Form1 and set its DragMode property to Manual.  Add the following code to the global module: '============= Global.Bas =============== 'NOTE: Enter the following Declare statement as one, single line:

Declare Function SendMessage& Lib &quot;User&quot; (ByVal hWnd%, ByVal wMsg%,     ByVal wParam%, lParam As Any) Declare Function GetFocus Lib &quot;User&quot; As Integer Global Const LB_GETTOPINDEX = &H400 + 15

  Add the following code to the DragDrop event procedure of List1: '============== 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(&quot;A &quot;) InsertI& = Y \ ColumnHeight If InsertI& <= List1.ListCount Then ' Enter the following two lines on one, single line: List1.AddItem &quot;This is inserted @&quot; + Format$(InsertI&           + TopI&, &quot;0&quot;), InsertI& + TopI& Else List1.AddItem &quot;This is inserted&quot; End If

End Sub

 From the Run menu, choose Start (ALT, R, S) to run the program.

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 query words: 2.00 3.00

Keywords: KB80187

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.