Solved

Unable to access methodes in primary form in VB.NET

Posted on 2006-10-24
15
177 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
  • 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now