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?
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
Re: Calling private subs in another private subs?
to run your Button1_click event:
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:
private sub doSomething
MsgBox("Hi All !")
end sub
vb Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
doSomething
End Sub
vb Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
doSomething
End Sub
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?
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"... :)
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
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?
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)
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?
Re: Calling private subs in another private subs?
Sorry I've pressed 2 times to post button =(
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.
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 :(
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.
Re: Calling private subs in another private subs?