Italbro5
asked on
VB Dynamic Button
Hi
i create a Dynamic Button : Form1.Controls.Add "VB.CommandButton", "Button1", Frame1
Dim oButton As VB.CommandButton
Set oButton = Form1.Controls("Button1")
With oButton
.Top = 33600
.Left = 3700
.Caption = "Save"
.Width = 2000
.Height = 500
.Visible = True
End With
now i wanna make him to something like that when i click on it
For Each ctl In Form2.Controls
If (TypeOf ctl Is OptionButton) Then
If ctl.Value = True Then
Print #ff, ctl.Caption
ctl.Value = False
End If
End If
Next ctl
so basicly my questins is :
How do i do a Button1_Click() with a dynamic button
thx
Italbro
i create a Dynamic Button : Form1.Controls.Add "VB.CommandButton", "Button1", Frame1
Dim oButton As VB.CommandButton
Set oButton = Form1.Controls("Button1")
With oButton
.Top = 33600
.Left = 3700
.Caption = "Save"
.Width = 2000
.Height = 500
.Visible = True
End With
now i wanna make him to something like that when i click on it
For Each ctl In Form2.Controls
If (TypeOf ctl Is OptionButton) Then
If ctl.Value = True Then
Print #ff, ctl.Caption
ctl.Value = False
End If
End If
Next ctl
so basicly my questins is :
How do i do a Button1_Click() with a dynamic button
thx
Italbro
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Private WithEvents btnObj As CommandButton
Private Sub Form_Load()
Set oButton= Controls.Add("VB.CommandBu tton", "oButton")
With oButton
.Top = 33600 ' are you sure of value, try 0 to start
.Left = 3700
.Caption = "Save"
.Width = 2000
.Height = 500
.Visible = True
End With
End Sub
private oButton_click
...
end sub
Private Sub Form_Load()
Set oButton= Controls.Add("VB.CommandBu
With oButton
.Top = 33600 ' are you sure of value, try 0 to start
.Left = 3700
.Caption = "Save"
.Width = 2000
.Height = 500
.Visible = True
End With
End Sub
private oButton_click
...
end sub
Oops, sorry, my browser did something weird.
Yes, that's the only rule you need to make it work
Yes, that's the only rule you need to make it work
ASKER
Dhaest dosent work
it does nothing when i click
look what u wrote :
Private WithEvents btnObj As CommandButton
private oButton_click
...
end sub
not the same name
it does nothing when i click
look what u wrote :
Private WithEvents btnObj As CommandButton
private oButton_click
...
end sub
not the same name
Just take a look at the first example I gave. There is the name the same !
I've question to ask Dhaest , no offense
If you can declare like that why not just add button to the form?
If you can declare like that why not just add button to the form?
Using the Controls.Add method for adding command buttons is not very useful for the reasons you note here: they have no code in their events. This is fine for labels and textboxes, I guess. But for buttons and menu items, your best bet is the "old school" method of creating a control array and loading additional members as necessary.
Example:
You have command button named cmdReadFile and assign its Index property Zero. Its purpose is to read the contents of a text file into some string buffer array that is a form-level string var. We assume there are potentially many of these files named SourceFile_01, SourceFile_02, etc and that you will create an element in the string array for each file. Maybe not very worthwhile, but an understandable design target.
When you look in click event, you will see that there is now an Index argument. So you can write code here in anticipation of the buttons you will add at runtime, like this:
Private Sub cmdReadFile( Index as integer )
dim nFileHandle as integer
dim sTemp as string
nFileHandle = FreeFile
Open sBaseName & Format$(Index + 1, "00") for input as #nFileHandle
Do while not EOF(nFileHandle)
Line Input #nFileHandle, sTemp
msFileContents(Index + 1) = msFileContents(Index +1) & sTemp
Loop
Close #nFileHandle
End Sub
When you know you how many files, you can just add the command buttons like this:
For i = 1 to intNumberNeeded - 1
Load cmdReadFile(i)
cmdReadFile(i).Top = cmdReadFile(i-1).Top + 630
' assume we will just be stacking the buttons here
' you would likely be changing the caption in real life, right?
cmdReadFile(i).Visible = True ' they're invisible by default
Next
ReDim msFileContents(0 to intNumberNeeded - 1) as String
... and you're ready to go
Example:
You have command button named cmdReadFile and assign its Index property Zero. Its purpose is to read the contents of a text file into some string buffer array that is a form-level string var. We assume there are potentially many of these files named SourceFile_01, SourceFile_02, etc and that you will create an element in the string array for each file. Maybe not very worthwhile, but an understandable design target.
When you look in click event, you will see that there is now an Index argument. So you can write code here in anticipation of the buttons you will add at runtime, like this:
Private Sub cmdReadFile( Index as integer )
dim nFileHandle as integer
dim sTemp as string
nFileHandle = FreeFile
Open sBaseName & Format$(Index + 1, "00") for input as #nFileHandle
Do while not EOF(nFileHandle)
Line Input #nFileHandle, sTemp
msFileContents(Index + 1) = msFileContents(Index +1) & sTemp
Loop
Close #nFileHandle
End Sub
When you know you how many files, you can just add the command buttons like this:
For i = 1 to intNumberNeeded - 1
Load cmdReadFile(i)
cmdReadFile(i).Top = cmdReadFile(i-1).Top + 630
' assume we will just be stacking the buttons here
' you would likely be changing the caption in real life, right?
cmdReadFile(i).Visible = True ' they're invisible by default
Next
ReDim msFileContents(0 to intNumberNeeded - 1) as String
... and you're ready to go
ASKER
QJohnson are you 100% sure u can add code for a boutton create diynamicly ???
If you'll notice, there isn't any "dynamic" code in this technique. It's just written in such a way that it anticipates being used by any number of newly-added members of the control array.
I am 100% sure that this works because I dynamically populate menus in many of my projects with this same technique.
I'll bet this codes can convince you:
1) start a new Standard exe project
2) open the code widow for the form in module view
3) select the code there (it should only be the declaration and end sub for Form_Load
4) paste the code below over it
5) double click a command button onto your form and position it near the top
6) set the button's index property to zero
7) hit F5 to run it and you'll see the new buttons and observe their behavior.
It's crude but it should give you ideas about how you want to do your own.
>>>>>>>> code to paste into new project follows: >>>>>>>>
Private Sub Command1_Click(Index As Integer)
If Index = 0 Then
MsgBox "I was here at design time"
Else
MsgBox "I was added, my index is: " & Index
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 5
Load Command1(i)
With Command1(i)
.Top = Command1(i - 1).Top + 630
.Caption = "I'm new - number: " & i
.Visible = True
End With
Next
End Sub
<<<<<<<< end of code to paste <<<<<<<<<<
For your own code, if you can't think of any way to use the index property to cause the right code to run, remember you can always be a bit less elegant and put a select case statement in the button's click event like this:
Private Sub cmdMine_Click (Index as Integer)
Select Case Index
Case 0
' code for the one available at design time
Case 1
' code for the first one you add
Case 2
' code for the second one you add
End Sub
This looks a lot more elegant if you declare some constants for these, of course, e.g.:
Private Const BA_READ_FILE As Integer = 0
Private Const BA_WRITE_FILE As Integer = 1
Private Const BA_DELETE_FILE As Integer = 2
(using BA as a cue that these are ButtonArray constants)
so that the case statement becomes -->
Select Case Index
Case BA_READ_FILE
' presumably code to read a file
Case BA_WRITE_FILE
.... etc.,
give it a try... it's really pretty easy stuff
Q
I am 100% sure that this works because I dynamically populate menus in many of my projects with this same technique.
I'll bet this codes can convince you:
1) start a new Standard exe project
2) open the code widow for the form in module view
3) select the code there (it should only be the declaration and end sub for Form_Load
4) paste the code below over it
5) double click a command button onto your form and position it near the top
6) set the button's index property to zero
7) hit F5 to run it and you'll see the new buttons and observe their behavior.
It's crude but it should give you ideas about how you want to do your own.
>>>>>>>> code to paste into new project follows: >>>>>>>>
Private Sub Command1_Click(Index As Integer)
If Index = 0 Then
MsgBox "I was here at design time"
Else
MsgBox "I was added, my index is: " & Index
End If
End Sub
Private Sub Form_Load()
Dim i As Integer
For i = 1 To 5
Load Command1(i)
With Command1(i)
.Top = Command1(i - 1).Top + 630
.Caption = "I'm new - number: " & i
.Visible = True
End With
Next
End Sub
<<<<<<<< end of code to paste <<<<<<<<<<
For your own code, if you can't think of any way to use the index property to cause the right code to run, remember you can always be a bit less elegant and put a select case statement in the button's click event like this:
Private Sub cmdMine_Click (Index as Integer)
Select Case Index
Case 0
' code for the one available at design time
Case 1
' code for the first one you add
Case 2
' code for the second one you add
End Sub
This looks a lot more elegant if you declare some constants for these, of course, e.g.:
Private Const BA_READ_FILE As Integer = 0
Private Const BA_WRITE_FILE As Integer = 1
Private Const BA_DELETE_FILE As Integer = 2
(using BA as a cue that these are ButtonArray constants)
so that the case statement becomes -->
Select Case Index
Case BA_READ_FILE
' presumably code to read a file
Case BA_WRITE_FILE
.... etc.,
give it a try... it's really pretty easy stuff
Q
ASKER
I dont know why it wasnt working
but now it does
thx
Italbro
but now it does
thx
Italbro
ASKER
the only thing i need to has is this :
Private WithEvents oButton As CommandButton
if i dont write this, does it mean it wont work ?