הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Wednesday, June 1, 2011 2:36 PM
While studying sample code for vb.net, I put the code in a button click event and need to click the button to see the results. It would be nice to have this code run by adding Button1_Click() to save time but I get an error because of the parameters needed for the Click event.
I get a suggested "Generate Method Stub for Button click event" which is probably a clue to the problem if I had any idea what that means.
Any suggestions on getting this to work will be appreciated.
Thanks,
Jerry (starting to get it but still confused at times)
All replies (13)
Wednesday, June 1, 2011 2:40 PM ✅Answered | 1 vote
Hi,
Use PerformClick
The Method Stub is this bit:>>
** Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click**
** End Sub**
Use the ComboBoxes at the top of your code window, select the item from the left one, such as Button1, TextBox1 or whatever.
Then select the EVENT you need the method stub for from the one on the right. :-)
Option Strict On
Option Explicit On
Option Infer Off
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
'Your code here:>>
'Example:
MessageBox.Show("Hi there!!")
End Sub
End Class
P.S: Instead of PerformClick()
you could use something like:>>
Button1_Click(Button1, Nothing)
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 2:46 PM ✅Answered | 1 vote
Don't use PerformClick method. It may not work in all situations depending on various factors like its location and whether it is inside any tab control etc.
If you are just unsure about the parameters to be passed to Button1_Click when calling from code, just pass Nothing.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1_Click(Nothing, Nothing)
End Sub
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com
Wednesday, June 1, 2011 2:47 PM ✅Answered | 1 vote
Or, if you have code that you want to execute both when the form loads and when the button is clicked, place that code in a separate method and call that method from both the Form.Load and Button.Click events:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DoSomething
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DoSomething
End Sub
Private Sub DoSomething()
'Your code here:>>
End Sub
End Class
Wednesday, June 1, 2011 2:53 PM ✅Answered | 1 vote
Or, if you have code that you want to execute both when the form loads and when the button is clicked, place that code in a separate method and call that method from both the Form.Load and Button.Click events:
Hi Blackwood,
Even the way I've shown it will work of course, you don't have to create a separate method. :-) ;-)
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 2:56 PM ✅Answered | 1 vote
Don't use PerformClick method. It may not work in all situations depending on various factors like its location and whether it is inside any tab control etc.
Hi pradeep1210,
I've never had any problems with a Button on a TabPage within a TabControl. :-) ;-) :-D
The buttons on my shirts occasionally come off though!! LOL!!
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 3:07 PM ✅Answered | 1 vote
Don't use PerformClick method. It may not work in all situations depending on various factors like its location and whether it is inside any tab control etc.
Hi pradeep1210,
I've never had any problems with a Button on a TabPage within a TabControl. :-) ;-) :-D
The buttons on my shirts ocasionally come off though!! LOL!!
Regards, John
Click this link to see how to insert a picture into a forum post.
Ok.. Then to verify what I'm saying, do the following.
1. Add a TabControl to your form. Name will be TabControl1 and will have 2 tab pages by default.
2. Add a button on TabPage1. Name will be Button1
3. Add a button to TabPage2. Name will be Button2
4. Add a button to the Form. Name will be Button3.
5. Add the following code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button1.PerformClick()
Button2.PerformClick()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Say Hi!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("Say Hello!")
End Sub
Note that our intention is to call both the message boxes one after the other.
6. Run the application and click Button3. Try to get both the message boxes one after the another. That won't ever happen. You will get only one of the two messageboxes depending on which tab is active. But we intended to invoke the click events of both buttons, isn't it?
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com
Wednesday, June 1, 2011 3:07 PM ✅Answered | 1 vote
Hi Blackwood,
Even the way I've shown it will work of course, you don't have to create a separate method.
Hi John. I completely agree, which is why I proposed your post as the answer. I usually use the separate method approach, but that is just personal preference,
Wednesday, June 1, 2011 3:46 PM ✅Answered | 1 vote
Hi Blackwood,
Even the way I've shown it will work of course, you don't have to create a separate method.
Hi John. I completely agree, which is why I proposed your post as the answer.
I usually use the separate method approach, but that is just personal preference,
Hi Blackwood,
Yes and I'm am grateful for that, thank you. :-D
Regards, John
Click this link to see how to insert a picture into a forum post.
Wednesday, June 1, 2011 4:32 PM ✅Answered
I tried each one of the suggestions seperately and then as shown below with all of them. The code should display two ellipses but they do not appear until I click the button. The code I used was:
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.PerformClick()
Button1_Click(Nothing, Nothing)
Button1_Click(Button1, Nothing)
DrawIt() 'has same code as button1_click
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim gr As Graphics = Canvas.CreateGraphics
Dim BluePen As Pen = New Pen(Color.Black, 1)
Dim YellowBrush As New SolidBrush(Color.Yellow)
gr.SmoothingMode = SmoothingMode.HighQuality
gr.DrawEllipse(BluePen, 10, 10, 200, 200)
gr.SmoothingMode = SmoothingMode.HighSpeed
gr.DrawEllipse(BluePen, 40, 40, 200, 200)
gr.Dispose()
End Sub
Sub DrawIt()
Dim gr As Graphics = Canvas.CreateGraphics
Dim BluePen As Pen = New Pen(Color.Black, 1)
Dim YellowBrush As New SolidBrush(Color.Yellow)
gr.SmoothingMode = SmoothingMode.HighQuality
gr.DrawEllipse(BluePen, 10, 10, 200, 200)
gr.SmoothingMode = SmoothingMode.HighSpeed
gr.DrawEllipse(BluePen, 40, 40, 200, 200)
gr.Dispose()
End Sub
Wednesday, June 1, 2011 4:36 PM ✅Answered | 1 vote
Jercook,
Put a breakpoint in and watch to see where it goes awry. At first glance, it looks like it should work but if you watch it line-by-line, it's sure to show you something.
Wednesday, June 1, 2011 4:40 PM ✅Answered
I put a breakpoint as you suggested and each time it jumped to the proper event or sub. The problem must be in drawing the ellipses and making them show because the code did execute as expected.
Thanks,
Jerry
Wednesday, June 1, 2011 4:41 PM ✅Answered | 1 vote
I put a breakpoint as you suggested and each time it jumped to the proper event or sub. The problem must be in drawing the ellipses and making them show because the code did execute as expected.
Thanks,
Jerry
Wow - that is weird.
For that, you might want to start another question but in essence you've proven that the answers you got here did execute the button click. I'd suggest that you mark all the appropriate answers and then, if you want, start a new thread.
Thursday, June 2, 2011 12:42 AM
Ok.. Then to verify what I'm saying, do the following.
1. Add a TabControl to your form. Name will be TabControl1 and will have 2 tab pages by default.
2. Add a button on TabPage1. Name will be Button1
3. Add a button to TabPage2. Name will be Button2
4. Add a button to the Form. Name will be Button3.
5. Add the following code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Button1.PerformClick() Button2.PerformClick() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox("Say Hi!") End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MsgBox("Say Hello!") End Sub
Note that our intention is to call both the message boxes one after the other.
6. Run the application and click Button3. Try to get both the message boxes one after the another. That won't ever happen. You will get only one of the two messageboxes depending on which tab is active. But we intended to invoke the click events of both buttons, isn't it?
Pradeep, Microsoft MVP (Visual Basic)
http://pradeep1210.wordpress.com
Hi pradeep1210,
Okay, I got that scenario to work like this instead. :-)
>>
Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Button1_Click(sender, e)
Button2_Click(sender, e)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MessageBox.Show("Say Hi!")
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox("Say Hello!")
End Sub
End Class
Regards, John
Click this link to see how to insert a picture into a forum post.