Please Help me.

1. In a LoginForm1 two textbox are present and a button also.
2. first user enter username & password in these field of LoginForm1.
3. After enter username & password by click the login button in the loginForm1 and a new
form2 is open.
4. when the new form2 is open another child form3 is open along with this new form2.
5. I want to set a lable in the child form3.
6. I want to set the text of lable from the text of textbox in the LoginForm1.

so please help me in C# code. How to implement this.

Dani AI

Generated

Two clean, safe approaches that avoid exposing controls directly are: 1) pass the textbox text through constructors as you create the forms, or 2) give Form3 a public property that sets its label. already pointed out there are two ways — below are concise, ready-to-run C# WinForms examples and a few cautions to avoid common pitfalls.

Constructor-based (preferred for simple data):

// LoginForm1 (button click)
private void btnLogin_Click(object sender, EventArgs e)
{
    string user = txtUsername.Text;
    var f2 = new Form2(user);
    f2.Show();
}

// Form2
public partial class Form2 : Form
{
    private readonly string _username;
    public Form2(string username)
    {
        InitializeComponent();
        _username = username;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        var f3 = new Form3(_username);
        f3.Show(this);
    }
}

// Form3
public partial class Form3 : Form
{
    public Form3(string userName)
    {
        InitializeComponent();
        lblUser.Text = userName;
    }
}

Property-based (keeps a parameterless constructor):

// Form3
public partial class Form3 : Form
{
    public string UserName
    {
        get { return lblUser.Text; }
        set { lblUser.Text = value; }
    }

    public Form3()
    {
        InitializeComponent();
    }
}

// Form2_Load
var f3 = new Form3 { UserName = _username };
f3.Show();

Notes and cautions: do not make controls public — use constructor args or properties. If Form3 will be an MDI child, set f3.MdiParent before Show(). Set the label on the UI thread; if you ever update it from a background thread, use Invoke/BeginInvoke. Validate login data in LoginForm1 before passing it onward. These patterns keep code decoupled and easy to test.

Recommended Answers

All 3 Replies

Please refrain from creating duplicate posts

please help me.

We have helped you. You have been shown two ways to do this both in your original thread and your other thread related to showing a single instance of a form.
We are not here to write the code for you. We will give you advise and guidance but you need to do the work yourself, that's how you learn.
If there is something specific in the advise we have given you that you don't understand then please post in your original thread to tell us what you need explained.

Kindly mark this thread as solved so we can keep all the posts together in one thread.

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.