Click here to Skip to main content
15,891,853 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Basically my system has login system..
so if password is correct it will proceed to new window and appear logged user name on the top of the corner.

i did like this?

C#
public partial class Form1 : Form
{
    // This is the text that will be entered in form2
    public String form2text;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Show form2
        Form2 objForm2 = new Form2(this);
        objForm2.ShowDialog();
        // When form2 is closed, update the label text on form1
        label1.Text = form2text;
    }
}

public partial class Form2 : Form
{
    // This is the instance of Form1 that called form2
    private Form1 form1caller;

    public Form2(Form1 form1caller)
    {
        InitializeComponent();
        this.form1caller = form1caller;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Pass the textBox value to form1 before closing form2
        form1caller.form2text = textBox1.Text;
        this.Close();
    }
}
Posted
Updated 30-Nov-14 22:02pm
v2
Comments
Mathew Soji 1-Dec-14 4:07am    
Check

http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
BillWoodruff 1-Dec-14 5:16am    
fyi: there are much better quality articles on CP on inter-form/class exchange. See the series by OriginalGriff.
syed shanu 1-Dec-14 4:24am    
This is the perfect answer :)

1 solution

You do know that your code ... as it is ... will work ? Technically, you have injected a reference to an instance of an object of Type 'Form1 into an instance of an object of Type 'Form2. So, the injected Type (Form1) has a public field, 'form2Text, that can, then, be set within the instance of 'Form2.

Note that by doing this you have exposed everything in the Form1 instance whose access modifier (declaration) is 'public or 'internal to the instance of Form2.

As the links suggested by other people responding on this thread show, there are other ways to achieve this.

If you wanted to stay with the model of "injection," you could, for example, pass the Label object on Form1 to the second Form: that reduces the possibility that what happens in the second Form could do something strange to the first Form.

However, I'd like to suggest to you there is a much better way to implement this based on the assumption that the intent of your design is to have a required UserName/Password entry dialog that is presented first, and requires validation, before the user ever sees (has access to) the Main Form.

If you want to see a code example of this model ... where the Main Form will not even be created unless the log-in is successful ... just ask.
 
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