|
-
Jan 30th, 2004, 11:33 AM
#1
Thread Starter
Member
Multiple Forms
iv'e got two forms, the first is Mainform and second_form
wha i am trying to do, is to access a button on the mainform from the second form,,,,,,,
while running the second form, i managed to do
Dim mf as new mainform
mf.button1.text = " new text updated"
but i found out that i have now two mainform opened,,,,,
and i couldnt' manage to change the data from mainform1
so what u advise me to do in this situation,
thank for helping
-
Jan 30th, 2004, 11:50 AM
#2
Re: Multiple Forms
Originally posted by xyz777
iv'e got two forms, the first is Mainform and second_form
wha i am trying to do, is to access a button on the mainform from the second form,,,,,,,
while running the second form, i managed to do
Dim mf as new mainform
mf.button1.text = " new text updated"
but i found out that i have now two mainform opened,,,,,
and i couldnt' manage to change the data from mainform1
so what u advise me to do in this situation,
thank for helping
you have to understand how classes work. A form is a class. You need to have a reference to the form to be able to access it.
If you want to be able to access a form from another form, you can pass a reference to the form in the new constructor (have to change the constructor yourself), or you could just declare the forms publicly in a module. Something like this:
VB Code:
Module Module1
' Change Form1 to whatever you want
Public frm1, frm2 As Form1
Sub Main()
frm1 = New Form1
frm2 = New Form1
' runs your app with frm1
Application.Run(frm1)
End Sub
End Module
you can access frm1 and frm2 anywhere in your app if you put that code in a module
edit: umm make sure you change the startup object to the module. If you dont know how: right click on your project in the Solution Explorer, click on Properties. In the left hand make sure Common Properties/General is selected. in the left section choose Sub Main from the "Startup object" dropdown
Last edited by MrPolite; Jan 30th, 2004 at 11:53 AM.
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Jan 30th, 2004, 06:30 PM
#3
PowerPoster
Hi xyz777,
I read your question differently from Mr Polite. As I read it you are wondering why you have two different MainForms opened.
I suspect that you have used MainForm as the application start form, or at least have created an instance of it in a Sub Main procedure. Therefore, when you placed the code
"Dim mf as new mainform"
in the second form, you created a second and entirely separate instance of MainForm, causing confusion (to you not VB.NET).
Remove the above code and alter the call to
MainForm.button1.text = " new text updated" (assuming that MainForm is the name you hav declared)
This works provided that MainForm is still open.
For More involved aspects see:
http://www.vbforums.com/showthread.p...hreadid=276165
and for an unusual alternative:
http://www.vbforums.com/showthread.p...hreadid=276604
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 13th, 2004, 02:56 PM
#4
Lively Member
Sorry to drag this up out of the tar pits of time, but I'm haing a very similar problem that the suggested solution does not work for.
My particular line of code is:
VB Code:
Form1.TabControl1.SelectedIndex = Me.ListBox1.SelectedIndex
That is called from Form2 (me = Form2). I get the build error:
Reference to a non-shared member requires an object reference
And "Form1.TabControl1" is underlined in my code.
Form1 is where the application starts from and it calls Form2. Both instances are open and shown. Form2 is trying to set a value on Form1. What am I doing wrong?
-
Feb 13th, 2004, 03:51 PM
#5
Frenzied Member
Form2 doesn't know anything about Form1. It's sort of like trying to use integer variable x in sub A when it's declared in sub B.
The easiest solution would be to declare the forms in the module, as above.
-
Feb 13th, 2004, 05:10 PM
#6
Lively Member
I tried that:
VB Code:
Module Module1
Public PaddbFlag, lbflag As Integer
Public Frm1 As Form1
Public Frm2 As New Form2
Public Sub Main()
Frm1 = New Form1
Application.Run(Frm1)
End Sub
End Module
I get this error when I try to execute the code on form2:
An unhandled exception of type 'System.NullReferenceException' occurred in Test006.exe
Additional information: Object reference not set to an instance of an object.
How can I be sure that the program is starting in Sub Main and not in Form 1?
-
Feb 13th, 2004, 05:49 PM
#7
Frenzied Member
When you reference Form1 from Form2, don't call it Form1, call it Frm1, in your example. That's the name you gave it. Otherwise it's like saying
VB Code:
dim i as integer
For integer = 0 to yadayada...
-
Feb 13th, 2004, 07:31 PM
#8
PowerPoster
Hi,
Just to clarify what salvelinus siad;
When you create an instance of a form, that form exists ONLY under the name you gave it in the instancing.
e.g.
Where fcls1 and fcls2 are the forms, or more correctly the Form SubClasses, you created in the designer
In Module
Public frm1 as new fcls1
Public frm2 as new fcls2
In a sub or method in frm1 you refer to any object in frm2 as, e.g.
frm2.TextBox1
fcls1 and fcls2, your originally designed forms (subclasses) do not exist in your runtime application - one of the big differences from VB6.
Hope this helps.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Feb 13th, 2004, 07:50 PM
#9
Junior Member
Thanks everyone for replying.
BUT I found someone who explained it very understandably.
http://www.devcity.net/net/article.a...=multipleforms
This link has a zip file with full sample code.
Thanks,
-
Feb 16th, 2004, 03:12 PM
#10
Lively Member
Well, thanks to Dooglo's article, I was able to get it to work by overloading Form2's constructor method and by passing Form1 as an arguement, i.e. Dim frm2 As New Form2(Me). By passing the arguement to Form2's constructor, I was able to assign it to a variable. This gives Form2 a direct link to Form1. Worked like a charm.
But I'm still a bit hazy on this instanciation stuff. Here's where you guru's get to educate a relative noob.
Is there a way to assign a varible to an already existing instance of an object? Maybe I'm not using the right words. In the statement, "Dim frm2 As New Form2," not only are you creating a new instance of Form2, but you are also assigning a variable, frm2, to it, right? Or do I have this wrong?
Is "Dim frm2 As Form2" - notice the abscense of "New"- a valid statement? I guess that it would be. It creates the varible, frm2, but does not assign it to any particular instance of a Form2. Is there a way of assigning frm2 to an already existing instance of Form2?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|