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
Printable View
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
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.
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
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.
I'm terribly sorry, I don't quite understand all the jargons. :confused:
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.
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()