Search code examples
c#winformsuser-inputuser-experience

How to return values, entered by the user in a welcome screen, to the main screen after closed?


Previous Post

As per my previous post. I got it working correctly.

In the welcome screen, the user enters his/her details. Like name and surname. Then the user clicks the next button. The welcome screen closes and the main window opens.

However, the name and surname are no longer accessible in the main.cs file.

The welcome screen code:

    public string username;
    public string usersurname;

    static private Form Sender;

    static public void Run(Form sender)
    {
        if (sender == null)
            throw new ArgumentNullException();

        Sender = sender;
        new WelcomeForm().ShowDialog();
    }
   
    public WelcomeForm()
    {
        InitializeComponent();
    }

    private void sign_in_Click(object sender, EventArgs e)
    {   
        username = textBox1.Text;
        usersurname = textBox2.Text;
        Close();
    }

My Main window code:

    private void Form1_Load(object sender, EventArgs e)
    {
        WelcomeForm.Run(this);
    }

    public Form1()
    {
        InitializeComponent();
    }

How do I get access to username and usersurname in my main.cs file as well?

Updated Code (Welcome screen):

    private WelcomeFormInputData InputData = new WelcomeFormInputData();

    static private Form Sender;

    static public WelcomeFormInputData Run(Form sender)
    {
        if (sender == null)
            throw new ArgumentNullException();
        Sender = sender;
        var form = new WelcomeForm();
        return form.ShowDialog() == DialogResult.OK ? form.InputData : null;
    }

    private void ButtonValidate_Click(object sender, EventArgs e)
    {
        InputData.UserName = textBox1.Text;
        InputData.UserSurname = textBox2.Text;
        DialogResult = DialogResult.OK;
        Close();
    }
    public WelcomeForm()
    {
        InitializeComponent();
    }
public class WelcomeFormInputData
{
  public string UserName { get; set; }
  public string UserSurname { get; set; }
}

Solution

  • You can simply improve the Run method as:

    static public WelcomeForm Run(Form sender)
    {
        if (sender == null)
            throw new ArgumentNullException();
        Sender = sender;
        var form = new WelcomeForm();
        return form.ShowDialog() == DialogResult.OK ? form : null;
    }
    

    You need to manage this dialog result with a button :

    private void ButtonValidate_Click(object sender, EventArgs e)
    {
      UserName = textBox1.Text;
      UserSurname = textBox2.Text;
      DialogResult = DialogResult.OK;
      Close();
    }
    

    Every form has some properties to manage that:

    https://learn.microsoft.com/dotnet/api/system.windows.forms.form.dialogresult

    By default, the result is DialogResult.None when a form is closed.

    A non-null result indicates the user had validated something and the opener form can use results as you done by assigning public fields that you should set to external read only properties:

    public string UserName { get; private set; }
    public string UserSurname { get; private set; }
    

    Another mean is to return a structured data entity instead of the form itself:

    public class WelcomeFormInputData
    {
      public string UserName { get; set; }
      public string UserSurname { get; set; }
    }
    

    So you can change that in the welcome form:

    private WelcomeFormInputData InputData = new WelcomeFormInputData();
    
    static public WelcomeFormInputData Run(Form sender)
    {
        if (sender == null)
            throw new ArgumentNullException();
        Sender = sender;
        var form = new WelcomeForm();
        return form.ShowDialog() == DialogResult.OK ? form.InputData : null;
    }
    
    private void ButtonValidate_Click(object sender, EventArgs e)
    {
      InputData.UserName = textBox1.Text;
      InputData.UserSurname = textBox2.Text;
      DialogResult = DialogResult.OK;
      Close();
    }
    

    Here we return only the data if validated.

    You can name artifacts as you need to be the most meaningful, the simplest and the most coherent.

    Note: you can change Form by MainForm if you need to control the MainForm specializations.

    static public WelcomeForm Run(MainForm sender)
    

    But perhaps you can throw out the Sender usage because in the main form you call Run and next you can show it and use the inputed data:

    static public WelcomeFormInputData Run()
    {
        var form = new WelcomeForm();
        return form.ShowDialog() == DialogResult.OK ? form.InputData : null;
    }
    

    And from the main form:

    var userdata = WelcomeForm.Run();
    Show();
    if ( userdata != null ) 
    {
      ... userdata.UserName ...
      ... userdata.UserSurname ...
    }