Button click in VB automatically

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 33439 Views - Last Post: 04 August 2008 - 11:43 AM Rate Topic: -----

#1 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Button click in VB automatically

Post icon  Posted 01 August 2008 - 12:04 AM

I am trying to get one of the buttons on my form to be clicked automatically, when form loads, to basically initiate some of the things on the form. Thanks in advance
Is This A Good Question/Topic? 0
  • +

Replies To: Button click in VB automatically

#2 lewax00  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 13-January 08

Re: Button click in VB automatically

Posted 01 August 2008 - 12:43 AM

In the form's Load event handler add the code:

Button1_Click(Button1, EventArgs.Empty)

(where Button1_Click is the name of the button's Click handler, and Button1 is the name of the button)

Just like using any other sub.
Was This Post Helpful? 2

#3 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Re: Button click in VB automatically

Posted 01 August 2008 - 12:57 AM

It doesn't seem to work

i want this btn to be automa clicked
	Private Sub btnGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGreen.Click
		ci.circle(Pens.Green)
	End Sub


so when I use
btnBlue_Click(btnBlue, EventArgs.Empty)
it does nothing
I added this in the load form but still doesn't so anything

srry i meant

	Private Sub btnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlue.Click
		ci.circle(Pens.Blue)
	End Sub

Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

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

Re: Button click in VB automatically

Posted 01 August 2008 - 12:57 AM

why don't you just add the code,that runs when the button is clicked, to the form load event?
Was This Post Helpful? 0
  • +
  • -

#5 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Re: Button click in VB automatically

Posted 01 August 2008 - 01:07 AM

Basically its an event driven programming assignment ... also i am using vector graphic and I can't add anything on picturebox without clicking .. but in my assignment i have to show a vector graphic on picbox when form loads, not after a button is clicked

this is what I am trying to add to form load but it doesn't work ... but this same code woks if i put it in a btn click routine

gr.DrawEllipse(color, b - a, c - a, 2 * a, 2 * a)
gr.DrawEllipse(color, b - a + 40, c - a, 2 * a, 2 * a)
gr.DrawEllipse(color, b - a + 80, c - a, 2 * a, 2 * a)


and whn i click that btn
Was This Post Helpful? 0
  • +
  • -

#6 dineeshd  Icon User is offline

  • member icon

Reputation: 38
  • View blog
  • Posts: 619
  • Joined: 30-June 08

Re: Button click in VB automatically

Posted 01 August 2008 - 01:11 AM

I had found a similar thread, may be usefull for you..... :rolleyes:

http://www.dreaminco...wtopic57794.htm
Was This Post Helpful? 0
  • +
  • -

#7 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Re: Button click in VB automatically

Posted 01 August 2008 - 01:31 AM

i have similar problem but a bit diff ... i just want the program to do three lines of code it self probab through form_load but the thread you gave me talks only about getting from one action to another(action like click). Doesn't talk about load ...

This is what my teacher told me

You may just put a 'Display' or 'Start' button on your form to
force the form to display the circles for the first time.
Was This Post Helpful? 0
  • +
  • -

#8 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Button click in VB automatically

Posted 01 August 2008 - 01:36 AM

Use Button1.PerformClick() to click a button. This however does not work in form load because the button has not been created yet. You might try doing a me.show right after the Sub Form_Load().
I would suggest that anything to be set be done in a subroutine you can call from form load or button click

This post has been edited by WayneSpangler: 01 August 2008 - 01:42 AM

Was This Post Helpful? 0
  • +
  • -

#9 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Button click in VB automatically

Posted 01 August 2008 - 02:12 AM

After running a test using me.show at start does not work either.
However, a quirk of Net is if you link a picturebox image to a bitmap then it is presistance so try this code. I don't like to use PerformClick so the code uses a subroutine called Draw.
Imports System.Drawing
Imports System.Drawing.Drawing2D

Public Class Form1
	Dim bmp As Bitmap = Nothing

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Draw()
	End Sub

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Draw()
	End Sub

	Private Sub Draw()
		With PictureBox1
			bmp = New Bitmap(.ClientRectangle.Width, .ClientRectangle.Height, Imaging.PixelFormat.Format32bppArgb)
			Using gr As Graphics = Graphics.FromImage(bmp)
				gr.DrawEllipse(Pens.Red, PictureBox1.ClientRectangle)
				gr.DrawRectangle(Pens.Red, PictureBox1.ClientRectangle)
				gr.DrawLine(Pens.Red, 0, 0, .ClientRectangle.Width, .ClientRectangle.Height)
			End Using
			PictureBox1.Image = bmp
		End With
	End Sub
End Class

To see what I mean add the following button and click on it after form has loaded.
	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		With PictureBox1
			Using gr As Graphics = Graphics.FromImage(bmp)
				gr.DrawLine(Pens.Purple, 0, .ClientRectangle.Height, .ClientRectangle.Width, 0)
			End Using
			.Invalidate()
		End With
	End Sub

This post has been edited by WayneSpangler: 01 August 2008 - 02:22 AM

Was This Post Helpful? 0
  • +
  • -

#10 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Button click in VB automatically

Posted 01 August 2008 - 08:19 PM

Since you are not doing anything with the arguments, you can execute the event by using the following in your load event.

btnBlue_Click(Nothing, Nothing)


Please use :code: when posting your code.
Was This Post Helpful? 0
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Button click in VB automatically

Posted 01 August 2008 - 08:44 PM

WayneSpangle said:

Use Button1.PerformClick() to click a button. This however does not work in form load because the button has not been created yet.

You can us it in Form.Load


Simply write.
CType(Me.Controls(<Name Of Button>), Button).PerformClick()


This post has been edited by AdamSpeight2008: 01 August 2008 - 08:57 PM

Was This Post Helpful? 0
  • +
  • -

#12 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Re: Button click in VB automatically

Posted 01 August 2008 - 09:04 PM

NOthing is working

here is the code
Public Class ThreeRings



	'Define a new circle event class created
	Private WithEvents ci As CircleEvent = New CircleEvent

	'Define graphics to draw circles
	Dim gr As Graphics
	'Define the parameters of circles so they can se change easily
	Dim a As Integer = 50
	Dim b As Integer = 100
	Dim c As Integer = 100

	Private Sub ThreeRings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		'Change the name of the form and also initiates the graphics
		Me.Text = "Three Rings"
		gr = pbVectors.CreateGraphics


	End Sub

	Public Sub fillCircle(ByVal color As Pen) Handles ci.CircleColor
		'Indirectly handles button clicks through event
		'Draw three circles according to the color button clicked by user
		gr.DrawEllipse(color, b - a, c - a, 2 * a, 2 * a)
		gr.DrawEllipse(color, b - a + 40, c - a, 2 * a, 2 * a)
		gr.DrawEllipse(color, b - a + 80, c - a, 2 * a, 2 * a)
	End Sub

	Private Sub btnRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click
		ci.circle(Pens.Red)
	End Sub

	Private Sub btnBlue_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBlue.Click
		ci.circle(Pens.Blue)
	End Sub

	Private Sub btnGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGreen.Click
		ci.circle(Pens.Green)
	End Sub
End Class




Also there is an event class as well

Public Class CircleEvent


	Public Sub circle(ByVal color As Pen)
		RaiseEvent CircleColor(color)
	End Sub

	Public Event CircleColor(ByVal color As Pen)

End Class



I want one of the buttons red,blue,or green to be pushed automatically when form loads(and I have to do this I don't have anyother option unless someone comes up with something)
Was This Post Helpful? 0
  • +
  • -

#13 lewax00  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 13-January 08

Re: Button click in VB automatically

Posted 02 August 2008 - 08:10 PM

So I suppose something I didn't think to ask (just to be sure) is, do the buttons work normally? (when the user clicks them) Just in case the problem is elsewhere.
Was This Post Helpful? 0
  • +
  • -

#14 adnan0  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 23
  • Joined: 01-August 08

Re: Button click in VB automatically

Posted 02 August 2008 - 10:57 PM

To be honest here is what I want to put three rings in picturebox. And there would be 3 buttons on form to change the color of the ring. I also have to use event to change color when buttons are clicked, which you probably have figured out by now. But I couldn't find any way to display those rings when form is loaded. So I basically have to show those three rings when program starts. My prof told me one of the ways is to put a start button but make it initiate some how but refuses to tell how .. .that's why I am seeking for help.. I hope this helps
Was This Post Helpful? 0
  • +
  • -

#15 WayneSpangler  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 106
  • Joined: 22-March 08

Re: Button click in VB automatically

Posted 03 August 2008 - 02:27 AM

Start a new program, add a picturebox and button and add the following code.
Imports System.Drawing.Drawing2D

Public Class Form1

	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		Button1.PerformClick()
	End Sub

	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		Dim gp As New GraphicsPath
		gp.AddEllipse(PictureBox1.ClientRectangle)
		Using g As Graphics = PictureBox1.CreateGraphics
			g.DrawPath(Pens.Black, gp)
		End Using
	End Sub

End Class


Run it. You will not see anything. Click the button and you will see the oval. Minimize the form and then restore it. There is nothing in the picturebox because it is not presistent. That is why I said to use a bitmap. I thought a Show.Me would work but it does not. You have to make your drawing after the picturebox shows and even then you would have to do a perform click on restoring after minimizing. Then you have the problem of bring up a program over yours. When you go back to yours the picturebox will be empty.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2