I have a login form(Form1). Which has two text box(textBox1,textBox).
when i clicked the Login Button another new form(Form2) is opened.This new form is
a MDIPARENT form. I want to set the text of a lable of a new child form of this parent
form which is same as the text of TextBox1 of Form1. Please Help me in C# code.

Dani AI

Generated

Short summary: wants the TextBox value from the login form (Form1) to appear in a label on a new MDI child of Form2. Both 's property approach and 's "pass the form reference" work; here are cleaner, low-coupling patterns and a few practical tips.

Constructor-injection (clean, simple):

public partial class ChildForm : Form
{
    public ChildForm(string labelText)
    {
        InitializeComponent();
        label1.Text = labelText;
    }
}

Create the MDI parent and the child from Form1 (pass the login text directly):

var mdi = new Form2();        // make/show the MDI parent
mdi.IsMdiContainer = true;
mdi.Show();

var child = new ChildForm(textBox1.Text);
child.MdiParent = mdi;       // set parent before showing
child.Show();

Alternative: hand the login text to the MDI parent and let it create children (useful if the parent manages many children):

public Form2(string loginName) { InitializeComponent(); _loginName = loginName; }

private void ShowChild()
{
    var c = new ChildForm(_loginName);
    c.MdiParent = this;
    c.Show();
}

Practical notes and pitfalls:

  • Prefer constructor parameters or thin public methods/properties over exposing controls (don’t make controls public).
  • Set MdiParent before calling Show() to avoid layout glitches.
  • Pass the text before closing/hiding the login form. If forms run on different threads, marshal to the UI thread.
  • ’s property solution is fine for quick apps; ’s reference-passing works but increases coupling. Constructor injection keeps dependencies explicit and makes testing easier.

Recommended Answers

All 2 Replies

Hello Bijaya123,
this is my variant:
You have to add to your form2 two public properties (for example Field1 and Field2).

public string Field1{get;set;}
public string Field2{get;set;}

When you open Form2 you have to add values to properties:

Form2 frm = new Form2{Field1 = TextBox1.Text, Field2=TextBox2.Text};
....

After it you can use Field1 and Field2 for setting Labels or another controls.

Hope it help you

Another way to do it is to pass a reference to the First form into the Second so that the second form can access public memebers of the first.
My tutorial outlines how to acheive this.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.