Pass Values between forms

prav_roy

Well-known member
Joined
Sep 10, 2005
Messages
70
Location
Mumbai
Programming Experience
1-3
hi,
i want to pass values of one form to another form,
ie, i have a form say Form1 in that i have two textboxes and a button when i press the button on some validation the values of textboxes should be passed to form2,
one way is to make use of constructors
ie. dim frm2 as new form2(val1,val2)
is this method is efficient or is there any other effective way to carry out this, can i make use of property, if so plz give me some tips
its window based application

Thank you
 
You need to stop thinking of forms as something special, as everyone seems to. A form is an object just like any other in .NET. If you want to pass data into ANY object then you need to either set a property or pass a parameter to a method, which includes a contructor. If you want to get data out of an object then you need to either get a property or call a function and get the return value. That is the case for all objects including forms.
 
so,
i have a Form1,Form2 and a class mgrClass
what i do is i have two textbox in form1,when i say submit i call mgrClass which consist property (to set my user name,password)
and now i try to acces the values stord in property from Form2 but its not working
am i doing anything wrong.. can i access property like this
thank u
 
It's not working? how do you mean? Have you declared an instance of form2 inside form1. Have you correctly set the modifier of the property to Friend or Public etc? A little more detail would be helpful.
 
You don't understand the concepts of object oriented programming properly. No shame in that. It's not something people get straight away. I suggest you do a bit of reading on the classes, objects and OOP. The link below is a good place to start.

http://www.startvbdotnet.com/oop/default.aspx
 
I attached a simple VB.NET (version 1.1) project which might be of assistance. Also as others have mentioned you should read up on object oriented program concepts.

Points of interest in the simple project.
  • Refer to Startup.vb which sets up a variable of type main form which allows you to access properties and objects of the main form. Also there is a simple class (may not be the best name though) which is used to gain access from a child form as demo'd in the child form "update" button.
  • I kept code fairly simple so it would be easy to read.
  • You do not need a constructor as you asked about.
This is not the only method of dealing with this issue, just one alternative.
 

Attachments

  • PassingValues.zip
    8.5 KB · Views: 67
Back
Top