Window form Parent-child forms update each other

Source: Internet
Author: User

Main form

public partial class Form1:form
{
Public Form1 ()
{
LoginForm dlg = new LoginForm ();
Dlg. StartPosition = formstartposition.centerparent;
Dlg. ShowDialog ();
InitializeComponent ();
This. StartPosition = Formstartposition.centerscreen;
}

Parent forms define delegates and events
public delegate void Changetxt (string text);
public event Changetxt Changestxt_event;
private void Button1_Click (object sender, EventArgs e)
{
Form2 frm = new Form2 (this);//Pass Form 1 pointer
Child Form Subscription Events
Frm.changeftext_event + = new Form2.changetext (frm_changetext_event);
frm. StartPosition = Formstartposition.centerscreen;
frm. Show (this);//The form is not placed outside the parent form
}

void Frm_changetext_event (String text)
{
TextBox1.Text = Text;
}

private void Button2_Click (object sender, EventArgs e)
{
Changestxt_event (TextBox1.Text);
}
}

Child form

public partial class Form2:form

{
Public Form2 (Form1 frm)
{
InitializeComponent ();
Subscribe to Events
Frm.changestxt_event + = new Form1.changetxt (frm_changestxt_event);
}

Child forms Define Delegate events
public delegate void Changetext (string text);
public event Changetext Changeftext_event;
Update method
void Frm_changestxt_event (String text)
{
TextBox1.Text = Text;
}

private void Button1_Click (object sender, EventArgs e)
{
Changeftext_event (TextBox1.Text);
}
}

Window form Parent-child forms update each other

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.