-2

newbie question Passing veriable between two forms, it's a string "LoggedUserName"

I have MainForm that starts LoggingFrm:

    public void Form1_Load(object sender, EventArgs e)
    {
        
        LoggingFrm newLogin = new LoggingFrm();
        
        newLogin.ShowDialog();

        if(string.IsNullOrEmpty(newLogin.LoggedUser))
        {
            Close();
        }
        else
        {
            User = newLogin.LoggedUser;
            tsslUser.Text = Uzytkownik;
            LoggedUser = User;
            
        }
    }

And logging frm:

    private string User;
    public string LoggedUser 
    { 
        get
        {
            return User;
        }
        set
        {
            User = value;
        }
    }

    private void btnLogIn_Click(object sender, EventArgs e)
    {
        
        String user = tbUser.Text;
        String pass = tbPassword.Text;
        if (IsLogin(user, pass))                  // Mysql code behind
        {
            MessageBox.Show($"Logged as {user}!");
            LoggedUser = tbUser.Text;            //Setting public string
            Close();
        }
        else
        {
            MessageBox.Show($"User {user} not found!");
        }

    }

and third form with

LoggingFrm frm = new LoggingFrm();

lblTest.Text = frm.LoggedUser

And at that point it works fine, but if I try to transfer the string to any different form it doesn't work - string is null.

no matter if I try to pass it from LoggingFrm or MainFrm

Any ideas?

godot
  • 3,422
  • 6
  • 25
  • 42
Oezu
  • 7
  • 2
  • 2
    Take a moment to read through the [editing help](//stackoverflow.com/editing-help) in the help center. Formatting on Stack Overflow is different than on other sites. The better your post looks, the easier it is for others to read and understand it. – gunr2171 Apr 19 '21 at 13:32
  • A common solution for inter-form communication is to keep references between them. Often opening a new form with a parameter is a simple solution: `LoggingFrm newLogin = new LoggingFrm(this);`. This passes the main form to the child form. There, in the constructor, you should copy that refence to a class level variable.. This is similar to the post below, but of course much more porwerful and dynamic - Now you can access all (public or internal) fiields or properties. – TaW Apr 19 '21 at 13:40

2 Answers2

0

If you want use your LoggingFrm.LoggedUser prop to always return the once-set value then make the appropriate member field static:

private static string User;

Every classes instantiating a LoggingFrm can reach the value through LoggedUser property now at least if it was once set.

ps: forms are IDisposable, use them in using block!

cly
  • 661
  • 3
  • 13
0

You're not passing LoggedUser to the third form in that example. You're creating a new instance of LoggingFrm in the third form. This is a completely different object than the one you created in the Form1_Load method - none of the non-static members are going to "carry over" between instances. Unless you're manually setting something in the constructor for LoggingFrm, then everything is going to be the default value when you create that new instance in your third form. This is why the string is null.

In order to pass the LoggedUser value that was entered in LoggingFrm around, one option is to store that value in Form1. You're already doing that inside of the else block in the properties or fields User and LoggedUser. Then when you create the instance of the third form, you need to pass the values you stored in Form1 to the third form's constructor.

public class ThirdForm
{
    public ThirdForm(string loggedUser)
    {
        LoggedUser = loggedUser;
    }
    
    private string LoggedUser { get; set; }
}

public class Form1
{
    private launchThirdFrmBtn_Click(object sender, EventArgs e)
    {
        ThirdForm frm = new ThirdForm(User);
        frm.Show();
    }
}
Joshua Robinson
  • 3,399
  • 7
  • 22