Results 1 to 5 of 5

Thread: [RESOLVED] calling a control from within the code to operate it

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Resolved [RESOLVED] calling a control from within the code to operate it

    I'm trying to figure out how I would call a function within a function within VB Net. In VBA this is easy, but I have not figured out what arguments I would place into (e) in the function call.

    Here is the VBA code, which is easy to call.
    Code:
    BTN_CREATE_TASK_Click()
    Now in VB this is what the calling function looks like.
    Code:
        Private Sub BTN_CREATE_TASK_Click(sender As Object, e As EventArgs) Handles BTN_CREATE_TASK.Click
    What do I fill (e) will before I call the function, or do I need to call it another way say SendMessage?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,852

    Re: calling a control from within the code to operate it

    Quote Originally Posted by FunkMonkey View Post
    I'm trying to figure out how I would call a function within a function within VB Net. In VBA this is easy, but I have not figured out what arguments I would place into (e) in the function call.

    Here is the VBA code, which is easy to call.
    Code:
    BTN_CREATE_TASK_Click()
    Now in VB this is what the calling function looks like.
    Code:
        Private Sub BTN_CREATE_TASK_Click(sender As Object, e As EventArgs) Handles BTN_CREATE_TASK.Click
    What do I fill (e) will before I call the function, or do I need to call it another way say SendMessage?
    If you want to call the event handler then for something as simple as a Click event you could just pass EventArgs.Empty as the click event doesn't expect anything useful there anyway.

    However, I personally wouldn't call event handlers from other bits of code. If there is something that needs to be called from multiple places then I would put the code in it's own method and then call that from the Click event, or any other place you need to call it from.

    If the other place you want to call this from is also a click event then you could have the one event handler respond to multiple events e.g.

    Code:
    Private Sub BTN_CREATE_TASK_Click(sender As Object, e As EventArgs) Handles BTN_CREATE_TASK.Click, Button2.Click

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Mar 2025
    Posts
    182

    Re: calling a control from within the code to operate it

    Quote Originally Posted by PlausiblyDamp View Post
    If you want to call the event handler then for something as simple as a Click event you could just pass EventArgs.Empty as the click event doesn't expect anything useful there anyway.

    However, I personally wouldn't call event handlers from other bits of code. If there is something that needs to be called from multiple places then I would put the code in it's own method and then call that from the Click event, or any other place you need to call it from.

    If the other place you want to call this from is also a click event then you could have the one event handler respond to multiple events e.g.

    Code:
    Private Sub BTN_CREATE_TASK_Click(sender As Object, e As EventArgs) Handles BTN_CREATE_TASK.Click, Button2.Click
    Thanks. Sounds like the best way to go is to create a seperate function. Much appreciated.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,796

    Re: calling a control from within the code to operate it

    That is considered good form.
    My usual boring signature: Nothing

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

    Re: calling a control from within the code to operate it

    It's worth noting that a Button control has a PerformClick method, so you can call that to have that Button raise its Click event and thus execute the event handler for that event. There generally aren't methods to raise events like that but Click for a Button is so common that they decided to add one for that. PD's suggestion is what you'll have to do for most other events, so you can do the same for a Button's Click event for consistency. As suggested, you should pretty much NEVER call an event handler directly in your own code.
    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

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