From: Johnny J�rgensen on 15 Apr 2010 11:53 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:salad(a)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?
From: Patrice on 15 Apr 2010 12:07 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
From: Mr. Arnold on 15 Apr 2010 12:17 Rich P wrote: > > 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. If one has the experience, expertise and wants the fast track to 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.
From: Salad on 15 Apr 2010 14:00 Rich P wrote: > Greetings (from the Access NG :), Hi Rich: Greetings as well. > > 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: Kinda. In Access, the click event has no parameter. It's simply an 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 > 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. Just finding the functions in Help is a chore. Ex: Left() and Right() 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.
From: Steve Thackery on 15 Apr 2010 15:05 "Salad" <salad(a)oilandvinegar.com> wrote in message news:SqidnX3XkNehulrWnZ2dnUVZ_gydnZ2d(a)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
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to set the Tab size in a TextBox Control Next: Removing the beep |