Methods for interacting between WinForm forms

Source: Internet
Author: User
In fact, I have also written similar theme in the past. Here I will summarize various methods, and the content is indeed based on some. So this article is written to colleagues who have just learned C, hope to help you! Sorry, there are no strange bugs in this article to arouse everyone's interest, but I will try my best to write some interesting theme in the next article!
There are many ways to pass data between forms:
1. Define a constructor in the subform. The parameter type is the main form. to display the subform, use this constructor to instantiate the subform, then pass the this pointer in. It is too abstract. I should understand it as soon as I write it:
Public class frmMain: Form
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
}

Public class frmControl: Form // subform, used to control some display of the main Form!
{
Private frmMain mainForm;
Public frmControl (frmMain mainForm)
{
This. mainForm = mainForm;
}
Private void button#click (object sender, EventArgs e)

{

FrmMain. textBox1.Text = this. textBox1.Text; // pass the text box value of the subform to the text box of the main form!

}

}
2. I personally feel that the above method is not very good. Although it is very simple to implement, I just want to change the title Text of the form and pass the reference of the entire main form to the subform, this method is not very elegant. We use interfaces to improve the above method, which can limit the function exposed to subforms and reduce the coupling between forms:
Public interface IChangeTitle:
{
Void ChangeTitle (string title );
}
Public class frmMain: Form, IChangeTitle
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
Public void ChangeTitle (string title)
{
This. Text = title;
}
}

Public class frmControl: Form // subform, used to control some display of the main Form!
{
Private IChangeTitle ichangeTitle;
Public frmControl (IChangeTitle ichangeTitle)
{
This. ichangeTitle = ichangeTitle;
}
Private void button#click (object sender, EventArgs e)

{

IchangeTitle. ChangeTitle (this. textBox1.Text); // call the method through the interface

}

}
3. To further reduce the coupling between forms, we can use delegation to achieve this requirement:
Public partial class ChildForm: Form
{
Public delegate void TitleChangedHandler (string title );
Public TitleChangedEventHandler TitleChanged;
Public ChildForm ()
{
InitializeComponent ();
}


Private void btn_ OK _Click (object sender, EventArgs e)
{
If (TitleChanged! = Null)
TitleChanged ("Test Title"); // delegate call

}
}
Assign a value to the delegate variable in the main form:
Public partial class MainForm: Form
{
Private ChildForm loginForm = new ChildForm ();
Public MainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged = new ChildForm. TitleChangedEventHandler (FormTitleChanged );
}

Protected void FormTitleChanged (string title)
{
This. Text = title;
}

Private void button#click (object sender, EventArgs e)
{
LoginForm. Show ();
}
}

4. You can also define a custom event in the subform, and then customize an event parameter to pass the information you want to pass:
Public partial class ChildForm: Form
{
Public class TitleChangedEventArgs: EventArgs // event parameter class
{
Private string title = "";
Public string Title
{
Get
{
Return title;
}
Set
{
Title = value;
}
}
}
Public delegate void TitleChangedEventHandler (object sender, TitleChangedEventArgs e );
Public event TitleChangedEventHandler TitleChanged;
Public ChildForm ()
{
InitializeComponent ();
}


Private void btn_ OK _Click (object sender, EventArgs e)
{
TitleChangedEventArgs e1 = new TitleChangedEventArgs ();
E1.Title = "Login sucessed ";
OnTitleChanged (e1); // trigger event

}

Protected virtual void OnTitleChanged (TitleChangedEventArgs e) // Method for triggering an event
{
If (TitleChanged! = Null)
TitleChanged (this, e );
}
}

Subscribe to this event on the main form:
Public partial class MainForm: Form
{
Private ChildForm loginForm = new ChildForm ();
Public MainForm ()
{
InitializeComponent ();
LoginForm. TitleChanged + = new ChildForm. TitleChangedEventHandler (FormTitleChanged );
}

Protected void FormTitleChanged (object sender, ChildForm. TitleChangedEventArgs e)
{
This. Text = e. Title;
}

Private void button#click (object sender, EventArgs e)
{
LoginForm. Show ();
}
}

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.