How to Drop Item into Specified Location in VB List Box

Last reviewed: June 21, 1995
Article 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

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

1. 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 "User" (ByVal hWnd%, ByVal wMsg%,
          ByVal wParam%, lParam As Any)
       Declare Function GetFocus Lib "User" () 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("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
    
    

  • 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 reference words: 1.00 2.00 3.00
    KBCategory: kbprg kbcode
    KBSubcategory: PrgCtrlsStd


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: June 21, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.