Some methods of data transfer interaction between forms in WinForm-practical tips

Source: Internet
Author: User

In fact, I have also written a similar theme, here a variety of methods to sum up, the content is indeed a few, so this article is written to just learn C # colleagues, I hope that some help to everyone! Sorry, this article does not have the strange bug to arouse everybody's interest, but the next article I will try to write some interesting topic!

There are many more ways to pass data between forms :
1, customize a constructor in the subform, the parameter type is the main form, when you want to display the subform, use this constructor to instantiate the subform, and then pass the this pointer into it, it's too abstract to say, and I'll probably write about it:

Copy Code code as follows:

public class Frmmain:form
{
...
Frmcontrol controlform=new Frmcontrol (this);
Controlform.show ();
}
public class Frmcontrol:form//subform to control some display of the main form!
{
Private Frmmain MainForm;
Public Frmcontrol (Frmmain mainform)
{
This.mainform=mainform;
}
private void Button1_Click (Object Sender,eventargs e)
{
Frmmain.textbox1.text=this.textbox1.text; The text box value of the handle form is passed to the text box of the main form!
}
}

2, I personally feel that the above method is not very good, although the implementation is very simple, just want to change the title text of the form, the whole main form of the reference to pass to the subform, this way is not very elegant, we use the interface to improve the above method, so that you can limit the functionality exposed to the subform, reduce the coupling between forms:
Copy Code code as follows:

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 to control some display of the main form!
{
Private Ichangetitle Ichangetitle;
Public Frmcontrol (Ichangetitle ichangetitle)
{
This.ichangetitle=ichangetitle;
}
private void Button1_Click (Object Sender,eventargs e)
{
Ichangetitle.changetitle (This.textBox1.Text); To invoke a method through an interface
}
}

3, to further reduce the coupling between forms, we can use delegates to achieve this requirement:
Copy Code code as follows:

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 invocation
}
}

The main form assigns values to the delegate variable:
Copy Code code as follows:

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 Button1_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 some of the information you want to deliver:
Copy Code code as follows:

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)//Trigger event method
{
if (titlechanged!= null)
Titlechanged (this, e);
}
}

The main form is subscribed to this event:
Copy Code code as follows:

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 Button1_Click (object sender, EventArgs e)
{
Loginform.show ();
}
}


An event is raised out of the child window, and the parent window implements the event, and we can transmit the data without closing the parent window and the child window.

Thinking: In the child window "Add User" by clicking "Add User", will dynamically raise an event, through the event, we can carry out data transmission

In other windows, you can respond to the event

The child window is responsible for raising an event outward, delegating the implementation of the event to another object, and the details of how the event is handled, the child window is not concerned, which embodies a loose coupling of a setting

the knowledge points involved :
How do I define an event?

Copy Code code as follows:

public delegate void Useraddeventhandler ();
public event Useraddeventhandler Useradd;
How do I respond to events raised by child windows?

Userdialog.useradd + = userdialog_useradd;//delegate Useradd implementation to Userdialog_useradd method

void Userdialog_useradd ()
{
Method body
}
How do I pass data to a parent window in a child window?

By delegate's declaration, add two parameters:

public delegate void Useraddeventhandler (Object Sender,usereventargs e);

Where Usereventargs is a class that inherits from EventArgs

public class Usereventargs:eventargs
{
Public User CurrentUser {Get;set}
Public Usereventargs (user user)
{
This.currentuser = user;
}
}
Thus, when the event is raised externally, the user's information is passed through the second argument

private void Button1_Click (object sender, EventArgs e)
{
To raise an event outward
if (useradd!=null)
{
User user = new user ();
User. UserName = TextBox3.Text;
User. PassWord = Textbox4.text;
Useradd (this,new Usereventargs (user));
}
}
In the Userdialog_useradd method of the parent window, you can work with the data you receive
private void Add user Toolstripmenuitem_click (object sender, EventArgs e)
{
Frmuser Userdialog = new Frmuser ();
Delegate the implementation of Useradd to the Userdialog_useradd method
Userdialog.useradd + = Userdialog_useradd;

Userdialog.showdialog ();
}

void Userdialog_useradd (Object Sender,usereventargs e)
{
MessageBox.Show (E.currentuser.username);
}

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.