Winfrom controls data transfer between forms

Source: Internet
Author: User

Method 1
You can customize a constructor in a subform. The parameter type is the main form. When you want to display the subform, use this constructor to instantiate the subform and pass this pointer in. The Code is as follows:
Main form. cs
Public class frmMain: Form
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
}
Copy code
Subform. cs
Subform. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> 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!
}
}
Copy code

Method 2
In fact, the method is not very good. Although the implementation is very simple, just want to change the title Text of the form, it will 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. The Code is as follows:
Define the API. cs
Public interface IChangeTitle:
{
Void ChangeTitle (string title );
}
Copy code
Main form. cs
Main form. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> Public class frmMain: Form, IChangeTitle
{
...
FrmControl controlForm = new frmControl (this );
ControlForm. Show ();
Public void ChangeTitle (string title)
{
This. Text = title;
}
}
Copy code
Subform. cs
Subform. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> 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)
{
// Call a method through an interface
IchangeTitle. ChangeTitle (this. textBox1.Text );
}
}
Copy code

Method 3
To further reduce the coupling between forms, we can use delegation to achieve this requirement. The Code is as follows:
Main form. cs
Main form. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> // Assign a value to the delegate variable to 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 ();
}
}
Copy code
Subform. cs
Subform. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> Public partial class ChildForm: Form
{
// Declare and define Delegation
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

}
}
Copy code

Method 4
You can also define a custom event in the subform, and then customize an event parameter to pass some information you want to pass. The Code is as follows:
Main form. cs
Main form. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> // 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 ();
}
}
Copy code
Subform. cs
Subform. cs
<! --

Code highlighting produced by Actipro CodeHighlighter (freeware)
Http://www.CodeHighlighter.com/

--> Public partial class ChildForm: Form
{
Public class TitleChangedEventArgs: EventArgs // event parameter class
{
Private string title = "";
Public string Title
{
Get {return title ;}
Set {title = value ;}
}
}
// Declare the delegate
Public delegate void TitleChangedEventHandler (object sender, TitleChangedEventArgs e );
// Define the event
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

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

From Bychentufeiyang's column

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.