开发者

Updating textbox on mainform with a variable on a child form that is launched from main form

I have a windows form app. The main form has a textbox, and a button to launch another form. When the other form is launched, both forms are on screen (but the launched form is modal). The child form has a textbox and button, and when the button is pressed, I want the textbox o开发者_如何学编程n the main form (the parent) to be updated with the value in the textbox on the child form.

How is this functionality achieved?


Ideally you want to keep both forms from being dependent on each other, this could be achieved with interfaces:

public interface IMainView
{
  public void UpdateValue(string val);
}

public interface IChildView
{
  public void Show(IMainView parent);
}

have your main form implement IMainView and the child implement IChildView, the parent calls child.show(this) and the child calls parent.UpdateValue(blah);

Hope this helps.


If the child form is closed when the button is clicked, you could put a public property which wraps the value of the textbox on the child form. Then the main form can read this property after calling ShowDialog.

If you want this to happen without closing the child form, you can create a function on the main form to change the textbox. Then the child form would call that function.


The best ways to achive this situation are clockWize's and Hans Passants's advices. But what about that?

Write a property for your textbox at parent form, like this.

public string TextBoxText
{
get { return txtTextBox.Text;}
set { txtTextBox.Text = value;}
}

When you are opening the child form set the owner.

ChildForm f = new ChildForm();
f.Owner = this;
f.Show();

Create an event handler to child forms button click event.

public Button1_Click(object sender; EventArgs e)
{
ParentForm f = (ParentForm)this.Owner;
f.TextBoxText = txtChildTextBox.Text;
}

i didn't compile code; so may have errors :)

}


When a button is pressed to close the launched form, returning you to the main form- the launched form's text box is still in scope.

Closing a form is merely changing the object's state, not disposing of it. So in the button eventhandler that launches the form from the main form, the next line after launching your modal window, it can access the text from the object it launched as the textbox is a child of that form's object. Unless you're launching your modal window in another thread, which I wouldn't figure you are since it's modal, when it is closed, it should go to the next line in the buttons eventhandler that launched it.

your main form may have code something like this right now (haven't done winforms in a while so bear with me if I miss something):

public void Button1_Click(object sender, ClickEventArgs e)
{
    SomeFormIWantToLaunch launchForm = new SomeFormIWantToLaunch();
    launchForm.ShowDialog(this);
}

You need to just add after launchForm.ShowDialog(this); something like:

this.SomeTextBox.Text = launchForm.ATextBox.Text;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜