1.11M Members

VB6 Create Command Buttons

 
0
 

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?

 
0
 

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.

 
1
 

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
 
2
 

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
 
0
 

Dear Trilok31,

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

 
1
 

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

 
0
 

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

Question Answered as of 3 Years Ago by trilok31, P.manidas and debasisdas
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: