|
-
Sep 13th, 2005, 10:09 PM
#1
Thread Starter
Lively Member
Different Forms
Let's say I have a TextBox1; on my MainForm which contains a value of 4.
I added a new WindowsForm and created a TextBox2 on my new form. How do I extract/get the value, 4 from my MainForm ?
thks,
FYRe
sOMEONE'S gONNA dO iT, wHY nOT yOU ?
-
Sep 13th, 2005, 10:19 PM
#2
Re: Different Forms
You can either have the first form set a value in the second form, or you can have the second form retrieve the value from the first form. In the first case the first form needs a reference to the second form. In the second case the second form needs a reference to the first form.
If you want to use the second case, you could leave the TextBox as Friend or change it to Public and then the second form can retrieve that value directly. My preference would be to declare the TextBox as Private and then create a Public method or ReadOnly property in the first form that returns the value, then have the second form call that method or property.
Last edited by jmcilhinney; Sep 13th, 2005 at 11:31 PM.
-
Sep 13th, 2005, 11:29 PM
#3
Thread Starter
Lively Member
Re: Different Forms
In short, I want to pass a value from my MainForm to my new WindowsForm. Could someone show me an example?
----------------------------------------
MainForm:
----------------------------------------
NewForm:
----------------------------------------
thks,
FYRe
sOMEONE'S gONNA dO iT, wHY nOT yOU ?
-
Sep 13th, 2005, 11:36 PM
#4
Re: Different Forms
There are two ways. You can define a method in the NewForm class that takes the desired value as an argument, and then call that method from the MainForm class. This method could be a constructor if you want. The other option would be to define a property in the NewForm class and set that property from the MainForm. I'm assuming you already know how to define and call a method, and if you're not familiar with defining properties then you can use the example I gave you in your previous thread. You wouldn't make the property ReadOnly in this case of course. In fact, you could choose to make it WriteOnly.
-
Sep 13th, 2005, 11:42 PM
#5
Thread Starter
Lively Member
Re: Different Forms
I'm terribly sorry, I don't quite understand all the jargons.
So, here, I attached some screenshots; a similar example of what I'm doing.
Referring to " Form2codes.JPG ",
I want the value of TextBox1 in Form 2 to be 4, as in Form1.
sOMEONE'S gONNA dO iT, wHY nOT yOU ?
-
Sep 13th, 2005, 11:59 PM
#6
Re: Different Forms
You could put a value to the constructor of your form2.
Expand the region windows form designer code
VB Code:
dim mynumber as integer<----your variable
'then put a parameter to your sub new
Public Sub New(number as integer)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
me.mynumber=number<---pass the value
'Add any initialization after the InitializeComponent() call
End Sub
'then you can have the value
'in your form2 load event
me.textbox1.text=mynumber.tostring()
'in calling your form2
dim f as new form2(number)
f.showdialog()
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
|