locked
Mouse Location on Drag from another Form RRS feed

  • Question

  • Hello,  I am dragging a textbox from one from(B) to another form(A).  Textboxes are not visible on the form(A), so I was looking for the x,y position of the mouse.  However, it looks like when I do the drag,drop on the form I can't figure out a way to get the x,y position of the mouse at the time the left button was released.

    Any suggestions. 

    Monday, February 10, 2014 9:24 PM

Answers

  • Hi,

     Maybe i am not understanding what your trying to do but, the Drag and Drop events all have access to the x and y location of the mouse pointer. It gives the x and y location of the actual screen coordinates so if it is the actual Form coordinates that you want then you can use the PointToClient method to get the x and y of where the mouse pointer is in the form.

     Here is an example that you can check out to see how i got the x and y positions.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.AllowDrop = True
        End Sub
    
        Private Sub Form1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
            Dim dropPoint As Point = Me.PointToClient(New Point(e.X, e.Y))
            Me.Text = dropPoint.ToString 'Sets the forms text to the spot where your draging
        End Sub
    
        Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim dropPoint As Point = Me.PointToClient(New Point(e.X, e.Y))
            MessageBox.Show("dropped at - " & dropPoint.ToString)
    
            Dim strDragDrop As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Me.BackgroundImage = Image.FromFile(strDragDrop(0))
        End Sub
    
        Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
            Dim fileList As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Dim validExt() As String = {".jpg", ".bmp", ".png", ".gif"}
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                If validExt.Contains(IO.Path.GetExtension(fileList(0)).ToLower) Then
                    e.Effect = DragDropEffects.Copy
                End If
            End If
        End Sub
    End Class
    

    You can see as i drag it on the form the forms Title Text shows the coordinates and then when i drop the image it shows in a messagebox where i dropped it at.

    • Marked as answer by LandLord323 Tuesday, February 11, 2014 2:23 AM
    Monday, February 10, 2014 10:14 PM

All replies

  • Hi,

     Maybe i am not understanding what your trying to do but, the Drag and Drop events all have access to the x and y location of the mouse pointer. It gives the x and y location of the actual screen coordinates so if it is the actual Form coordinates that you want then you can use the PointToClient method to get the x and y of where the mouse pointer is in the form.

     Here is an example that you can check out to see how i got the x and y positions.

    Public Class Form1
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.AllowDrop = True
        End Sub
    
        Private Sub Form1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragOver
            Dim dropPoint As Point = Me.PointToClient(New Point(e.X, e.Y))
            Me.Text = dropPoint.ToString 'Sets the forms text to the spot where your draging
        End Sub
    
        Private Sub Form1_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragDrop
            Dim dropPoint As Point = Me.PointToClient(New Point(e.X, e.Y))
            MessageBox.Show("dropped at - " & dropPoint.ToString)
    
            Dim strDragDrop As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Me.BackgroundImage = Image.FromFile(strDragDrop(0))
        End Sub
    
        Private Sub Form1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Me.DragEnter
            Dim fileList As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
            Dim validExt() As String = {".jpg", ".bmp", ".png", ".gif"}
            If e.Data.GetDataPresent(DataFormats.FileDrop) Then
                If validExt.Contains(IO.Path.GetExtension(fileList(0)).ToLower) Then
                    e.Effect = DragDropEffects.Copy
                End If
            End If
        End Sub
    End Class
    

    You can see as i drag it on the form the forms Title Text shows the coordinates and then when i drop the image it shows in a messagebox where i dropped it at.

    • Marked as answer by LandLord323 Tuesday, February 11, 2014 2:23 AM
    Monday, February 10, 2014 10:14 PM
  • Great...

    what do I need to import, I don't have access to Me.PointToClient?

    Monday, February 10, 2014 10:40 PM
  • Hello,  I am dragging a textbox from one from(B) to another form(A).  Textboxes are not visible on the form(A), so I was looking for the x,y position of the mouse.  However, it looks like when I do the drag,drop on the form I can't figure out a way to get the x,y position of the mouse at the time the left button was released.

    Any suggestions. 

    I've never heard of a control being dragged from one form in the same app to another form in the same app.

    Doesn't each form have it's own list of controls for that form (i.e. Me.Controls)? I don't think you can remove a control from one form and drag it to another form then add it to the other form. Or maybe you can. But I surely don't know how to do it and don't want to expend the energy attempting to find a method that may be able to do so. As I figure the method would be substantial. Like copying the control to a memory stream or something if that's possible, removing it from the form it is being dragged from then adding it to the form it is being dragged to. And what if a mistake is made during the drag. Like the mouse is let up while the control has not reached the new form.

    Or maybe you have to copy every property or anything else in the old control, remove it from the old form, create a new control on the other form with all the properties from the other control and anything else in the old control. Makes no sense to do this to me.


    Please BEWARE that I have NO EXPERIENCE and NO EXPERTISE and probably onset of DEMENTIA which may affect my answers! Also, I've been told by an expert, that when you post an image it clutters up the thread and mysteriously, over time, the link to the image will somehow become "unstable" or something to that effect. :) I can only surmise that is due to Global Warming of the threads.


    • Edited by Mr. Monkeyboy Monday, February 10, 2014 10:47 PM 5555
    Monday, February 10, 2014 10:44 PM
  • what do I need to import, I don't have access to Me.PointToClient?

    Is there a message at compile time when you try to add that code? If so, you should quote the message.

    PointToClient is a method of the Control object.  A form is a control, so it is effectively available in any Windows forms application.

    http://msdn.microsoft.com/en-us/library/system.windows.forms.control.pointtoclient(v=vs.110).aspx

    Monday, February 10, 2014 11:05 PM
  • Great...

    what do I need to import, I don't have access to Me.PointToClient?

     Hi,

     It does not require any imports. If you are trying to type "Me.PointToClient(bla...bla...) " into your form project then intellisense does not usually show it unless you type it most or all the way out if i remember write. If you copy and pasted the whole line into your code and it is showing an error before you compile the project then post the error message you get. Also, just to be sure are you using a VB.NET Form Application or are you using another?

    Monday, February 10, 2014 11:23 PM
  • thanks for the help
    Tuesday, February 11, 2014 2:23 AM
  • thanks for the help

     Your Welcome.   :)
    Tuesday, February 11, 2014 2:42 AM