|
-
March 15th, 2011, 09:35 PM
#1
[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.
-
March 15th, 2011, 10:11 PM
#2
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
-
March 15th, 2011, 11:04 PM
#3
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.
-
March 15th, 2011, 11:30 PM
#4
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
-
March 16th, 2011, 01:07 AM
#5
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. 
Also you can use other collection types if you like and they serve your purpose.
-
March 16th, 2011, 01:38 AM
#6
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
Last edited by 957; March 16th, 2011 at 01:40 AM.
Reason: typo
-
March 16th, 2011, 01:50 AM
#7
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.
-
March 16th, 2011, 03:33 AM
#8
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
-
March 16th, 2011, 11:09 AM
#9
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
-
March 16th, 2011, 06:28 PM
#10
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"
-
March 16th, 2011, 06:54 PM
#11
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
how about an if statement?
-
March 16th, 2011, 08:24 PM
#12
Re: *Ironic angry face* Dynamically creating methods for dynamically created buttons
Make it PUBLIC instead of DIM
-
March 16th, 2011, 09:58 PM
#13
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
-
March 16th, 2011, 09:59 PM
#14
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
-
March 16th, 2011, 10:13 PM
#15
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|