dcsimg
Results 1 to 17 of 17

Thread: [RESOLVED] Cannot move dropped textbox

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Resolved [RESOLVED] Cannot move dropped textbox

    Dear Gurus,
    I have an application where I'm actually moving, with drag and drop, some texboxes in a picturebox.
    Now, once dropped, each textbox is becoming locked, in the sense that I cannot further adjust location and move it, while I would like to enable it.

    Do you know how can I achieve it? Is there any property in the dropped textbox that I'm missing?

    I report here below the implemented code.

    Thanks for your support,
    A.

    Code:
    Private Sub Form11_Load(sender As Object, e As EventArgs) Handles Me.Load
            PictureBox3.AllowDrop = True
    
        End Sub
    
    
        Private Sub PictureBox3_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox3.DragDrop
    
            Dim tb As TextBox = DirectCast(e.Data.GetData(GetType(TextBox)), TextBox)
            Dim t1 As New TextBox
    
            t1.Location = New Point(MyPanel2.PointToClient(Control.MousePosition).X - Me.PictureBox3.Left,
                                    MyPanel2.PointToClient(Control.MousePosition).Y - Me.PictureBox3.Top)
            t1.Text = tb.Text
            t1.Font = tb.Font
            t1.TextAlign = HorizontalAlignment.Center
            t1.ForeColor = tb.ForeColor
            t1.BackColor = tb.BackColor
    
            PictureBox3.Controls.Add(t1)
    
        End Sub
    
        Private Sub PictureBox3_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox3.DragEnter
            If e.Data.GetDataPresent(GetType(TextBox)) And CBool(e.KeyState And 8) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    
        Private Sub TextBox9_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox9.MouseDown
            TextBox9.DoDragDrop(TextBox9, DragDropEffects.Copy)
        End Sub
    
        Private Sub TextBox10_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox10.MouseDown
            TextBox10.DoDragDrop(TextBox10, DragDropEffects.Copy)
        End Sub

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,397

    Re: Cannot move dropped textbox

    Nothing is "becoming locked". You aren't moving anything. In the PictureBox3_DragDrop event handler, you aren't changing the Location of the TextBox that was dragged, which would be moving it. You're creating a new TextBox and adding that to the PictureBox, which would be considered a copy operation. The thing is, you're not handling any events of that new TextBox, so you can't drag it to drop it.

    If you really want to move a TextBox then move it and everything will keep working. If you want to create copies and be able to drag'n'drop them then you need to handle their events, which you do using the AddHandler statement when you create objects at run-time. If you do that, be sure to use RemoveHandler when you're done with the objects and also be sure to dispose them.

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: Cannot move dropped textbox

    Dear jmchilhinney,
    I understood perfectly what is the behavior. Thanks for the explanation.
    I’ll have a look around to find examples and in case I’ll get back. In case you have any, please send me the link.

    Thanks,
    A.

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: Cannot move dropped textbox

    Dear jmchilhinney,
    this is what I came up with - it is working as expected, please let me know if you have any further suggestion to improve it.

    Code:
    Private Sub Form11_Load(sender As Object, e As EventArgs) Handles Me.Load
            PictureBox3.AllowDrop = True
    
        End Sub
    
    
        Private Sub PictureBox3_DragDrop(sender As Object, e As DragEventArgs) Handles PictureBox3.DragDrop
    
            Dim tb As TextBox = DirectCast(e.Data.GetData(GetType(TextBox)), TextBox)
            Dim t1 As New TextBox
    
            t1.Location = New Point(MyPanel2.PointToClient(Control.MousePosition).X - Me.PictureBox3.Left,
                                    MyPanel2.PointToClient(Control.MousePosition).Y - Me.PictureBox3.Top)
            t1.Text = tb.Text
            t1.Font = tb.Font
            t1.TextAlign = HorizontalAlignment.Center
            t1.ForeColor = tb.ForeColor
            t1.BackColor = tb.BackColor
    
            PictureBox3.Controls.Add(t1)
            For Each tbd As TextBox In PictureBox3.Controls.OfType(Of TextBox)()
                AddHandler tbd.MouseMove, AddressOf textboxes_MouseMove
            Next
    
        End Sub
    
        Private Sub PictureBox3_DragEnter(sender As Object, e As DragEventArgs) Handles PictureBox3.DragEnter
            If e.Data.GetDataPresent(GetType(TextBox)) And CBool(e.KeyState And 8) Then
                e.Effect = DragDropEffects.Copy
            Else
                e.Effect = DragDropEffects.None
            End If
        End Sub
    
        Private Sub TextBox9_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox9.MouseDown
            TextBox9.DoDragDrop(TextBox9, DragDropEffects.Copy)
        End Sub
    
        Private Sub TextBox10_MouseDown(sender As Object, e As MouseEventArgs) Handles TextBox10.MouseDown
            TextBox10.DoDragDrop(TextBox10, DragDropEffects.Copy)
        End Sub
    
        Const HT_CAPTION As Integer = &H2
        Const WM_NCLBUTTONDOWN As Integer = &HA1
    
        Private Sub textboxes_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim tbd As TextBox = DirectCast(sender, TextBox)
                tbd.BringToFront()
                tbd.Capture = False
                Me.WndProc(Message.Create(tbd.Handle, WM_NCLBUTTONDOWN, CType(HT_CAPTION, IntPtr), IntPtr.Zero))
            End If
        End Sub
    Thanks,
    A.

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,397

    Re: Cannot move dropped textbox

    That's part of the way there but you shouldn't be using a loop to add the handler. If you do that then you'll be adding the same handler to each previous TextBox again. The new TextBox you just created is assigned to t1 so you only need to use AddHandler for t1. Also, you have no RemoveHandler anywhere. When you're done with the TextBoxes you created, be that when the form closes or some other time, you should be using RemoveHandler once for each AddHandler.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    22,304

    Re: Cannot move dropped textbox

    Code:
    Const HT_CAPTION As Integer = &H2
    Const WM_NCLBUTTONDOWN As Integer = &HA1
    
    Private Sub textboxes_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Dim tbd As TextBox = DirectCast(sender, TextBox)
            tbd.BringToFront()
            tbd.Capture = False
            Me.WndProc(Message.Create(tbd.Handle, WM_NCLBUTTONDOWN, CType(HT_CAPTION, IntPtr), IntPtr.Zero))
        End If
    End Sub
    That's actually moving textboxes. It's not dragdrop

  7. #7

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: Cannot move dropped textbox

    Dear jmcilhinney,
    thanks for your suggestion, but actually I want exactly that behavior. Previously I inserted the RemoveHandler in the if statement in the textboxes_MouseMove routine, but the behavior was not the one that I wanted. I made different tries and the code I posed is giving me the desired effect.
    What I'd be interested in knowing is where I should place the Dispose statement as you originally suggested and what is the scope for it.

    Thanks for your insights,
    A.

  8. #8
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    749

    Re: Cannot move dropped textbox

    It is entirely possible for sloppy, inefficient, or bad code to give you a desired result, but that doesn't mean the code shouldn't be changed.

    Change these three lines:

    Code:
            For Each tbd As TextBox In PictureBox3.Controls.OfType(Of TextBox)()
                AddHandler tbd.MouseMove, AddressOf textboxes_MouseMove
            Next
    to just this:

    Code:
                AddHandler t1.MouseMove, AddressOf textboxes_MouseMove
    and test it, and then you can apologize to jmc for dismissing his correct suggestion to you.

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,397

    Re: Cannot move dropped textbox

    Quote Originally Posted by AndreB82 View Post
    Previously I inserted the RemoveHandler in the if statement in the textboxes_MouseMove routine, but the behavior was not the one that I wanted.
    Of course that wasn't the behaviour you wanted. I never suggested that it would be. I said that you use RemoveHandler when you're done with the TextBoxes. Why would you be done with the TextBoxes when the MouseMove event is raised? If you are removing individual TextBoxes from the form then that would be when you use RemoveHandler for those specific TextBoxes. If you're not done with any of the TextBoxes until the form closes then you use RemoveHandler on all of them when the form closes. It should be fairly obvious that you use AddHandler when you want to start handling an event and RemoveHandler when you want to stop. This is why I tell people to work out the logic first and then write the code. If you try to think in code then you will often fail. Think about the logic first, then write code to implement that logic. I can't imagine what logic would tell you that you want to stop handling events when the mouse moves over a control.

  10. #10

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: Cannot move dropped textbox

    Dear all,
    thanks a lot for all your precious inputs, but you don't have to think that what to you is obvious is the same for others.

    I know I can have bored you, but if I start to read words like "apologize", I think it's better to quit here. I don't have to apologize for anything, as I didn't offend anybody and I always replied politely.

    Thanks,
    A.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,397

    Re: Cannot move dropped textbox

    Quote Originally Posted by AndreB82 View Post
    you don't have to think that what to you is obvious is the same for others.
    It's your application so what needs to happen should be obvious to you. I'm not saying that what code to write should be obvious but what that code should be doing should be obvious because it's your application. If you don't know what you want your application to do, how can anyone else? Again, if you try to think in code then things won't be obvious but if you think in application logic and translate to code, then it is much clearer. Ask yourself, based on the logic of how your application works, when you no longer want to handle events for those TextBoxes created at run-time. Is it the first time the mouse cursor moves over them? I would hope that it would be obvious that you still want to react to events after that so it should be obvious that that is not when you use RemoveHandler. I specifically stated that you should use it when you are done with the TextBoxes so you simply need to ask yourself when you are done with the TextBoxes. Are you saying that, in terms of application logic, it's not obvious to you when you are done with those TextBoxes?

  12. #12
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    749

    Re: [RESOLVED] Cannot move dropped textbox

    You drag one textbox to your picturebox.
    A new textbox is created in PictureBox3, and is currently the only textbox in PictureBox3.
    Your For Next loop loops through all textboxes in PictureBox3 (which right now is one textbox) and for each textbox it does the AddHandler statement. The end result of this loop is that the newly created textbox has this handler added to it.

    You drag the next textbox to your picturebox.
    A new textbox is created in PictureBox3, making two textboxes in PictureBox3
    Your For Next loop loops through all textboxes in PictureBox3 (which now is two textboxes) and for each textbox it does the AddHandler statement. The end result of this loop is that the newly created textbox has this handler added once, and the initial textbox you dropped onto PictureBox3 now has this handler added twice.

    You drag the next textbox to your picturebox.
    A new textbox is created in PictureBox3, making three textboxes in PictureBox3
    Your For Next loop loops through all textboxes in PictureBox3 (which now is three textboxes) and for each textbox it does the AddHandler statement. The end result of this loop is that the newly created textbox has this handler added once, the textbox you added second has this handler added twice, and the initial textbox you dropped onto PictureBox3 now has this handler added three times.

    You drag the next textbox to your picturebox.
    A new textbox is created in PictureBox3, making four textboxes in PictureBox3
    Your For Next loop loops through all textboxes in PictureBox3 (which now is four textboxes) and for each textbox it does the AddHandler statement. The end result of this loop is that the newly created textbox has this handler added once, the textbox you added third has this handler added twice, the textbox you added second has this handler added three times, and the initial textbox you dropped onto PictureBox3 now has this handler added four times.

    etc., etc., etc.

    Good luck.
    Last edited by OptionBase1; Jun 16th, 2019 at 06:31 PM.

  13. #13

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: Cannot move dropped textbox

    I said I quit here.
    Sorry to have bothered you.

    Thank you

  14. #14
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    749

    Re: [RESOLVED] Cannot move dropped textbox

    You're welcome.

  15. #15

    Thread Starter
    Member
    Join Date
    Sep 2018
    Posts
    40

    Re: [RESOLVED] Cannot move dropped textbox

    Not so sure to be welcome, if I have to apologize without having offended anybody

  16. #16
    Fanatic Member
    Join Date
    Nov 2017
    Posts
    749

    Re: [RESOLVED] Cannot move dropped textbox

    If you're hung up on me suggesting you apologize to jmc, I'm genuinely sorry. It was said mostly in jest.

  17. #17
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,397

    Re: Cannot move dropped textbox

    Quote Originally Posted by AndreB82 View Post
    I said I quit here.
    Sorry to have bothered you.

    Thank you
    Let's not get all passive-aggressive here. If you want help, ask for it. If you don't like what someone says, ignore it. You seem to be getting rather offended for someone who insists they didn't offend anyone. You may not have intended to offend anyone, but that doesn't mean someone wasn't offended. I doubt that OptionBase1 was trying to offend you either, but it seems to have happened. See how offense is in the eye of the beholder? Some people consider me rude. I don't try to be rude specifically but I'm not too concerned if people find me that way at times. I'm not here to make friends. I'm here to provide the information that I think people need to become better developers. They may not always agree with my ideas of what they need but people often don't know what they need, just what they want, which is not always best for them. Often times, I do will make a barbed comment if someone does something silly or lazy, e.g. doesn't bother to read the documentation for a method they called that didn't work. The idea is that, the next time they find themselves in a similar situation, they'll remember how they felt reading that comment and try to avoid it this time. Some will avoid it by doing the right thing to begin with while others will keep doing the wrong thing but ask a question at a different forum. The latter people don't deserve help, in my opinion, so I'm not sorry to see them go. The site admins may disagree, of course. If I've offended, meh. It wasn't my intention but if it's the result, so be it.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width