Search code examples
excelvbatextboxlocking

Lock Textboxes after drag and drop


I have 20 textboxes in my userform, they are all locked, one is filled with text and the other ones are empty. I want to drag the text from one box to the other. I unlock the textbox with text and drag the text to another textbox an drop it there. After that I want to lock both textboxes again. The first problem is that the text is not dropped but copied and the second is that I really don't know how to lock the textboxes again after dropping the text.

Private Sub TextBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal DragState As MSForms.fmDragState, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
TextBox1.Locked = False
Effect = 2
End Sub
Private Sub TextBox2_BeforeDropOrPaste(ByVal Cancel As MSForms.ReturnBoolean, ByVal Action As MSForms.fmAction, ByVal Data As MSForms.DataObject, ByVal X As Single, ByVal Y As Single, ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
TextBox2.Locked = False
End Sub
Private Sub TextBox1_AfterUpdate()
TextBox1.Locked = True
End Sub
Private Sub TextBox2_AfterUpdate()
TextBox2.Locked = True
End Sub

Solution

  • The first problem is that the text is not dropped but copied

    the second is that I really don't know how to lock the textboxes again after dropping the text.

    Is this what you are trying? I have taken an example of 2 textboxes. Feel free to apply it accordingly to your project. Both the textboxes in the example below are locked. You do not need to unlock TextBox1.

    Option Explicit
    
    Dim ctl As Control
    
    Private Sub UserForm_Initialize()
        TextBox1.Text = "Sid"
        TextBox1.DragBehavior = fmDragBehaviorEnabled
        TextBox2.DragBehavior = fmDragBehaviorEnabled
    End Sub
    
    Private Sub TextBox1_BeforeDragOver(ByVal Cancel As MSForms.ReturnBoolean, _
                                        ByVal Data As MSForms.DataObject, ByVal X As Single, _
                                        ByVal Y As Single, ByVal DragState As MSForms.fmDragState, _
                                        ByVal Effect As MSForms.ReturnEffect, ByVal Shift As Integer)
        Set ctl = TextBox1
        TextBox2.Locked = False
    End Sub
    
    Private Sub TextBox2_AfterUpdate()
        If Not ctl Is Nothing Then ctl.Text = ""
        TextBox2.Locked = True
    End Sub
    

    In Action

    enter image description here