|
-
Aug 13th, 2014, 09:01 PM
#1
Thread Starter
Lively Member
[RESOLVED] Sub within a Sub
Hey there, people.
I have two subs:
Code:
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
and
Code:
Private Sub _11registernew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
and I need to run the BindingNavigatorAddNewItem_Click sub inside of the _11registernew_Load sub.
All help is appreciated.
Thanks!
-
Aug 13th, 2014, 09:07 PM
#2
Banned
Re: Sub within a Sub
 Originally Posted by bruel1999
Hey there, people.
I have two subs:
Code:
Private Sub BindingNavigatorAddNewItem_Click(sender As Object, e As EventArgs) Handles BindingNavigatorAddNewItem.Click
and
Code:
Private Sub _11registernew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
and I need to run the BindingNavigatorAddNewItem_Click sub inside of the _11registernew_Load sub.
All help is appreciated.
Thanks!
Just put:
vb.net Code:
BindingNavigatorAddNewItem.PerformClick()
in the Form Load event
Last edited by berny22; Aug 13th, 2014 at 09:10 PM.
-
Aug 13th, 2014, 09:08 PM
#3
Re: Sub within a Sub
Code:
Private Sub _11registernew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindingNavigatorAddNewItem_Click(sender, New EventArgs())
End Sub
-
Aug 13th, 2014, 09:34 PM
#4
Re: Sub within a Sub
 Originally Posted by dee-u
Code:
Private Sub _11registernew_Load(sender As Object, e As EventArgs) Handles MyBase.Load
BindingNavigatorAddNewItem_Click(sender, New EventArgs())
End Sub
Nope. It's very poor form to call an event handler directly. In cases where you want to simulate a button click, calling the appropriate PerformClick method is appropriate. In all other cases, the appropriate action is to take the common code out of either event handler, put it in a method of its own and then call that method from both event handlers.
Also, if you ever do want an EventArgs object, it's proper to use EventArgs.Empty rather than explicitly invoke a constructor.
-
Aug 13th, 2014, 10:29 PM
#5
Re: Sub within a Sub
Besides it's the wrong Sender too... the sender in the Form load would be the form, but the sender of a button click is the button. So to pass the form as the sender to a button click event handler would be very bad. Almost as bad as crossing the streams.
Now, another (and cleaner option in my opinion) is to move the code to a sub... then the load event handler and the form load event handlers would simply call that sub.
-tg
-
Aug 14th, 2014, 12:03 AM
#6
Re: Sub within a Sub
Posting that code even upon seeing the PerformClick suggestion, I expected to get some heat but I guess it is not that bad at all to show another way to skin a cat. It maybe inappropriate / bad but it can also perform the job, or am I wrong? :-) I don't want to derail this thread so off I go.
-
Aug 14th, 2014, 12:09 AM
#7
Re: Sub within a Sub
 Originally Posted by berny22
Just put:
vb.net Code:
BindingNavigatorAddNewItem.PerformClick()
in the Form Load event 
Yes, this is how I have called code with a button from a sub before.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Aug 14th, 2014, 12:22 AM
#8
Re: Sub within a Sub
 Originally Posted by dee-u
Posting that code even upon seeing the PerformClick suggestion, I expected to get some heat but I guess it is not that bad at all to show another way to skin a cat. It maybe inappropriate / bad but it can also perform the job, or am I wrong? :-) I don't want to derail this thread so off I go.
That code will probably do the job in this particular case but if there are better ways to do it, especially if they are no more trouble, then it's better to provide those ways only or at least indicate that they are better. No point getting someone new into bad habits that they then need to break later. There are certain situations where that code would cause issues too. If there are other options that avoid those issues then they would generally be preferable, especially when someone inexperienced might not understand the cause of the issue and therefore not be able to fix it without further help. Providing more than one way to do things is not necessarily bad but it can be a good way to confuse those less experienced, especially if an option is inferior in some way and that is not pointed out.
-
Aug 14th, 2014, 05:44 AM
#9
Re: Sub within a Sub
 Originally Posted by dee-u
Posting that code even upon seeing the PerformClick suggestion, I expected to get some heat but I guess it is not that bad at all to show another way to skin a cat. It maybe inappropriate / bad but it can also perform the job, or am I wrong? :-) I don't want to derail this thread so off I go.
Actually there could have been a lot wrong with it... what if the event handler for the button used sender to determine the button clicked, or some other critical information? Since you passed in form, the event handler would fail when it tries to cast the sender to a button. At that point, it's no good.
-tg
-
Aug 14th, 2014, 10:49 AM
#10
Thread Starter
Lively Member
Re: Sub within a Sub
 Originally Posted by berny22
Just put:
vb.net Code:
BindingNavigatorAddNewItem.PerformClick()
in the Form Load event 
Thanks!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|