Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 187
  • Last Modified:

Unable to access methodes in primary form in VB.NET

Greeting

I have a question on accessing a methode in a form in VB.net.  What I would like to learn how to do is use a methode in form1.vb from form2.vb.  I will give a small example of what i would like to learn to do.  Please note that form1 would be the form that loads first when you start the VB program.

form1.vb:

Public Class form1
   Inherits System.Windows.Forms.Form

  <all the form setup methodes here> 'There is a StatusBar declared as StatusBar1

  Public Sub UpDate_Panels(ByVal PanelNo As Int, ByVal txtMyText As String)
  'Used to send text to the panels
        StatusBar1.Panels.Item(panelNo).Text = txtStr
        StatusBar1.Update()
   End Sub

End Class


Form2.vb

Public Class form2
   Inherits System.Windows.Forms.Form

  <all the form setup methodes here>

  Private Sub DoingSomething()
    UpDate_Panels(1, "Analyzing Data")
    <perform task to analyze the data here>
  End Sub

End Class

From the example above how do I get the methode 'DoingSomething()' in Form2 to be able to access the methode 'UpDate_Panels()' in form1?

I have played around with the Friend keys word with no luck, I was never able to see the methode UpDate_panels as accessable in Form2.  My reference book doesn't do a very good job on explaining the proper use of Friend, Public, Private, Protected, ect...

Thanks for any help

SqDProgrammer
0
sqdprogrammer
Asked:
sqdprogrammer
  • 5
  • 4
  • 4
  • +1
1 Solution
 
newyuppieCommented:
are any of those forms parent/child of the other? are any of those forms opened through the other (the Form.Show method).

1) If for example Form2 was an MDI child of Form1, the code in Form2 to call methods from Form1 would be something like:

Dim MyParent as Form1 = DirectCast(Me.MdiParent, Form1)
MyParent.Update_Panels(params)

2) if Form1 is owner of Form2
Dim MyParent as Form1 = DirectCast(Me.Owner, Form1)
MyParent.Update_Panels(params)

et cetera

the Update_Panels method in form 1 should be declared Public for this to work.

0
 
Kinger247Commented:
Form1.UpDate_Panels(1, "Analyzing Data")  

works fine.
0
 
newyuppieCommented:
kinger, but isnt there a statusbar being declared and initialized in Form1's code? if you dont DirectCast it, wouldnt you be using a new instance of the form and losing any stored values, etc?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kinger247Commented:
I just created a form with a button that opens up second form.

From the second form I added a button that when clicked runs a procedure in the first form "UpDate_Panels".
This procedure then updated a label control on the first form.

the code I used was : Form1.UpDate_Panels(1, "Analyzing Data")

Unless I'm missing something, it seems to work ok,. ;)
0
 
SanclerCommented:
There's a difference here between VB.NET 2005 on the one hand and VB.NET 2003 and 2002 on the other hand.

Kinger247's approach works in 2005 because that (like earlier, non-NET, versions of VB) allows you to refer to the CLASS Form1 as a single INSTANCE of Form1.  But for 2003 (and 2002) you need to declare an instance of the class Form1 before you can access any of its properties or methods and then you have to reference them via that instance.

Roger
0
 
sqdprogrammerAuthor Commented:
Greetings

Kinger

Could i see more of the code you used.  I tried Form1.UpDate_Panels(1,"An....") the otherday and just recieved an error that a had a reference to a non shared member.

Newyuppie

Form 1 runs a series of methods from a different module and if something goes wrong then a error handler methode opens the second form (form2) and form2 attempts to write to form1.  Does that help any?
0
 
sqdprogrammerAuthor Commented:
I just read the last comment, i am using VB.NET 2003
0
 
Kinger247Commented:
Which version of vs are you using sqdprogrammer  ?
0
 
Kinger247Commented:
Ok. :)

You'll need to create a reference then to the owner form like newyuppie stated earlier.

You can handle the error anywhere you want to, if not in form1 it will error in form2.
0
 
newyuppieCommented:
>> There's a difference here between VB.NET 2005 on the one hand and VB.NET 2003 and 2002 on the other hand.

Kinger247's approach works in 2005 because that (like earlier, non-NET, versions of VB) allows you to refer to the CLASS Form1 as a single INSTANCE of Form1.  But for 2003 (and 2002) you need to declare an instance of the class Form1 before you can access any of its properties or methods and then you have to reference them via that instance.

Roger
>>

thanks for the clarification roger. what if there are several instances of the form running, in 2005 to which one of them would the referred class point to? where could i read more about this?


sqdprogrammer,
on your Form1 error handler, when you open Form2, you could call the Show method like this

Form2.Show (Me), which will make Form2's owner Form1.

then you could try the code i sent earlier:

Dim MyParent as Form1 = DirectCast(Me.Owner, Form1)
MyParent.Update_Panels(params)

from Form2, wherever you need the method called.


0
 
sqdprogrammerAuthor Commented:
THANK YOU ALL
0
 
newyuppieCommented:
thank you for the grade

NY
0
 
Kinger247Commented:
yes, ur a master now newyuppie  !

Bring in the cakes tomorrow ...!
0
 
sqdprogrammerAuthor Commented:
Do you two by chance know each other?
0
 
SanclerCommented:
newyuppie

Here's a reference in the documentation

   http://msdn2.microsoft.com/en-us/library/1fsza1t2.aspx

As that explains, just one instance is created with direct reference to the class.  If you want any more, you have to create them in the normal way with As New.  So, when you call Form1.MyMethod it will be just the instance created (perhaps even by that call) with the direct reference to the class that will respond.  If you wanted the response to come from any other instances - e.g. after the declaration Dim newForm1 As New Form1 - you would have to call the method via that other instance - e.g. newForm1.MyMethod.

Roger  
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now