Solved

Unable to access methodes in primary form in VB.NET

Posted on 2006-10-24
15
183 Views
Last Modified: 2012-05-05
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
Comment
Question by:sqdprogrammer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 13

Expert Comment

by:newyuppie
ID: 17797200
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
 
LVL 10

Expert Comment

by:Kinger247
ID: 17797277
Form1.UpDate_Panels(1, "Analyzing Data")  

works fine.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17797356
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
Industry Leaders: 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!

 
LVL 10

Expert Comment

by:Kinger247
ID: 17797372
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
 
LVL 34

Expert Comment

by:Sancler
ID: 17797408
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
 

Author Comment

by:sqdprogrammer
ID: 17797447
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
 

Author Comment

by:sqdprogrammer
ID: 17797469
I just read the last comment, i am using VB.NET 2003
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17797470
Which version of vs are you using sqdprogrammer  ?
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17797493
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
 
LVL 13

Accepted Solution

by:
newyuppie earned 500 total points
ID: 17797542
>> 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
 

Author Comment

by:sqdprogrammer
ID: 17797795
THANK YOU ALL
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17797840
thank you for the grade

NY
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17797869
yes, ur a master now newyuppie  !

Bring in the cakes tomorrow ...!
0
 

Author Comment

by:sqdprogrammer
ID: 17797894
Do you two by chance know each other?
0
 
LVL 34

Expert Comment

by:Sancler
ID: 17798115
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question