Welcome to Dream.In.Code
Become a VB.NET Expert!

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!




Automate a button click

2 Pages V  1 2 >  
Reply to this topicStart new topic

Automate a button click

RudyVB.net
12 Jul, 2008 - 07:06 AM
Post #1

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Hello All!

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

        newbutton.Name = lblPECounter.ToString
        newbutton.Text = "Text" + lblPECounter.ToString
        newbutton.Tag = lblPECounter.ToString

        Dim MinFont As New Font("arial", 12, FontStyle.Bold)
        newbutton.Font = MinFont


        newbutton.TextAlign = ContentAlignment.MiddleCenter



        ' newbutton.Width = 576

        newbutton.Height = 82
        newbutton.Left = Rand.Next(newbutton.Top, Me.Width - newbutton.Width)

        lblPECounter += 1
        newbutton.BackColor = Color.AliceBlue
        AddHandler newbutton.Click, AddressOf Buttons_Click
        Me.Controls.Add(newbutton)
        lblPEArray.Add(newbutton)
        ' MsgBox(newbutton.Text.ToString)


        '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

        Me.Controls.Add(lbl)

        Dim tmr As New Timer
        tmr.Tag = newbutton.Name

        tmr.Interval = 1000
        AddHandler tmr.Tick, AddressOf TimerEvent
        ' tmr.Tag = lbl

        tmr.Start()
        Dim C As Control = CType(sender, Control)
        'add label2
        Dim lblInmV As New Label

        lblInmV.Name = "lblInmVF" + newbutton.Name

        lblInmV.Tag = True

        lblInmV.Text = "Text2" + newbutton.Name

        lblInmV.Font = MinFont
        lblInmV.TextAlign = ContentAlignment.MiddleCenter
        lblInmV.AutoSize = True

        lblInmV.Location = New Point(newbutton.Left, C.Top + -340)
        ' lblInmV.Image = My.Resources.pvmetal2red
        lblInmV.BorderStyle = BorderStyle.Fixed3D
        lblInmV.ImageAlign = ContentAlignment.MiddleCenter
        lblInmV.BringToFront()

        Me.Controls.Add(lblInmV)

    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

        Dim count As Integer
        tmr = sender
        tmr.Enabled = False
        ' i -= 1
        'lblInm2 = Me.Controls("lblInmVF" + lblPECounter.ToString)
        lblInm2 = Me.Controls("lblInmVF" + tmr.Tag)
        lbl = Me.Controls("lbltimer" + tmr.Tag)
        'lbl.Tag = i
        count = lbl.Tag
        ' count = 10
        count -= 1

        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


    End Sub
End Class

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Automate A Button Click
12 Jul, 2008 - 07:10 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Moved to VB.NET so this can get the attention of the proper experts smile.gif
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Automate A Button Click
12 Jul, 2008 - 08:02 AM
Post #3

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
Here you go.
vb

CType(Me.Controls( <Name of button> ), Button).PerformClick()

User is offlineProfile CardPM
+Quote Post

Damage
RE: Automate A Button Click
12 Jul, 2008 - 08:03 AM
Post #4

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
could you not declare the button outside the method and reference it in the timer?

like

CODE

Public Class Form1
dim mybutton as new button


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      newbutton.Name = lblPECounter.ToString
        newbutton.Text = "Text" + lblPECounter.ToString
        newbutton.Tag = lblPECounter.ToString

        Dim MinFont As New Font("arial", 12, FontStyle.Bold)
        newbutton.Font = MinFont


        newbutton.TextAlign = ContentAlignment.MiddleCenter



        ' newbutton.Width = 576

        newbutton.Height = 82
        newbutton.Left = Rand.Next(newbutton.Top, Me.Width - newbutton.Width)

        lblPECounter += 1
        newbutton.BackColor = Color.AliceBlue
        AddHandler newbutton.Click, AddressOf Buttons_Click
        Me.Controls.Add(newbutton)
        lblPEArray.Add(newbutton)
        ' MsgBox(newbutton.Text.ToString)

    End Sub


and then in a timer that runs like everysooften check that it meets your condition and fire the event
CODE

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
if now.minute = 30 then
newbutton_Click(Me, e)

end if
    End Sub



looks kinda sketchy to me, but it should kinda work i think

This post has been edited by Damage: 12 Jul, 2008 - 05:51 PM
User is online!Profile CardPM
+Quote Post

Ameel
RE: Automate A Button Click
12 Jul, 2008 - 08:03 AM
Post #5

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 91



Thanked: 2 times
My Contributions
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 tongue.gif hf

P.S: adams code looks orsm

This post has been edited by Ameel: 12 Jul, 2008 - 08:07 AM
User is offlineProfile CardPM
+Quote Post

Damage
RE: Automate A Button Click
12 Jul, 2008 - 08:04 AM
Post #6

D.I.C Addict
Group Icon

Joined: 5 Jun, 2008
Posts: 754



Thanked: 7 times
Dream Kudos: 75
My Contributions
or use adams code cause, it just looks slick tongue.gif and he kinda knows what he's doing
User is online!Profile CardPM
+Quote Post

RudyVB.net
RE: Automate A Button Click
12 Jul, 2008 - 10:57 AM
Post #7

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

QUOTE(PsychoCoder @ 12 Jul, 2008 - 08:10 AM) *

Moved to VB.NET so this can get the attention of the proper experts smile.gif



Thanks! My mistake. biggrin.gif
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Automate A Button Click
12 Jul, 2008 - 11:27 AM
Post #8

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Thank you all for the suggestions. I do have a couple of questions

Adam,
If I do this

CODE
Private Sub TestClick()
         Dim testbtn As Button

      testbtn = CType(Me.Controls("0"), Button).PerformClick()

    End Sub

"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
User is offlineProfile CardPM
+Quote Post

RudyVB.net
RE: Automate A Button Click
12 Jul, 2008 - 11:33 AM
Post #9

D.I.C Head
**

Joined: 3 May, 2008
Posts: 61

Nevermind Adam,

I think I have it. I'm going to try it my my real project. I'll let you know!!

Thanks!

Rudy
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Automate A Button Click
12 Jul, 2008 - 01:40 PM
Post #10

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
RudyVB.net, Does it work now?

QUOTE
P.S: adams code looks orsm
smile.gif

QUOTE
or use adams code cause, it just looks slick tongue.gif and he kinda knows what he's doin
bigsmile.gif
QUOTE
proper experts
mrt.gif sarcasm.gif

This post has been edited by AdamSpeight2008: 12 Jul, 2008 - 01:42 PM
User is offlineProfile CardPM
+Quote Post

Ameel
RE: Automate A Button Click
12 Jul, 2008 - 02:13 PM
Post #11

D.I.C Head
**

Joined: 19 Jun, 2008
Posts: 91



Thanked: 2 times
My Contributions
QUOTE(RudyVB.net @ 12 Jul, 2008 - 12:27 PM) *


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?




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
User is offlineProfile CardPM
+Quote Post

AdamSpeight2008
RE: Automate A Button Click
12 Jul, 2008 - 02:40 PM
Post #12

LINQ D.I.C.
Group Icon

Joined: 29 May, 2008
Posts: 863



Thanked: 54 times
Dream Kudos: 2250
My Contributions
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
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 01:58AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month