-2

I have a MainForm which has a Textbox and a Button in it. Then I have a second form with a single button. On program start, the MainForm is initialized and when I click the button, the second form shows up (ShowDialog()) still keeping open the MainForm.

So I have these two forms opened next to each other. The thing I want is, that when I click the button, the button will send a string to the MainForm. MainForm will take the text and display it in it's textbox. But I want to make the change happen immediately - without hiding and showing the MainForm again. Sort of like refresh it, when the button on the second form is pressed.

How can I do that?

Note: It's important to have the text, which is send to the MainForm, have declared in the second form. (In my program, the text is dynamically being changed on the second form level)

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Michal Hromas
  • 75
  • 1
  • 7
  • 1
    see answers in this link: http://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form – Harsh Mar 08 '16 at 23:18
  • Wait then why you have the second form if you want to just show it in `Form1` – Just Do It Mar 08 '16 at 23:18

2 Answers2

1

Try sending the TextBox to the constructor of the second form and the second form, when you give click the button, change the Text property of the TextBox and it will appear as if it will be updated as they are referring to the same place.

public partial class Form1 : Form
{
    public Form1(TextBox txt)
    {
        InitializeComponent();

        this.txt = txt;
    }

    //variable
    TextBox txt = null;

    private void button1_Click(object sender, EventArgs e)
    {
        txt.Text = "Your text";
    }
}
Richard
  • 568
  • 1
  • 6
  • 23
0

If I correctly understand, you need create a property in the winform. ex:

public partial class frmLogin : Form
    {
       public bool LoggedIn
        {
            get { return loggedIn; }
        }

        public frmLogin()
        {

            InitializeComponent();


        }
}

// Now, in your forms, you can do.
frmLogin frm = new frmLogin ();

frm.ShowDialog();

var value = frm.LoggedIn;
Marinpietri
  • 112
  • 3
  • 8