This must be real simple.
From form1, I call up form 2.
now, in form2, I'd like to MessageBox.Show the text in Form1's TextBox1.
I've obviously tried
VB Code:
MessageBox.Show Form1.Textbox1.Text
and that is wrong.
How do I access this property?
Printable View
This must be real simple.
From form1, I call up form 2.
now, in form2, I'd like to MessageBox.Show the text in Form1's TextBox1.
I've obviously tried
VB Code:
MessageBox.Show Form1.Textbox1.Text
and that is wrong.
How do I access this property?
OK, I created a module and declared both forms there. Then, it was pretty simple. The IDE complied, and I was happy.
:afrog:
Question: Is what I did the correct way?
check out this thread
http://www.vbforums.com/showthread.p...hreadid=288072
It's fine. The only problem I've found with declaring the forms in a module is when you close a form, you can't reopen it. I had a main form that could call different forms. The secondary forms would get disposed of when they were closed.
Someone did post a workaround to it, passing the calling form to the secondary form, but I'd reworked the form by then.
umm ive always handled forms in module and have never come across the problem of not being able to reopen a form, i know you need to be careful when dealing with forms because you can open them when they dont really exist, all i can really say at this moment in time is if you close it make the module variable nothing then try reopening it
so use onclosing event
Hi,
"This must be real simple.
From form1, I call up form 2.
now, in form2, I'd like to MessageBox.Show the text in Form1's TextBox1."
Unless I'm crazy the answer IS real simple.
try
In form1
VB Code:
Dim frm2 As New fclsTest frm2.TextBox2.Text = Me.TextBox1.Text frm2.ShowDialog()
in form2 Load event
VB Code:
MessageBox.Show(TextBox2.Text)
Hi,
You could also use:
In form1
VB Code:
Dim form2 as new fclsTest form2.MesBox(me.TextBox1.Text) form2.ShowDialog
In form 2
VB Code:
Public Sub MesBox(Message1 as String) messagebox.show(Message1) End Sub
I expect there are other ways also:wave:
Dear Mendhak, the only not strange way (because often I like to use very personal strategy;) ) I have successfully tested in your situation, is declare Form1 as public, in a module. Form2 should be visible, anyway, from Form1, being declared and instanced inside it and so, if you need only to do like similar at what you described, Taxes suggestion is good (always if I well understand :rolleyes: ):
Instance Form2 (inside Form1), then write into its controls or public variables what you need and then show it...or something similar. In this case you'd not need to put any form in a module.
Someone said to me a Form Child can access its father, but I've never used, nor verified, it.
Beg your pardon for my caotich english!
Well, I get an error when I try it, saying the form's already disposed.Quote:
Originally posted by carlblanchard
umm ive always handled forms in module and have never come across the problem of not being able to reopen a form, i know you need to be careful when dealing with forms because you can open them when they dont really exist, all i can really say at this moment in time is if you close it make the module variable nothing then try reopening it
so use onclosing event
Module: declare form1 and form2. Show form1
form1: show form2
form2: blah blah, click the little X close button, return to form1
form1: show form2 -> error, form2 is disposed
I know I could eliminate the X button, Hide instead of Close, etc. But it's an error I get.
Yes Salvelinus, if you close the form then you have to reinstance it, but now you do it locally (in Form1), not in a module, and the new instance will not be accessible from everywhere. Anyway this last condition should not be a problem for you. Stay valid that you can instance Form2 and at the next line you can write into it. Isn't it?
:confused:
I found it easier to just declare form2 in the form1 event that shows it. I don't really reference anything from one form on another that often.
Yes, I could also have passed the value to a public variable in Form2. I was wanting to avoid the hassle of having to do that though. Reminiscent of VB6 days :)
And thanks for your input, everyone.