14 Replies - 13132 Views - Last Post: 28 March 2010 - 09:27 PM Rate Topic: -----

#1 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Automate a button click

Post icon  Posted 12 July 2008 - 08:06 AM

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


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


Is This A Good Question/Topic? 0
  • +

Replies To: Automate a button click

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Automate a button click

Posted 12 July 2008 - 08:10 AM

Moved to VB.NET so this can get the attention of the proper experts :)
Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Automate a button click

Posted 12 July 2008 - 09:02 AM

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


Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 28
  • View blog
  • Posts: 1,090
  • Joined: 05-June 08

Re: Automate a button click

Posted 12 July 2008 - 09:03 AM

could you not declare the button outside the method and reference it in the timer?

like

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
 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 July 2008 - 06:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Automate a button click

Posted 12 July 2008 - 09:03 AM

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 :P hf

P.S: adams code looks orsm

This post has been edited by Ameel: 12 July 2008 - 09:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 28
  • View blog
  • Posts: 1,090
  • Joined: 05-June 08

Re: Automate a button click

Posted 12 July 2008 - 09:04 AM

or use adams code cause, it just looks slick :P and he kinda knows what he's doing
Was This Post Helpful? 0
  • +
  • -

#7 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Re: Automate a button click

Posted 12 July 2008 - 11:57 AM

View PostPsychoCoder, on 12 Jul, 2008 - 08:10 AM, said:

Moved to VB.NET so this can get the attention of the proper experts :)



Thanks! My mistake. :D
Was This Post Helpful? 0
  • +
  • -

#8 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Re: Automate a button click

Posted 12 July 2008 - 12:27 PM

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

Adam,
If I do this

 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 July 2008 - 12:28 PM

Was This Post Helpful? 0
  • +
  • -

#9 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Re: Automate a button click

Posted 12 July 2008 - 12:33 PM

Nevermind Adam,

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

Thanks!

Rudy
Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Automate a button click

Posted 12 July 2008 - 02:40 PM

RudyVB.net, Does it work now?

Quote

P.S: adams code looks orsm
:)

Quote

or use adams code cause, it just looks slick :P and he kinda knows what he's doin
:bigsmile:

Quote

proper experts
:fool: :sarcasm:

This post has been edited by AdamSpeight2008: 12 July 2008 - 02:42 PM

Was This Post Helpful? 0
  • +
  • -

#11 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Automate a button click

Posted 12 July 2008 - 03:13 PM

View PostRudyVB.net, on 12 Jul, 2008 - 12:27 PM, said:

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

	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

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 July 2008 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#12 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2256
  • View blog
  • Posts: 9,444
  • Joined: 29-May 08

Re: Automate a button click

Posted 12 July 2008 - 03:40 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 July 2008 - 03:41 PM

Was This Post Helpful? 0
  • +
  • -

#13 Ameel  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 171
  • Joined: 19-June 08

Re: Automate a button click

Posted 13 July 2008 - 01:21 AM

Yah. I agree. Adams code is more appropriate and exhaustive. Mine just call up the sub for when button is clicked
Was This Post Helpful? 0
  • +
  • -

#14 RudyVB.net  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 03-May 08

Re: Automate a button click

Posted 13 July 2008 - 08:00 AM

View PostAdamSpeight2008, on 12 Jul, 2008 - 02:40 PM, said:

RudyVB.net, Does it work now?

Quote

P.S: adams code looks orsm
:)

Quote

or use adams code cause, it just looks slick :P and he kinda knows what he's doin
:bigsmile:

Quote

proper experts
:fool: :sarcasm:



Hi Adam!

Your code worked perfectly, just what I needed. I knew I was close with only needing a line or two of code.

Thanks to all who helped me on this!! :^:


Rudy
Was This Post Helpful? 0
  • +
  • -

#15 djlakshmi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 72
  • Joined: 22-March 10

Re: Automate a button click

Posted 28 March 2010 - 09:27 PM

View PostRudyVB.net, on 12 July 2008 - 07:06 AM, said:

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


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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1