0

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; }
}
Sam
  • 73
  • 5
  • 1
    Having a `Run` method within the welcome screen code that does the creation/display is going to make it more difficult to access those as it sorta hides the instance you've created. If your main window does the `new WelcomeForm()..` part instead, you can access public variables through the created instance. There are quite a few questions on here about this sort of thing. – Broots Waymb Jul 06 '20 at 13:21
  • 2
    Does this answer your question? [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) – Code Stranger Jul 06 '20 at 13:25
  • Can this question be removed as a duplicate? I have tried the code noted in that post but it didn't answer my question. – Sam Jul 06 '20 at 14:53

2 Answers2

2

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 ...
}
  • 1
    ModalResult is given an error. "The name does not exist in its current form" – Sam Jul 06 '20 at 13:26
  • Yes, its `DialogResult`... Answer updated. Delphi automatisms :) –  Jul 06 '20 at 13:28
  • Little bit lost here. How do I apply this using the textbox.text inputs ;( – Sam Jul 06 '20 at 13:36
  • Answer updated to add the assignments of values to be returned. –  Jul 06 '20 at 13:42
  • Getting an error for ? **InputData** : null. Also did not change the WelcomeForm to WelcomeFromInoutData. See my update. Then I would need to delete the welcome form and rename it to WelcomeFormInputData. – Sam Jul 06 '20 at 14:01
  • 1
    I updated your question. If you use the `WelcomeFormInputData`, it replaces the fields username and usersurname, hence you get better design and clean code. Perhaps you can throw out the `Sender` usage because in the main form you call `Run` and next can show it and use the inputed data. –  Jul 06 '20 at 14:03
  • Sorted! And thanks for the new code. Looks so much more cleaner!. I see you stored the name and surname in userdata. How can I get access to it individually i.e. name = UserName; sure = UserSurname;? – Sam Jul 06 '20 at 14:27
  • 1
    Thank you. In the main form, you can use `userdata.UserName` and `userdata.UserSurname`. I hope this may help you: [How do I improve my knowledge in C#](http://www.ordisoftware.com/files/stack-overflow/CsharpBegin.htm) –  Jul 06 '20 at 14:29
  • 1
    Will do! But your virtual lessons are much better! – Sam Jul 06 '20 at 14:43
-1

You can use like WelcomeForm.username and you can create class called LocalVar to access and store any data from any form e.g.

in `LocalVar` class use this type of variables:

`public static string username = "";`
`public static string usersurname = "";`

After this before you close WelcomeForm do code like this:

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



and now you can usr `LocalVar.username` anywhere in project.