1,105,416 Community Members

VB6 Create Command Buttons

Member Avatar
ben25x
Light Poster
45 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
debasisdas
Posting Genius
6,542 posts since Feb 2007
Reputation Points: 580 [?]
Q&As Helped to Solve: 476 [?]
Skill Endorsements: 25 [?]
Featured
 
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.

Member Avatar
P.manidas
Posting Whiz
309 posts since Oct 2008
Reputation Points: 39 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
trilok31
Newbie Poster
14 posts since Mar 2006
Reputation Points: 8 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
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
Member Avatar
P.manidas
Posting Whiz
309 posts since Oct 2008
Reputation Points: 39 [?]
Q&As Helped to Solve: 15 [?]
Skill Endorsements: 0 [?]
 
0
 

Dear Trilok31,

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

Member Avatar
trilok31
Newbie Poster
14 posts since Mar 2006
Reputation Points: 8 [?]
Q&As Helped to Solve: 4 [?]
Skill Endorsements: 0 [?]
 
1
 

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

Member Avatar
ben25x
Light Poster
45 posts since Dec 2010
Reputation Points: 12 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
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: