Call a form’s sub from a Sub Main()
-
I start my project with a module called SubmainController.vb which contains a Public Sub Main() In this public sub main I want to call a sub from another form in the project. I’ve tried the following without ant success. Dim Myform1 as new Form1 Call Myform1.LoadFromFile (property access my assign to the property or use its value Dim ProjectName as string “Web Project” Call ProjectName.Myform1.LoadFromFile (myform1 not a member of string) I though this would be simple. I appreciate help Thanks GregC Thanks, GregC
-
I start my project with a module called SubmainController.vb which contains a Public Sub Main() In this public sub main I want to call a sub from another form in the project. I’ve tried the following without ant success. Dim Myform1 as new Form1 Call Myform1.LoadFromFile (property access my assign to the property or use its value Dim ProjectName as string “Web Project” Call ProjectName.Myform1.LoadFromFile (myform1 not a member of string) I though this would be simple. I appreciate help Thanks GregC Thanks, GregC
How about making the
Sub
aPublic
method of a separate class, thus making it available to both forms without the need of instantiating either form unneccessarily. As an alternative, if the method is declaredShared
, it should be accessible even without creating an instance of the form, simply by callingForm.MethodName()
. Oh, and you don't need to use "Call" anymore. -
How about making the
Sub
aPublic
method of a separate class, thus making it available to both forms without the need of instantiating either form unneccessarily. As an alternative, if the method is declaredShared
, it should be accessible even without creating an instance of the form, simply by callingForm.MethodName()
. Oh, and you don't need to use "Call" anymore.Hi should have added that this is an event. Public Sub loadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadFromFile.Click is it possible to raise an event on another form from a sub main? i'm not quite sure how to code that. Would i need to change the above click event code and how would I call or raise the event from the sub main? And would rasing the event execute the click event? Thanks, GregC
-
Hi should have added that this is an event. Public Sub loadFromFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles loadFromFile.Click is it possible to raise an event on another form from a sub main? i'm not quite sure how to code that. Would i need to change the above click event code and how would I call or raise the event from the sub main? And would rasing the event execute the click event? Thanks, GregC
You can certainly do something like what you describe; here's what I did:
Module MainModule
Sub Main()
FormRaisingEvent.LoadFromFile(New Object, New EventArgs)
End Sub
End Moduleand the form's code looks like this:
Public Class FormRaisingEvent
Inherits System.Windows.Forms.Form' Windows Form Details omitted... Public Shared Sub LoadFromFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Hello!") End Sub End Class
The important things are 1) declare the
Sub
asShared
and 2) when you call the method of the form from somewhere else, pass it the parameters required in the method's signature. Let me know if this answers your question. -
You can certainly do something like what you describe; here's what I did:
Module MainModule
Sub Main()
FormRaisingEvent.LoadFromFile(New Object, New EventArgs)
End Sub
End Moduleand the form's code looks like this:
Public Class FormRaisingEvent
Inherits System.Windows.Forms.Form' Windows Form Details omitted... Public Shared Sub LoadFromFile(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MessageBox.Show("Hello!") End Sub End Class
The important things are 1) declare the
Sub
asShared
and 2) when you call the method of the form from somewhere else, pass it the parameters required in the method's signature. Let me know if this answers your question.