Results 1 to 10 of 10

Thread: [RESOLVED] Sub within a Sub

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Resolved [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!

  2. #2
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Re: Sub within a Sub

    Quote Originally Posted by bruel1999 View Post
    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:
    1. BindingNavigatorAddNewItem.PerformClick()
    in the Form Load event
    Last edited by berny22; Aug 13th, 2014 at 09:10 PM.

  3. #3
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sub within a Sub

    Quote Originally Posted by dee-u View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    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.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  7. #7
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: Sub within a Sub

    Quote Originally Posted by berny22 View Post
    Just put:

    vb.net Code:
    1. 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

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Sub within a Sub

    Quote Originally Posted by dee-u View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Sub within a Sub

    Quote Originally Posted by dee-u View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Apr 2013
    Posts
    82

    Re: Sub within a Sub

    Quote Originally Posted by berny22 View Post
    Just put:

    vb.net Code:
    1. 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
  •  



Click Here to Expand Forum to Full Width