Question

first off this may seem like a easy question but I'm really new at c# so bear with me :). I know this is probably a question that is already answered but searching on here and on google didn't really satisfy what i was looking for.

So I have a dialog form (login form) where user has to enter his password & username

private void loginButton_Click(object sender, EventArgs e)
{
    try
    {
        var sr = new System.IO.StreamReader("C:\\" + inlogNaamTextbox.Text + "\\Login.txt");
        gebruikersnaam = sr.ReadLine();
        passwoord = sr.ReadLine();
        sr.Close();

        if (gebruikersnaam == inlogNaamTextbox.Text && passwoord == inlogPasswoordTextbox.Text)
        {
            MessageBox.Show("Je bent nu ingelogd!", "Succes!");
            this.Dispose();
        }
        else
            MessageBox.Show("Gebruikersnaam of wachtwoord fout!", "Fout!");
    }

    catch (System.IO.DirectoryNotFoundException ex)
    {
            MessageBox.Show("De gebruiker bestaat niet!", "Fout!");
    }
}

When he clicks the loginButton, the mainForm pops up (the actual program). In this mainForm I have a label which should show the the username that was entered in the login form (Hello, username)

So i don't really have any code to get the value of the textBox to the label on the other form written myself, so I hope you guy can help me.

Here's the code on the mainForm

        private void indexForm_Load(object sender, EventArgs e)
    {
        startForm loginForm = new startForm();
        loginForm.ShowDialog();
    }
Was it helpful?

Solution

I would add a property to your login form that exposes the user name

public string Username
{
    get { return inlogNaamTextbox.Text; }
}

You can then access that property from the form that pops up the login form.

startForm loginForm = new startForm();
loginForm.ShowDialog();
userLabel.Text = loginForm.Username;

Note that a "better" answer would be to use an MVC or MVVM model to bind that information to models separate from the UI form. However, it is non-trivial to apply such models to WinForms (though it can be done).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top