Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how can i access a variable of one form to another using property.
I have modified this question............
i have a login form and then i created one sign in form for employee.
then i wrote two properties to access the username and id from the login form to the sign in form.
C#
public string getUname
       {
           get
           {
               return this.uname;
           }
           set
           {
               this.uname = value;
           }
       }
       #endregion

       #region // getUserid
       public string getUserid
       {
           get
           {
               return this.Userid;
           }
           set
           {
               this.Userid = value;
           }
       }

actually it worked.
following code on login page to assign values to property

SignIn signin = new SignIn();
signin.getUname = username;
signin.getUserid = id;

but then i added a MDI form within them means i opened MDI with correct login and then from MDI i am opening a sign in form and passing those variable values to database to check.
code on mdi form

private void MDI_EmployeeAttendance_Load(object sender, EventArgs e)
{
Login frmlogin = new Login();
SignIn signin = new SignIn();

uname = signin.getUname;
uid = signin.getUserid;
OR
uname=frmlogin.username;
uid=frmlogin.id;
}



but now the properties i have been written on sign in page are not getting the values from the login page. so what might be the problem i dont found after debugging. when i assign values from login form to property it assigns to it but when sign in form opens the property variables are null. so please help me...
Posted
Updated 10-Feb-12 1:51am
v2
Comments
E.F. Nijboer 9-Feb-12 9:28am    
Some code would be nice. But are you accessing the instance or class variables? How does the sign in form know about the login form? Wouldn't it be better to create another class that is responsible for the values and is passed to the login form and then passed to the sign in form?
[no name] 9-Feb-12 12:40pm    
plz be specific...why are mingling all the contents in one question...first tell us what is your main requirement...plz it would be nice..

You make the property public and access it via the form instance. If you need to do this in other methods, you make the form a member variable so you can get to it.

You also post code when you have a question so we can tell you how to fix it.
 
Share this answer
 
Comments
pietvredeveld 9-Feb-12 16:42pm    
Your solution will make the second form depending on the first. Better have a class responsible for the values as E.F. Nijboer suggested.
Christian Graus 9-Feb-12 17:05pm    
Well, to be honest, having a third class carry things like textbox values is insane. I'd do it with delegates, but that seems a little complex for someone who has to ask this. And yes, it seems obvious to me that one form is the main form and holds instances of the other forms that are shown.
This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900