Results 1 to 6 of 6

Thread: MouseDown Event Help Needed (DataGrid) [RESOLVED]

  1. #1

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677

    MouseDown Event Help Needed (DataGrid) [RESOLVED]

    Here is the code I use to initiate a Drag and Drop Sequence:

    VB Code:
    1. Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    2.     If Button = vbLeftButton Then 'Begins the dragging process
    3.         DataGrid1.DragIcon = QuoteForm.ImageList1.ListImages(6).Picture
    4.         DataGrid1.Drag vbBeginDrag
    5.         IMTextBox.Text = DataGrid1.Text
    6.         QuoteForm.dragging = True
    7.         QuoteForm.Show
    8.     End If
    9.     QuoteForm.txtDragValue = DataGrid1.Text & "_MAT"
    10. End Sub

    This works great for Drag and Drop BUT if I need to resize the DataGrid Columns, it doesn't work as VB thinks I'm in the middle of a Drag and Drop. Can someone help me change my code so that I can still use it to Drag and Drop BUT still give my users the ability to resize the columns? Thanks, Jeremy
    Last edited by JCScoobyRS; Apr 3rd, 2003 at 12:30 PM.
    He who listens well, speaks well.

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357
    Would this work?
    • Create a module level variable that tracks if the user is resizing
    • In the datagrid Mouse_Down event, set this variable to True
    • In the datagrid Mouse_Move event, check the variable and only do drag/drop if it's False
    • In the datagrid Mouse_Up event, reset the variable to False
    I may be wrong, but I think you can only resize columns in the header, so this code should work:
    VB Code:
    1. Private MOUSE_IN_COL_HEADER As Boolean
    2.  
    3. Private Sub DataGrid1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    4.     If Y < Int(DataGrid1.Columns.Item(0).Top + 2) Then MOUSE_IN_COL_HEADER = True
    5. End Sub
    6.  
    7. Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    8.     If Not MOUSE_IN_COL_HEADER Then
    9.         If Button = vbLeftButton Then 'Begins the dragging process
    10.             DataGrid1.DragIcon = QuoteForm.ImageList1.ListImages(6).Picture
    11.             DataGrid1.Drag vbBeginDrag
    12.             IMTextBox.Text = DataGrid1.Text
    13.             QuoteForm.dragging = True
    14.             QuoteForm.Show
    15.         End If
    16.         QuoteForm.txtDragValue = DataGrid1.Text & "_MAT"
    17.     End If
    18. End Sub
    19.  
    20. Private Sub DataGrid1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    21.     MOUSE_IN_COL_HEADER = False
    22. End Sub
    ~seaweed

  3. #3

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Is there anything for the DataGrid similar to "HitText" in the TreeView? If so, I could check to see if it's above Column(0) and if so, don't drag but if not, begin drag. How's that idea? Thanks, Jeremy
    He who listens well, speaks well.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Check out the RowContaining and ColContaining methods. They return the Row/Col given an X/Y coordinate.

    FYI: Drag and Drop is usually started in the MouseDown event, not sure why you are starting it every time the mouse is moved.

  5. #5

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    You are correct and although there are many ways to get it to work, this way works. After I complete my project, I'll be optimizing it and that is on the list. Thanks, Jeremy
    He who listens well, speaks well.

  6. #6

    Thread Starter
    Fanatic Member JCScoobyRS's Avatar
    Join Date
    Oct 2002
    Location
    Some Mountain in Colorado
    Posts
    677
    Bruce,
    Once again your expert advice pays off. Here is the working code:

    VB Code:
    1. Private Sub DataGrid1_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
    2. Dim RowVal As Single
    3. Dim ColVal As Single
    4. RowVal = DataGrid1.RowContaining(y)
    5. ColVal = DataGrid1.ColContaining(x)
    6.  
    7.     If Button = vbLeftButton Then 'Begins the dragging process
    8.         If RowVal > 0 And ColVal > 0 Then
    9.             DataGrid1.DragIcon = QuoteForm.ImageList1.ListImages(6).Picture
    10.             DataGrid1.Drag vbBeginDrag
    11.             IMTextBox.Text = DataGrid1.Text
    12.             QuoteForm.dragging = True
    13.             QuoteForm.Show
    14.         End If
    15.     End If
    16.     QuoteForm.txtDragValue = DataGrid1.Text & "_MAT"
    17. End Sub

    Thanks, Jeremy
    He who listens well, speaks well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width