-
[RESOLVED] *Ironic angry face* Dynamically creating methods for dynamically created buttons
Okay, I have this code.
Code:
Public Class Form1
Dim NPB As Button
Dim x As Integer
Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
Dim OpePro As String
OpenFileDialog1.ShowDialog()
OpePro = OpenFileDialog1.FileName
Process.Start(OpePro)
x = x + 25
NPB = New Button
NPB.Text = OpePro
NPB.Left = 5
NPB.Top = x
Controls.Add(NPB)
End Sub
End Class
As you can see, when I open a program from my menu, a new button will be created with text similar to this, "C:\Program ". The button it self will not do anything. How can I create the method at the same time the button it is for is created? Also how can I change the text do that it gives me, for example, CCleaner.exe instead of the whole target path? Also, I scream like a gerbil being hit on by a donkey in Tijuana.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
The link does not mention how to dynamically add a new function. By the way, I think I forgot to thank you in the other thread.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Okay, I got this far
Code:
Public Class Form1
Dim NPB As Button
Dim x As Integer
Dim OpePro As String
Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
OpenFileDialog1.ShowDialog()
OpePro = OpenFileDialog1.FileName
Process.Start(OpePro)
x = x + 75
NPB = New Button
NPB.Text = OpePro
NPB.Width = 75
NPB.Height = 62
NPB.Left = x
NPB.Top = 0
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button1_Click
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start(OpePro)
End Sub
End Class
When I create a second button this way to open a different program, the first button is overwritten
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
I've never done something similar to that, but my guess is that the reason your button is overwritten is that you use the same button variable
Therefore by assigning a new object to the variable, you loose the reference to the old one.
If you need a class variable for the button, try adding the button to a button list instead.
Something like
Code:
Dim myButtonList As List(Of Button)
......
Private Sub NewProgramToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewProgramToolStripMenuItem.Click
....
x = x + 75
dim NPB = New Button
NPB.Text = OpePro
NPB.Width = 75
NPB.Height = 62
NPB.Left = x
NPB.Top = 0
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button1_Click
myButtonList.Add(NPB) ' (remember to instantiate the list variable at one point)
End Sub
Whether it'll work I don't know because as I say I've not tried it myself, but off the top of my head - it might. :D
Also you can use other collection types if you like and they serve your purpose.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
That seems like I would have a redundant amount of code just to make other buttons. I was thinking more along the lines of a an array
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
In other words, I want to click on only one thing to create multiple buttons with separate functions.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
no progress with this
Code:
Public Class Form1
Dim OpePro(7) As String
Dim posl As Integer
Dim NPB As Button
Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
System.Diagnostics.Process.Start("taskmgr.exe")
End Sub
Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
System.Diagnostics.Process.Start("calc.exe")
End Sub
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
OpenFileDialog1.ShowDialog()
OpePro(0) = OpenFileDialog1.FileName
OpePro(1) = OpenFileDialog1.FileName
OpePro(2) = OpenFileDialog1.FileName
OpePro(3) = OpenFileDialog1.FileName
OpePro(4) = OpenFileDialog1.FileName
OpePro(5) = OpenFileDialog1.FileName
OpePro(6) = OpenFileDialog1.FileName
OpePro(7) = OpenFileDialog1.FileName
posl = posl + 75
NPB = New Button
NPB.Text = OpePro(0)
NPB.Width = 75
NPB.Height = 62
NPB.Left = posl
NPB.Top = 0
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button1_Click
NPB = New Button
NPB.Text = OpePro(1)
NPB.Width = 75
NPB.Height = 62
NPB.Left = posl
NPB.Top = 0
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button2_Click
For i = 0 To OpePro.Length - 1
Next
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro(0))
End Sub
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro(1))
End Sub
End Class
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Something like this?
Code:
For i = 0 To OpePro.Length - 1
NPB = New Button
NPB.Text = OpePro(i)
NPB.Width = 75 i
NPB.Height = 62
NPB.Left = posl + (posl * i) ' some calcs
NPB.Top = 0 + (posl * i) ' some more calcs
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button2_Click
Next
They could share the event handler
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
for this line
Code:
For i = 0 To OpePro.Length - 1
it says "object reference not set to an instance of an object"
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
how about an if statement?
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Make it PUBLIC instead of DIM
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
is this right?
Code:
Public Class Form1
Dim OpePro As String
Dim posl As Integer
Dim NPB As Button
Public i As Integer
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
OpenFileDialog1.ShowDialog()
OpePro = OpenFileDialog1.FileName
posl = posl + 75
For Me.i = 0 To OpePro.Length - 1
NPB = New Button
NPB.Text = OpePro(i)
NPB.Width = 75
NPB.Height = 62
NPB.Left = posl
NPB.Top = 0
Controls.Add(NPB)
AddHandler NPB.Click, AddressOf Button1_Click
Next
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro)
End Sub
Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
System.Diagnostics.Process.Start("taskmgr.exe")
End Sub
Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
System.Diagnostics.Process.Start("calc.exe")
End Sub
End Class
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
the second button overwrote the first one so I don't think that it's what you meant
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
You need an ARRAY of buttons...
Code:
Dim NPB(7) As Button
and a few (i)'s
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Like this?
Code:
Public Class Form1
Dim OpePro As String
Dim posl As Integer
Dim NPB(7) As Button
Public i As Integer
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
OpenFileDialog1.ShowDialog()
OpePro = OpenFileDialog1.FileName
posl = posl + 75
For Me.i = 0 To OpePro.Length - 1
NPB(i) = New Button
NPB(i).Text = OpePro(i)
NPB(i).Width = 75
NPB(i).Height = 62
NPB(i).Left = posl
NPB(i).Top = 0
Controls.Add(NPB(i))
AddHandler NPB(i).Click, AddressOf Button1_Click
Next
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro)
End Sub
Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
System.Diagnostics.Process.Start("taskmgr.exe")
End Sub
Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
System.Diagnostics.Process.Start("calc.exe")
End Sub
End Class
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Buttons are written to the same place.
add 80 or so each time
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Code:
NPB(i) = New Button
I get an error. index was outside to bounds on the array
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
should I make OpePro an array?
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Try something like this:
Code:
Dim btn as Button = New Button()
btn.name = "MyButton"
'.... some time later
Dim activeButton As Button = CType(Me.Controls("MyButton"), Button)
activeButton.Text = "Hooray"
Look at btn
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
I did that and when I make a second button, the first one disappears.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
also I just realized I excluded
Code:
btn.name = "MyButton"
because I didn't know where to put it.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
SUCCESS!
Code:
Public Class Form1
Dim OpePro(7) As String
Dim posl As Integer
Dim NPB_1 As Button
Dim NPB_2 As Button
Dim i As Integer = i = OpePro.Length - 1
Private Sub Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Add.Click
posl = posl + 75
NPB_1 = New Button
NPB_2 = New Button
If i = 0 Then
OpenFileDialog1.ShowDialog()
OpePro(i) = OpenFileDialog1.FileName
NPB_1.Text = i
NPB_1.Width = 75
NPB_1.Height = 62
NPB_1.Left = 0
NPB_1.Top = 0
Controls.Add(NPB_1)
AddHandler NPB_1.Click, AddressOf Button1_Click
i = i + 1
End If
If i = 1 Then
OpenFileDialog1.ShowDialog()
OpePro(i) = OpenFileDialog1.FileName
NPB_2.Text = i
NPB_2.Width = 75
NPB_2.Height = 62
NPB_2.Left = 75
NPB_2.Top = 0
Controls.Add(NPB_2)
AddHandler NPB_2.Click, AddressOf Button2_Click
i = i + 1
End If
End Sub
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro(0))
End Sub
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Process.Start(OpePro(1))
End Sub
Private Sub Task_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Task.Click
System.Diagnostics.Process.Start("taskmgr.exe")
End Sub
Private Sub Calc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calc.Click
System.Diagnostics.Process.Start("calc.exe")
End Sub
End Class
Now, how do I pause between if statements.
-
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
I will end this thread here. Thank you dglienna and Alsvha. You both really helped me with brainstorming.