-1

Okay, I saw a couple of answers to similar questions but nothing was quite right, so I thought I would write down my own problem. I have a couple of Forms that are supposed to be connected.

private void btnAdd_Click(object sender, EventArgs e)
        {
            AuthorForm authorForm = new AuthorForm();
            authorForm.Show();
        }

This is the code from the Form2 that opens once I want to create a new list of Authors. With that, the third Form "AuthorForm" pops up and that's where I want to enter a name to add to a listBox that is in Form2. I don't know how to pass the string from the TextBox to the other form.

I'm sorry if this doesn't make much sense. I'm pretty bad at trying to explain what I need but if you have questions, I'll try to explain better.

  • 2
    Does this answer your question? [Passing Values from one Form to another Form in a Button click](https://stackoverflow.com/questions/39026518/passing-values-from-one-form-to-another-form-in-a-button-click) – P. Roe Mar 06 '20 at 21:49
  • Okay, so I wasn't paying enough attention and that's on me. I have to use `authorForm.ShowDialog();` and I didn't. So that's on me. Thank you for the responses. I appreciate them –  Mar 06 '20 at 23:10

3 Answers3

1

Hi and welcome to StackOverflow. There are several ways to achieve this but I believe the easiest would be to use a property in your AuthorForm as follows:

// Inside AuthorForm create the following...
public string AuthorName { get; private set; }

Having done that, you can set this property either in text changed, or when the user clicks to commit the entered text, you then read this property from the Form2.

// This goes inside the text changed off your name text box.
this.AuthorName = AuthorTextBox.Text; // Assuming the name of the textox is AuthorTextBox

Inside Form2 you call read the property as follows.

private void btnAdd_Click(object sender, EventArgs e)
{
    AuthorForm authorForm = new AuthorForm();
    authorForm.ShowDialog();
    // Read the entered text from the property
    var userInput = authorForm.AuthorName;
}

You can proceed to make use of the text the user entered after this.

Fabulous
  • 2,393
  • 2
  • 20
  • 27
  • Okay, so I wasn't paying enough attention and that's on me. I have to use `authorForm.ShowDialog();` and I didn't. So that's on me. Thank you for the responses. I appreciate them –  Mar 06 '20 at 23:10
0

If i'm not mistaken you want to change Form2 instance after authorForm.Show(); statment

You can initial public variable for example name in Form2 and then send all the form2 instance to AuthorForm with constructor (use this keyword) , now you can change variable from form2 in authorForm

see :

    AuthorForm authorForm = new AuthorForm();
    authorForm.Show(this);

AuthorForm :

Form2 form2;

public AuthorForm(Form2 form2)
{
    this.form2 = form2;
}

public void DoSomething()
{
   form2.name =  "test";
}
0

If I got it right, you want to get values from from AuthorForm. You can do that by adding a property to the AuthorForm, like that:

public string AuthorName { get; set; }

And use authorForm.ShowDialog() so all the opened forms will freeze until you use the Close() method in the form you opened as a dialog. And then you can continue the form. For example: AuthorForm.cs:

public void BtnClose_Click(object sender, EventArgs e)
{
    AuthorName = "rom_totach";
    Close();
}

MainForm.cs:

public void BtnOpenAuthorForm(object sender, EventArgs e)
{
    AuthorForm form = new AuthorForm();
    form.ShowDialog();
    MessageBox.Show(form.AuthorName);
}

I hope I helped!

rom_totach
  • 106
  • 8