PDA

Click to See Complete Forum and Search --> : Call Public Subs from other forms


Gunnar
Oct 15th, 2002, 10:29 AM
Hi,
I'm just switched from VB 6 to VB.Net and tried to use a public sub at the MainForm from a second sub form. But it didn't appears in the property/method contents of the MainForm-form. So what do I have to do make the sub accessable from within other forms?
Thanks for your help.

Gunnar

Cander
Oct 15th, 2002, 10:36 AM
Put the sub in a class and call the class from both forms

dim blah as New Class1()

blah.MySub()

MrGTI
Oct 15th, 2002, 01:10 PM
What's wrong with just putting the code in a module?

Slow_Learner
Oct 15th, 2002, 05:01 PM
Don't know if it's some heinous .NET sin but I am also fond of sticking cross-form stuff in modules (nod to edneeis)

Hu Flung Dung
Oct 15th, 2002, 05:14 PM
Originally posted by Slow_Learner
Don't know if it's some heinous .NET sin but I am also fond of sticking cross-form stuff in modules (nod to edneeis)

No, its not a sin! If it makes your code easier to write/read/modify, then its what you should be doing!

Gunnar
Oct 16th, 2002, 01:57 AM
Ok, when I put the function into a module it can be used in both forms, but this function needs a control on form1 that's why I tried to use functions of form1 in form2, so I can start a function in the module but this function isn't able to do it's job.
Why should I generate a new object by using dim blah as new class()...? A public sub/function in an object have to be usable by any other objects no matter who generates this object and where this happens. When the program starts there is already an object of type Class, when I use the dim new... I end up with two form1's of the type class...

Gunnar

Edneeis
Oct 16th, 2002, 03:12 AM
If it uses a control on form1 then you'll have to pass the control into it.


'in module or other class
if its in a class make it shared
Public Sub ChangeText(ByVal newtext as string,ByRef txtbox as TextBox)
txtbox.Text=newtext
End Sub

'call from form1
ChangeText("Something New",Me.TextBox1)
'or if its in a class
ClassName.ChangeText("Something New",Me.TextBox1)

Edneeis
Oct 16th, 2002, 03:16 AM
another way to do it would be to have a setcontrol sub in the module that sets the module level control to the one of form1 and gets used in the changetext sub for all forms or what not that use it.


'in module

Public useTextBox as TextBox

Public Sub ChangeText(ByVal newText As String)
If Not useTextBox Is Nothing Then
useTextBox.Text=newText
End If
End Sub

'then set it in form1_load
useTextBox=Me.TextBox1

'then anyone can call it like this
ChangeText("Something Newer")

Gunnar
Oct 16th, 2002, 03:53 AM
Hi,
I used your second example and it works perfect. Thanks a lot.
But I can't imagine there is another way to address the control. If I'm in a sub in Form2 (while the ActiveX Control is in Form1) then Me. will address all controls and functions in Form2, is there another level above Me so I can address in a way like OverMe.Form1.ActiveXControl.Method?

Gunnar

Gunnar
Oct 16th, 2002, 06:16 AM
Concerning your first solution, that was what I tried, but when there's a public sub in Form1 and I try to call it in Form2 with command Form1.Subname there's no such method of Form1.

Gunnar

Edneeis
Oct 16th, 2002, 10:34 AM
It should work fine. I think the trouble you may be having is the naming and seperating an instance from the main class. If the form class is named Form1 then you wouldn't call it like Form1.MyMethod you have to use the instance that has been created and passed around like:


'in Form1 if you make an instance of Form2
dim frm as New Form2

'then you do NOT do
Form2.MyMethod()

'you use the instance
frm.MyMethod()

Gunnar
Oct 17th, 2002, 03:21 AM
Hi,
yes but with this I have two problems.

No.1 - When the program starts there's already an instance of
the MainForm created because of starting the program, but I don't know what's the name of this object is. So when I load in SubForm (Form2) in the program I have no idea how to address to MainForm object that already exists. And generating a new object by calling dim FormName as new MainForm in the SubForm would create a second object of type MainForm but I neither want nor need a second object of that type.

No.2 - I generated the SubForm with dim Form2 as new SubForm, but when I try to address functions with Form2.method, Form is unknow, when I use SubForm.method it works with all Public Shared declared functions.

Another Question is, what is practically the difference between Private, Public and Public shared declared functions? As far I understand it from the help Private functions are only accesable from within the class where they are defined, Public are accessible from any class and Public Shared is accessable from any class but doesn't have access to local control elements in the class where it's defined. But I can't call Public declared function from another class even not when I generate an object of that type and try to address object.publicfunction. Only when it's public shared it appears as method but with the problem of not being able to use local controls. So practically I see no differences if a sub is declared private or public.

Gunnar

Edneeis
Oct 17th, 2002, 04:13 AM
If you are in the Mainform that is started with your app then you can reference it with 'Me'. You shouldn't need a second mainform, all the other forms should be created by the mainform and it can pass itself to them with Me.

Private and Public you pretty much have right but Shared means it isn't tied to any instance so you don't need to make an object of the class and you can still use it. That is why those are the only ones you are seeing and why you had to use the actually Form class name.

Here is an example:

Gunnar
Oct 17th, 2002, 05:55 AM
Ok, that makes sense. Thanks a lot for the example.

Thanks,
Gunnar