sqdprogrammer
asked on
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(pan elNo).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
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(pan
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
Form1.UpDate_Panels(1, "Analyzing Data")
works fine.
works fine.
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?
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,. ;)
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,. ;)
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
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
ASKER
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?
Kinger
Could i see more of the code you used. I tried Form1.UpDate_Panels(1,"An.
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?
ASKER
I just read the last comment, i am using VB.NET 2003
Which version of vs are you using sqdprogrammer ?
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THANK YOU ALL
thank you for the grade
NY
NY
yes, ur a master now newyuppie !
Bring in the cakes tomorrow ...!
Bring in the cakes tomorrow ...!
ASKER
Do you two by chance know each other?
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
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
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(par
2) if Form1 is owner of Form2
Dim MyParent as Form1 = DirectCast(Me.Owner, Form1)
MyParent.Update_Panels(par
et cetera
the Update_Panels method in form 1 should be declared Public for this to work.