How to Drag Data from a Grid to a List Box in VB 3.0 (113436)



The information in this article applies to:

  • Microsoft Visual Basic Standard Edition for Windows 3.0

This article was previously published under Q113436

SUMMARY

This article shows by example how to select a cell from a grid control and drag the selection to a list box control. The sample code uses data from the BIBLIO.MDB database that came with Visual Basic. The data will be used to populate the Grid control.

By using the technique demonstrated in this article, you will have the ability to allow your users to click on any line of the grid control and drag the data to a sorted list box control. You could use this to build documents that contain certain fields of data from a database.

MORE INFORMATION

Example of Dragging and Dropping from a Grid Control to a List Box

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a List box (List1) and a Grid (Grid1) to the form.
  3. Place the following code in the general declarations section of the form:
       ' Dragging is a flag used for each control to determine
       ' if something is being dragged.
       Dim dragging As Integer
       Dim text_to_drag$
  4. Add the following code to the Form_Load event procedure:
       Sub Form_Load ()
          ' Initialize the grid control:
          Grid1.Cols = 3
          Grid1.Rows = 60
          Dim db As database
          Dim ds As dynaset
          Dim counter%
          grid1.ColWidth(1) = 3000      'For Author name
          grid1.ColWidth(2) = 1000      'For Author ID
          grid1.Col = 1
          grid1.Row = 0
          grid1.Text = "Author Name"    'Header for Author Name
          grid1.Col = 2
          grid1.Row = 0
          grid1.Text = "Author ID"      'Header for Author ID
          Set db = OpenDatabase("BIBLIO.MDB")
          Set ds = db.CreateDynaset("Authors")
          counter% = 1                  'Start counter at Row=1
          Do Until ds.EOF Or counter% = 60
             grid1.Col = 1
             grid1.Row = counter%
             grid1.Text = "" & ds(1)       'Load the Author Name
             grid1.Col = 2
             grid1.Row = counter%
             grid1.Text = "" & ds(0)       'Load the Author ID
             counter% = counter% + 1
             ds.MoveNext
          Loop
          ds.Close
          db.Close
       End Sub
  5. Add the following code to the Grid1_Click event procedure:
       Sub Grid1_Click ()
          ' Highlight entire row:
          Dim highlight%
          highlight% = grid1.Row
          grid1.SelStartRow = highlight%
          grid1.SelEndRow = highlight%
          grid1.SelStartCol = 1
          grid1.SelEndCol = 2
       End Sub
  6. Add the following code to the Grid1_DragDrop event procedure:
       Sub Grid1_DragDrop (Source As Control, X As Single, Y As Single)
          grid1.Drag 0
          dragging = False
       End Sub
  7. Add the following code to the Grid1_MouseDown event procedure:
       ' Enter the following two lines as one, single line:
       Sub Grid1_MouseDown (Button As Integer, Shift As Integer,
          X As Single, Y As Single)
          ' If the mouse goes down, set the dragging flag in case this
          ' is for a drag:
          Dim selected%
          dragging = True
          selected% = grid1.Row
          grid1.Col = 1
          text_to_drag$ = Trim$(grid1.Text)
       End Sub
  8. Add the following code to the Grid1_MouseMove event procedure:
       ' Enter the following two lines as one, single line:
       Sub Grid1_MouseMove (Button As Integer, Shift As Integer, X As
          Single, Y As Single)
          ' If the dragging flag was set, then we will enable the drag
          ' MouseDown has to set the flag first
          If dragging Then
             dragging = False  ' Cancel the flag.
             grid1.Drag 1      ' Start the drag mode.
          Else
             grid1.Drag 0      ' Cancel if flag was not set.
          End If
       End Sub
  9. Add the following code to the Grid1_MouseUp event procedure:
       ' Enter the following two lines as one, single line:
       Sub Grid1_MouseUp (Button As Integer, Shift As Integer, X As
          Single, Y As Single)
          ' Mouse released on text box, so cancel the dragging mode:
          grid1.Drag 0
          dragging = False
       End Sub
  10. Add the following code to the List1_DragDrop event procedure:
        Sub List1_DragDrop (Source As Control, X As Single, Y As Single)
           List1.AddItem text_to_drag$  ' This inserts the text.
        End Sub
  11. Run the program.
Select an item from Grid1. Hold the mouse button down and drag the gray outline toward the List box. Then drop the item in the List1 box control by releasing the mouse button.

Modification Type:MinorLast Reviewed:1/9/2003
Keywords:KB113436