|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Button calling aother button's codeButton2's code, I want to call Button1's click event. I guess I need to pass parameters. The Button1_Click() statement is incorrect. So how does one call code contained in another button? Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MsgBox("I pressed a button") End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = "" Button1_Click() End Sub I corrected it by using Button1_Click(sender, e) in Button2's code. What exactly am I sending as a parameter to Button1's code? On 4/15/2010 10:48 AM, Salad wrote:
Show quoteHide quote > I'm just getting started. How does one call another button's code? In Sender is the button that was clicked. This can be useful if you have > Button2's code, I want to call Button1's click event. I guess I need to > pass parameters. The Button1_Click() statement is incorrect. So how does > one call code contained in another button? > > Private Sub Button1_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button1.Click > MsgBox("I pressed a button") > End Sub > > Private Sub Button2_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button2.Click > TextBox1.Text = "" > Button1_Click() > End Sub > > I corrected it by using > Button1_Click(sender, e) > in Button2's code. What exactly am I sending as a parameter to Button1's > code? one handler for several controls, and you need to check the control that was used. I assume your goal is something more significant, but you could have one handler for both buttons. Something like this: Private Sub Buttons_Click (ByVal sender as System.Object, _ ByVal e as System.EventArgs) Handles Button1.Click, Button2.Click Dim btn As Button = DirectCast(sender, Button) MessageBox.Show("You pressed button " & btn.Text) End Sub The EventArgs parameter can be significant for other controls and handlers, but in a button click can often be ignored. -- Mike Salad wrote:
Show quoteHide quote > I'm just getting started. How does one call another button's code? In You're sending an object that can be cast back to a 'Button', the sender > Button2's code, I want to call Button1's click event. I guess I need to > pass parameters. The Button1_Click() statement is incorrect. So how > does one call code contained in another button? > > Private Sub Button1_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button1.Click > MsgBox("I pressed a button") > End Sub > > Private Sub Button2_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button2.Click > TextBox1.Text = "" > Button1_Click() > End Sub > > I corrected it by using > Button1_Click(sender, e) > in Button2's code. What exactly am I sending as a parameter to > Button1's code? object is going to be Button2's object properties I suspect. I don't recall how to do a cast in VB but here is a C# take on it var button = (Button)sender; string buttext = button.text; If you were doing the cast to a button in Button1_Click normally by pressing Button1 on the screen, the Button1 is the sender object and you can address the properties of Button1 when you cast 'sender' to be a 'Button' type. Am 15.04.2010 16:48, schrieb Salad:
Show quoteHide quote > I'm just getting started. How does one call another button's code? In The msgbox in Button1_Click says you've pressed a button, but to be exact,> Button2's code, I want to call Button1's click event. I guess I need to > pass parameters. The Button1_Click() statement is incorrect. So how > does one call code contained in another button? > > Private Sub Button1_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button1.Click > MsgBox("I pressed a button") > End Sub > > Private Sub Button2_Click(ByVal sender As System.Object, _ > ByVal e As System.EventArgs) Handles Button2.Click > TextBox1.Text = "" > Button1_Click() > End Sub > > I corrected it by using > Button1_Click(sender, e) > in Button2's code. What exactly am I sending as a parameter to > Button1's code? it's Button1, which makes the difference in this case. If you change it to "I pressed Button1", you see why you shouldn't call a click event handler if nothing has been clicked. So my answer is: Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MsgBox("I pressed a button") 'or "Button1" :-) DoSomething() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = "" DoSomething() End Sub private sub DoSomething 'doing something end sub Or, if you really just want to output you've pressed a button: (note the change with the "Handles" clause) Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click MsgBox("I pressed a button") End Sub -- Armin Just FYI There is another way than already mentioned. You could call:
Button1.PerformClick But I have experineced that to be less reliable than simply calling the click routine directly like mentioned previously. Unless you do something with the arguments in Button2_Click, you can simply pass "Nothing": Button2_Click(Nothing, Nothing) Cheers, Johnny J. -----Ursprungligt meddelande----- Från: Salad [mailto:sa***@oilandvinegar.com] Anslaget den: den 15 april 2010 16:48 Anslaget i: microsoft.public.dotnet.languages.vb Konversation: Button calling aother button's code Ämne: Button calling aother button's code I'm just getting started. How does one call another button's code? In Button2's code, I want to call Button1's click event. I guess I need to pass parameters. The Button1_Click() statement is incorrect. So how does one call code contained in another button? Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click MsgBox("I pressed a button") End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click TextBox1.Text = "" Button1_Click() End Sub I corrected it by using Button1_Click(sender, e) in Button2's code. What exactly am I sending as a parameter to Button1's code? Greetings (from the Access NG :),
It sounds like you want to call the same function but with different parameters. If this is the case -- that is called function overloading. This is a feature of OOP (that is not available in VBA) where you can declare a function multiple times with the same name but with different parameters for each declaration. Here is an example: Private Sub btnOL1_Click(...) Handles btnOL1.Click Console.WriteLine(OLtest()) End Sub Private Sub btnOL2_Click(...) Handles btnOL2.Click Console.WriteLine(OLtest(123)) End Sub Private Sub btnOL3_Click(...) Handles btnOL3.Click Console.WriteLine(OLtest(#4/15/2010#)) End Sub Function OLtest() As String Return "test1" End Function Function OLtest(ByVal x As Integer) As Integer Return x End Function Function OLtest(ByVal d As Date) As Date Return d End Function This is useful for organization of large projects. Basically, VB.Net is kind of like VBA/VB6 on some major steroids. You still have a lot of VBA/VB6 features but they have added OOP to the mix. If you are working in a non-integrated development environment you will get used to this very quickly. Just know that in OOP the big features are Inheritance, Overloading, and Polymorphysm. Note: these features are fairly involved, and will require some reading to get the full benefit since these features include other features like Interfaces (how you can communicate between .Net and com), Base Classes (Inheritance), Abstract/Virtual classes (polymorphysm) and a ton of other stuff. HTH Rich *** Sent via Developersdex http://www.developersdex.com *** Rich P wrote:
> If one has the experience, expertise and wants the fast track to > This is useful for organization of large projects. Basically, VB.Net is > kind of like VBA/VB6 on some major steroids. You still have a lot of > VBA/VB6 features but they have added OOP to the mix. If you are working > in a non-integrated development environment you will get used to this > very quickly. > > Just know that in OOP the big features are Inheritance, Overloading, and > Polymorphysm. Note: these features are fairly involved, and will > require some reading to get the full benefit since these features > include other features like Interfaces (how you can communicate between > .Net and com), Base Classes (Inheritance), Abstract/Virtual classes > (polymorphysm) and a ton of other stuff. building enterprise level solutions, the DOFactory is a path to take. http://www.dofactory.com/Patterns/Patterns.aspx http://www.dofactory.com/Framework/Framework.aspx It's the best investment I have ever made to date. Rich P wrote:
> Greetings (from the Access NG :), Hi Rich:Greetings as well. > Kinda. In Access, the click event has no parameter. It's simply an > It sounds like you want to call the same function but with different > parameters. If this is the case -- that is called function overloading. > This is a feature of OOP (that is not available in VBA) where you can > declare a function multiple times with the same name but with different > parameters for each declaration. Here is an example: event for the clicking the button. So one could press buttons and if they had code like Call Button1_Click then the click event for Button1 is called/executed from another button's click event. Thanks for the overloading hint/feature/explanation. Brought back memories of Java. Sometimes I might have code in a button that does a certain task. A second button might do some additional work first but finish the process by running code in the first button. I was thrown slightly by needing to pass parameters to a click event. I could have had the code event in Button1 call a sub and in Button2 call the sub at the end but I figured simply calling Button1 would work as well. > Just know that in OOP the big features are Inheritance, Overloading, and Just finding the functions in Help is a chore. Ex: Left() and Right() > Polymorphysm. Note: these features are fairly involved, and will > require some reading to get the full benefit since these features > include other features like Interfaces (how you can communicate between > Net and com), Base Classes (Inheritance), Abstract/Virtual classes > (polymorphysm) and a ton of other stuff. don't seem to be about strings. Any good book you'd recommend? BTW, Thanks all to those that responded to my post. Appreciate your info. To add to "Rich P" and even if you don't want to call the same method with
different parameters it could best to split UI and features. That is you could so something such as : Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click DoThis() End Sub Private Sub Button2_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button2.Click DoThat() DoThis() End Sub Or even the call to DoThis could be done inside "DoThat" if it makes sense... The idea is that rather than to have calls from one UI event handler to another (which can quickly become unclear and tricky) they should all call into separate actions you have build in your app... Also it helps to keep how the app should react to an event in your UI Code and have the capabilites of your app exposed somewhere else in other classes... -- Patrice "Salad" <sa***@oilandvinegar.com> wrote in message May I offer another take on this? Forgive me if I've misunderstood, but news:SqidnX3XkNehulrWnZ2dnUVZ_gydnZ2d@earthlink.com... > So how does one call code contained in another button? your initial requirement can be met very simply by using the Events list in the Properties pane when Button 2 is selected. Use the dropdown list next to 'Click' and select 'Button1_Click'. Thus both buttons are handled by the same code. Now you can test "Sender". Here is a really braindead example (without even an 'else' in it): Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click If sender Is Button1 Then Label1.Text = "button 1" End If If sender Is Button2 Then Label1.Text = "button 2" End If End Sub This is much like has already been posted, but I wanted to show you how simple it is to "borrow" another button's event handler. Is this of any help? Steve Steve Thackery wrote:
Show quoteHide quote > I knew about the Handles Button1.Click, Button2.Click method. However, > "Salad" <sa***@oilandvinegar.com> wrote in message > news:SqidnX3XkNehulrWnZ2dnUVZ_gydnZ2d@earthlink.com... > >> So how does one call code contained in another button? > > > May I offer another take on this? Forgive me if I've misunderstood, but > your initial requirement can be met very simply by using the Events list > in the Properties pane when Button 2 is selected. Use the dropdown list > next to 'Click' and select 'Button1_Click'. > > Thus both buttons are handled by the same code. > > Now you can test "Sender". Here is a really braindead example (without > even an 'else' in it): > > > Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As > System.EventArgs) Handles Button1.Click, Button2.Click > > If sender Is Button1 Then > Label1.Text = "button 1" > End If > > If sender Is Button2 Then > Label1.Text = "button 2" > End If > > End Sub > > > This is much like has already been posted, but I wanted to show you how > simple it is to "borrow" another button's event handler. > > Is this of any help? > > Steve I did not know about the If sender Is Button1 Then validation. I like that.
How to set the Tab size in a TextBox Control
Bug outside IDE only in Release mode DateGridView NRE Error how to capture a photo from my web cam ? communication between two vb apps Refer to childs of user control. Re: JPG Processing in .NET Help for populate Chart How to show scroll bars only if actually required Pausing an application waiting for a thread to finish... |
|||||||||||||||||||||||