I have just started learning Basic, and i know you can create command butons from the virtual environment, but how do you create (and, more importantly, let the user drag around) them from code?

Recommended Answers

All 6 Replies

You need to create a new instance of the command button and reposition it every time by changing the X , Y co-ordinates.

But may i know what is the requirement that make you drag the button around the form.

Try this one....

Creating a button

Private Sub Form_Load()
Dim Com As CommandButton
Set Com = Me.Controls.Add("vb.commandbutton", "BtnName")

    With Com
        .Left = 1000
        .Top = 3000
        .Width = 5000
        .Height = 1000
        .Visible = True
        .Caption = "My Button"
    End With
End Sub

Moving a button.....

Dim CmdX As Long
Dim CmdY As Long
Dim MoveBtn As Boolean


Private Sub Command1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
CmdX = X
CmdY = Y
MoveBtn = True
End Sub
Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveBtn Then
    Command1.Top = Command1.Top - (CmdY - Y)
    Command1.Left = Command1.Left - (CmdX - X)
End If
End Sub

Private Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveBtn = False
End Sub

Dear P.manidas little bit problem with you code is in first code u created command button, and with other code u are trying to drag a command button that is created in desgen mode, for event controlin you need to define command button in general property of project with clause WithEvents . it will go like this

Option Explicit 
Dim CmdX As Long
Dim CmdY As Long
Dim MoveBtn As Boolean
Dim WithEvents Com As CommandButton

Private Sub Form_Load()
Set Com = Me.Controls.Add("vb.commandbutton", "BtnName")

    With Com
        .Left = 1000
        .Top = 3000
        .Width = 5000
        .Height = 1000
        .Visible = True
        .Caption = "My Button"
        .DragMode = 0
    End With
End Sub

Private Sub Com_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
CmdX = X
CmdY = Y
MoveBtn = True
End Sub

Private Sub Com_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If MoveBtn Then
    Com.Top = Com.Top - (CmdY - Y)
    Com.Left = Com.Left - (CmdX - X)
End If
End Sub

Private Sub Com_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
MoveBtn = False
End Sub
commented: it fit the requirements on spot +1
commented: Nice +3

Dear Trilok31,

You are right sir, Thanks for modification. I think, now ben25x has got the solution for his thread. Thanks...

welcome P.manida
ben25x above code is tested and working this will satisfy your requirment
now you mark solved this post

commented: Well done +7

dood! thanks so much! that's exactly what I wanted! this is definitely solved. thanks again!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.