Join 150,137 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,227 people online right now. Registration is fast and FREE... Join Now!
So I'm trying to figure out the best way of doing this. I have some test code that I'll give at the end.
Working with dynamic button. So if i have 5 buttons created dynamicly, i can get alot of information from the button click of that button. You click a button on this app, it will show the name of the button.
But now I want, lets say button 4 to click at 4:45 pm today. Normally I would do something like compare a string to "4:45", and when true, then button 4.Perform_Click.
But button 4 is dynamic, so how do I call that button in my code. How can I address it. I know it will be there when the program is running, I just don't know how to get to it in code.
Now I do have the button names in a database, so I can get the name that I want from there. Just not sure how to tell it, "Do a perform click on button 4"
The code below is my test bed, so If you click the main button, it will create a button, and a timer. Click the button that was created, and it will show the button name.
Thanks!!
Rudy
CODE
Public Class Form1 Private lblPECounter As Integer = 0 Private lblPEArray As New ArrayList Public lblInm2 As Label Public btnInfo As String = ""
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim newbutton As New Button ' Dim lblPECounter As Integer = 0 Dim Rand As New Random ' Dim lblPEArray As New ArrayList
'LABEL FOR TMR Dim lbl As New Label Dim randTag As New Random lbl.Name = "lblTimer" + newbutton.Name 'lbl.Tag = 10 lbl.Tag = randTag.Next(5, 35) lbl.Top = 107 lbl.Left = newbutton.Left lbl.Text = lbl.Tag
End Sub Private Sub Buttons_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim btn As Button = sender ' btnInfo = btn.Text btnInfo = btn.Tag MsgBox(btn.Name.ToString)
End Sub
Private Sub TimerEvent(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim tmr As Timer Dim btnT As Button Dim lbl As Label '= CType(sender, Timer).Tag ' Dim i As Int32 = CInt(lbl.Tag) ''''''''''''''''Dim lblInm2 As Label
lbl.Tag = count lbl.Text = count.ToString If (count = 2) Then Dim tmr2 As New Timer tmr2.Interval = 1000 AddHandler tmr2.Tick, AddressOf tmrFlash_Tick tmr2.Start()
End If
'lbl.Text = i.ToString If (count = 0) Then
' tmr.Tag = btnInfo
btnT = Me.Controls(tmr.Tag.ToString)
' If btnT Is Nothing Then
'Else btnT.BackColor = Color.Aquamarine 'End If
Me.Controls.Remove(lbl)
tmr.Dispose() Else tmr.Enabled = True
' End If End If
End Sub
Private Sub tmrFlash_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) ''Handles tmrFlash.Tick Dim tmr As Timer
Dim lblCh As Label tmr = sender lblCh = Me.Controls("lblInmVF" + tmr.Tag)
If lblCh Is Nothing Then
ElseIf lblCh.Tag = True Then lblCh.BackColor = Color.Yellow lblCh.ForeColor = Color.Red lblCh.Tag = False Else lblCh.BackColor = Color.Red lblCh.ForeColor = Color.Black lblCh.Tag = True End If End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
use the following within the sub from which you want to call the button click (provided you have a proper sub called Button1_click which contains the code for what happens when that button is clicked)
Button1_Click(sender, e)
I'm pretty sure that's EXACTLY what you are after mate. I've looked up that one myself a few weeks ago hf
P.S: adams code looks orsm
This post has been edited by Ameel: 12 Jul, 2008 - 08:07 AM
"0" would be the name of the button, or it would be 1 or 2 and so on. But I get an syntax error that states, "Expression does not produce a value". So Am I using your code correctly?
Damage, The software will not let me call the newbutton.click directly. I would need to raise an event or something. I'm going to work with that a little more to see if I can get it to work. But I see what your idea is.
Ameel,
Using Button1_Click(sender, e) seems pretty cool.
I know what to put for the sender value, but what can I put for the e. If I look at the e value, it gives the me the X and Y cordinates of that button. Do I need to re put that in there, or can I put something else in there?
Thanks again for all you help!!
Rudy
This post has been edited by RudyVB.net: 12 Jul, 2008 - 11:28 AM
I know what to put for the sender value, but what can I put for the e. If I look at the e value, it gives the me the X and Y cordinates of that button. Do I need to re put that in there, or can I put something else in there?
You just put EXACTLY Button1_Click(sender, e).
What it does is that it just calls the EXISTING sub called Button1_click which contains the code for what happens when you click the button. So basically here's what you do:
Make a button in the form design. Double click it.
It will generate the following code
CODE
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'write stuff in here
End Sub
Next say there's another sub called event1 from which you want to call the button1_click function. Code is as follows
CODE
Public sub event1() 'write stuff in here Button1_Click(sender, e) End sub
That's all.
don't change sender to anything, don't change e to anything. It should just be sender and e.
Too easy, save the world!
This post has been edited by Ameel: 12 Jul, 2008 - 02:19 PM
Rudy.Net was after a way to simulating a click on button which is created through the code, if you look at GUI in the designer there are only 2 buttons, Button1 & Button2. When you click on Button1 it adds a new button to the form called "0" click again it adds another new button "1" etc
Rudy.Net wanted to know how to simulate a click on these created button and not the placed buttons. p.s. My code works for both case.
This post has been edited by AdamSpeight2008: 12 Jul, 2008 - 02:41 PM