Windows Develop Bookmark and Share   
 index > Windows Forms General > Drag and drop graphical question
 

Drag and drop graphical question

Hi

I have a datagrid view control, and four text boxes.

The textboxes are in a table layout panel, with rows set to Autosize and a startup visibility set to False. The AllowDrop property is true on all 4 textboxes, and the datagrid.

My aim is to have the four text boxes appear when the user drags a row out of the data grid view. I am currently accomplishing this by the following code:

Public isDrag As Boolean = False

    Private Sub BeginDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGVFavourites.MouseDown
        Dim data As DataGridView = CType(sender, DataGridView)

        If DGVFavourites.SelectedRows.Count = 1 Then
            data.DoDragDrop(data.SelectedRows(0), DragDropEffects.Copy)
            isDrag = True
        End If
    End Sub

    Private Sub ShowBoxes(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DGVFavourites.DragLeave
        If isDrag = True Then
            TBX1Drop.Visible = True
            TBX2Drop.Visible = True
            TBX3Drop.Visible = True
            TBX4Drop.Visible = True
        End If
    End Sub


Then the user drags the row on to one of the four text boxes, and I process the data. I am doing that by this code:

    Private Sub DragDropEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TBX1Drop.DragEnter, TBX4Drop.DragEnter, TBX3Drop.DragEnter, TBX2Drop.DragEnter
        e.Effect = DragDropEffects.Copy
        sender.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub DragDropLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBX1Drop.DragLeave, TBX4Drop.DragLeave, TBX3Drop.DragLeave, TBX2Drop.DragLeave
        sender.BorderStyle = BorderStyle.Fixed3D
    End Sub

    Private Sub DragDropComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TBX1Drop.DragDrop, TBX4Drop.DragDrop, TBX3Drop.DragDrop, TBX2Drop.DragDrop
        Dim data As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))

        sender.BorderStyle = BorderStyle.Fixed3D

        Select Case sender.Name
            Case Is = "TBX1Drop"
                MODGeneralRoutines.Play(Player.One, data.Cells(0).Value.ToString())
            Case Is = "TBX2Drop"
                MODGeneralRoutines.Play(Player.Two, data.Cells(0).Value.ToString())
            Case Is = "TBX3Drop"
                MODGeneralRoutines.Play(Player.Three, data.Cells(0).Value.ToString())
            Case Is = "TBX4Drop"
                MODGeneralRoutines.Play(Player.Four, data.Cells(0).Value.ToString())
        End Select

        TBX1Drop.Visible = False
        TBX2Drop.Visible = False
        TBX3Drop.Visible = False
        TBX4Drop.Visible = False

        isDrag = False
    End Sub


The problem is that the user can release the mouse button when their cursor is not over one of the "drop boxes" and the boxes are still displayed. How do I fix this?

If you require any more info please reply or message me.

Thanks
Guy Joseph - Intel Quad Q9450, 4Gb Ram, 1Tb Hard Drive, NVidia 8800GT, Marian Marc 8, VB Express 2008
  • Edited byGuyJos Wednesday, June 17, 2009 2:45 PMMaking code samples even clearer!
  • Edited byGuyJos Wednesday, June 17, 2009 2:43 PMMaking code samples clearer
  •  
GuyJos  Wednesday, June 17, 2009 2:42 PM
Hide them again after the DoDragDrop() call instead of the DragDrop event handler.

Hans Passant.
  • Marked As Answer byGuyJos Wednesday, June 17, 2009 9:13 PM
  •  
nobugz  Wednesday, June 17, 2009 6:52 PM
Hide them again after the DoDragDrop() call instead of the DragDrop event handler.

Hans Passant.
  • Marked As Answer byGuyJos Wednesday, June 17, 2009 9:13 PM
  •  
nobugz  Wednesday, June 17, 2009 6:52 PM
Is that the DoDragDrop handler for the data grid view?

Thanks

Guy Joseph - Intel Quad Q9450, 4Gb Ram, 1Tb Hard Drive, NVidia 8800GT, Marian Marc 8, VB Express 2008
GuyJos  Wednesday, June 17, 2009 8:53 PM
There's one DoDragDrop() call in your snippet. That one. It is a call, not a handler.

Hans Passant.
  • Marked As Answer byGuyJos Wednesday, June 17, 2009 9:13 PM
  • Unmarked As Answer byGuyJos Wednesday, June 17, 2009 9:13 PM
  •  
nobugz  Wednesday, June 17, 2009 8:58 PM
EDIT: Just tried that, and they don't appear at all.

Ok, so copy the code just below there, but still leave a copy where it is now?

Sorry I got confused with the DragDrop handler and the DoDragDrop() Call.

Many thanks

Guy Joseph - Intel Quad Q9450, 4Gb Ram, 1Tb Hard Drive, NVidia 8800GT, Marian Marc 8, VB Express 2008
  • Edited byGuyJos Wednesday, June 17, 2009 9:11 PMAdded result
  •  
GuyJos  Wednesday, June 17, 2009 9:09 PM
Hide them again after the DoDragDrop() call instead of the DragDrop event handler.

Hans Passant.

The Code now reads:

    Private Sub BeginDrag(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DGVFavourites.MouseDown
        Dim data As DataGridView = CType(sender, DataGridView)

        If DGVFavourites.SelectedRows.Count = 1 Then
            TBX1Drop.Visible = True
            TBX2Drop.Visible = True
            TBX3Drop.Visible = True
            TBX4Drop.Visible = True
            data.DoDragDrop(data.SelectedRows(0), DragDropEffects.Copy)
            TBX1Drop.Visible = False
            TBX2Drop.Visible = False
            TBX3Drop.Visible = False
            TBX4Drop.Visible = False
        End If
    End Sub

    Private Sub DragDropEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TBX1Drop.DragEnter, TBX4Drop.DragEnter, TBX3Drop.DragEnter, TBX2Drop.DragEnter
        e.Effect = DragDropEffects.Copy
        sender.BorderStyle = BorderStyle.FixedSingle
    End Sub

    Private Sub DragDropLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TBX1Drop.DragLeave, TBX4Drop.DragLeave, TBX3Drop.DragLeave, TBX2Drop.DragLeave
        sender.BorderStyle = BorderStyle.Fixed3D
    End Sub

    Private Sub DragDropComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles TBX1Drop.DragDrop, TBX4Drop.DragDrop, TBX3Drop.DragDrop, TBX2Drop.DragDrop
        Dim data As DataGridViewRow = e.Data.GetData(GetType(DataGridViewRow))

        sender.BorderStyle = BorderStyle.Fixed3D

        Select Case sender.Name
            Case Is = "TBX1Drop"
                MODGeneralRoutines.Play(Player.One, data.Cells(0).Value.ToString())
            Case Is = "TBX2Drop"
                MODGeneralRoutines.Play(Player.Two, data.Cells(0).Value.ToString())
            Case Is = "TBX3Drop"
                MODGeneralRoutines.Play(Player.Three, data.Cells(0).Value.ToString())
            Case Is = "TBX4Drop"
                MODGeneralRoutines.Play(Player.Four, data.Cells(0).Value.ToString())
        End Select
    End Sub


Guy Joseph - Intel Quad Q9450, 4Gb Ram, 1Tb Hard Drive, NVidia 8800GT, Marian Marc 8, VB Express 2008
GuyJos  Wednesday, June 17, 2009 9:14 PM

You can use google to search for other answers

Custom Search

More Threads

• OpenFileDialog Multiselect problem
• How to custom painting on the listview control and scrollbar?
• I need some help with the listview control.
• Drawing multiple circles
• Multiple forms?
• cross thread & forms
• Multiple FontStyles
• How to Open Windows Printer selection
• datagridview
• How do i get on MSN Messenger 7.5?