Results 1 to 14 of 14

Thread: Calling private subs in another private subs?

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Calling private subs in another private subs?

    Hi all,
    I'm trying to call a private sub in another private sub. For example ;

    Code:
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click
            '// For example, When I click on the Form1
            '// I want to call Button1_Click Sub.
    
        End Sub
    
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox("Hi All !")
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            '// Or when I clicked Button1
            '// I want to run codes in the
            '// Form1.Load Sub.
    
        End Sub
    How can I do this?
    Last edited by canavaroski90; Jun 15th, 2009 at 10:47 AM. Reason: Correcting

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Calling private subs in another private subs?

    You just call the PerformClick method of the button if you want to run the code inside a button click event handler. This only works with buttons.
    Code:
    Button1.PerformClick()
    For a general method that will work with anything, you just move the code inside the event handler to its own method and call that method whenever you need to.
    For example, instead of having this:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MsgBox("Hi All !")
        End Sub
    You can simply rearrange your code to this:
    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            SayHello()
        End Sub
    
    Private Sub SayHello()
            MsgBox("Hi All !")
    End Sub
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,423

    Re: Calling private subs in another private subs?

    to run your Button1_click event:

    vb Code:
    1. Button1.performclick

    if you have code that you want to run in Form1_Load + elsewhere, you should put that code in a seperate sub + call that:

    vb Code:
    1. private sub doSomething
    2.     MsgBox("Hi All !")
    3. end sub

    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.        doSomething
    3. End Sub

    vb Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.        doSomething
    3. End Sub

  4. #4

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    Thx for replies. I already knew how to use subs and now I learned button1.performclick thx for this.

    However working on different tools (or compounds whatever you say..) it's getting more complicated I think. For exaple;

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            '/// I WANT TO CALL
            '/// Linklabel1.LinkClicked Sub
    
        End Sub
    
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            System.Diagnostics.Process.Start(e.Link.LinkData)
        End Sub

    "" System.Diagnostics.Process.Start(e.Link.LinkData) "" code is not running in another sub or function, because it's need value of variable "e". And you can't send value in main sub because main sub (Button1_Click) has already had same variable! How can i solve?
    Last edited by canavaroski90; Jun 15th, 2009 at 11:24 AM.

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Calling private subs in another private subs?

    Yeah, that's very complicated. How did you manage to have it done like that? I mean, the "invisible code"...
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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

    Re: Calling private subs in another private subs?

    you have to create an object of what ever type e is (System.Windows.Forms.LinkLabelLinkClickedEventArgs in this case), set the appropriate properties, then you can pass it to your "clicked" event....

    -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??? *

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    thx again for replies techgnome , I couldn't get it clearly, could you explain it in more detailed way?

    --By the way, could anyone explain me how can I run (or open) files instead of .exe files? As .jpg with Windows Fax And Picture Viwer or .txt files with Noepad?
    Last edited by canavaroski90; Jun 15th, 2009 at 12:02 PM.

  8. #8
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: Calling private subs in another private subs?

    Okay, I wouldn't do that (performClick stuff).

    Create a subroutine which takes a string (the hyperlink) as a parameter. Call this subroutine from the button click, passing in the hyperlink of your link label. Also, call this routine from the hyperlink click routine passing in the same parameter. Two events calling one subroutine - it's the most maintainable approach for any application.

    And call the sub something meaningful, e.g.

    OpenLinkInDefaultBrowser(hyperlink as string)
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    Thx SJWhiteley, but I'm just begginer and have poorly vb.net knowledge =( Could you show me your suggestions in an example please?

  10. #10

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    Sorry I've pressed 2 times to post button =(

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Calling private subs in another private subs?

    Double posting seems to be more common these days, so something was probably changed.

    One thing to point out is that a sub is a sub. There's nothing really special about event handlers, except that they have been passed to event publishers so that they are called when the event is raised. Other than that, they are just subs that take two arguments. The first argument is always an object, while the second argument can be all kinds of different things, but all of those things are derived from Object. Therefore, you can always call the sub and pass in Nothing, Nothing for the arguments. It's not a GOOD idea, but there is nothing fundamentally wrong with it.

    SJWhitelys suggestion is essentially what Paul posted in #3. The only difference would be the name of the sub, and the fact that it now takes an argument, which is a string. The code will otherwise be the same as what Paul posted.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    Thx for relpy Shaggy Hiker.
    I think I couldn't be much understandable. I'm making sub, and copying codes in it and I'm calling this sub from Button1_Click event. Bu the code -->""System.Diagnostics.Process.Start(e.Link.LinkData)"" needs variable "e".

    Code:
       Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Call link(   /////  What should I send ? ///// )
        End Sub
    
        Sub link(  ////  What should I get ?  //// )
            System.Diagnostics.Process.Start(e.Link.LinkData)
        End Sub

    I can't send anything about "e" from Button1_Click sub, so naturally Sub link() needs an "e" value.

    When I tried to set value of the "e" in Button1_Click sub like;

    Code:
    Dim e as System.Windows.Forms.LinkLabelLinkClickedEventArgs
    and tried to send "e" from Button1_Click to Sub link() it's warning me as ""e is already decleared as a parameter of this method""


    Then I tried to use another variable instead of "e", but I don't know what values have to be added in this variable. When i use this variable, naturally sub link() can't find anything in my new variable, and program gives runtime error.

    I couldn't cope with this situation

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Calling private subs in another private subs?

    Had you named the variable something other than e, it would have worked. The button click event had an argument named e, so you can't declare a different variable with the same name, as that would make two variables named e. The arguments are variables as far as the sub is concerned. It wouldn't make sense to do this:

    Dim e as String
    Dim e as Integer

    because you couldn't tell them apart. Therefore, if you had given that variable a different name, like LinkLabelName or some such, then it would have worked. You could then set the members that you needed, and call the other sub. After all, the name that you pass to an argument doesn't have to have the same name as the variable in the sub declaration:

    IF you have:
    Dim x as integer

    and a sub declared as this:
    Public Sub MySub(myInt as integer)

    then there is no problem with this:

    MySub(x)

    even though "x" is not "myInt"


    However, it would be better if you declared a sub that took a string as an argument:
    Code:
    Public Sub OpenLinkInDefaultBrowser(hyperlink as string)
      System.Diagnostics.Process.Start(hyperlink)
    End Sub
    Then called it from the button click event, and the link label click. Here's the link label event:

    Code:
        Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
            OpenLinkInDefaultBrowser(e.Link.LinkData)
        End Sub
    The button click event would be about the same, but I don't know what you would be passing in.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2009
    Posts
    62

    Re: Calling private subs in another private subs?

    Thx for all

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