Transfer Data between delegated winform windows

Source: Internet
Author: User
Document directory
  • Comment

In winform applications, data transmission between windows is one of the most common applications, especially in management software.

There are multiple ways to transmit window data. In this article, we will mainly explain how to transmit data using delegation.

In this instance program, there are two forms: one main form frmmain, receives data; the other is a child form frmchild, which transmits data. The UI is as follows:

In the main form, click the [set] button to bring up a subform. Enter the corresponding value in the text box, click [OK] to return to the main form, and return data to the main form.

The Code is as follows:

I. Define the event class:
public class PassDataWinFormEventArgs : EventArgs
{

public PassDataWinFormEventArgs()
{
//
}
public PassDataWinFormEventArgs(string refName, string refPlace, int refAge)
{
this._name = refName;
this._place = refPlace;
this._age = refAge;
}

private string _name;
private string _place;
private int _age;

public string Name
{
get { return _name; }
set { _name = value; }
}
public string Place
{
get { return _place; }
set { _place = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
}

Pass data through the passdatawinformeventargs instance.
2. design the following code in the child form:
// Add a delegate
Public Delegate void passdatabetweenformhandler (Object sender, passdatawinformeventargs E );
// Add a passdatabetweenformhandler event
Public event passdatabetweenformhandler passdatabetweenform;
The code for writing the subform confirmation button is as follows:
private void bbtOK_Click(object sender, EventArgs e)
{
string name,place;
int age;

name = txtName.Text;
place = txtPlace.Text;

Int32.TryParse(txtAge.Text, out age);

PassDataWinFormEventArgs args = new PassDataWinFormEventArgs(name, place, age);

PassDataBetweenForm(this, args);

this.Dispose();
}

3. Design the main form code:

The code for the [set] button in the main form is as follows:

private void bbtSetup_Click(object sender, EventArgs e)
{
FrmChild frmChild = new FrmChild();
frmChild.PassDataBetweenForm += new FrmChild.PassDataBetweenFormHandler(FrmChild_PassDataBetweenForm);
frmChild.Show();
frmChild.Activate();
}

It is mainly used to open subforms and bind events.

The frmchild_passdatabetweenform () method receives the data transmitted by the passdatawinformeventargs instance. Therefore, the frmchild_passdatabetweenform () code is as follows:

private void FrmChild_PassDataBetweenForm(object sender,PassDataWinFormEventArgs e)
{
this.txtName.Text = e.Name;
this.txtPlace.Text = e.Place;
this.txtAge.Text = e.Age.ToString();
}
Iv. Postscript:
A major advantage of using delegation for data transmission is that subforms can be called in any form without having to modify the internal design of the subforms to achieve highly reusable subforms.
 
0 0

0

(Please comment on the article) «previous: asynchronous Delegation
» Next article: javascript obtains the value of checkboxlist

Posted on inforasc read (345) Comments (9) edit the category of the favorite online Abstract: C # basic knowledge

Comment

1740010

# On the 1st floor, winform beginners [unregistered users] top a public passdatawinformeventargs in the subform. What are the disadvantages of this method?
Reply to reference

# An error is reported when netking is debugged at, December 25 ,.
Passdatabetweenform (this, argS );
Error: the instance of the object has not been referenced.
View reply reference

# What do I mean when the third floor is ?.. Do you want to describe an application method of delegation? The delegate can be used to pass parameters.
Or is it a callback function.
View reply reference

#4 floor [main poster] 2009-10-26 20:33 inforasc @ Shao
It should be a delegated application!

When passing data between winforms, other methods have been tried. For example, using the owner attribute always feels highly coupled!
View reply reference

#5 floor [main poster] 2009-10-26 20:36 inforasc @ netking

The Code should be okay. I pasted it directly from the Vs. net source code.

Can you tell me which object is not instantiated?


View reply reference

# 6th floor, @ inforasc
Well. Recommended. I remember someone asked me a question once. Q: Is there a interface with a button on the interface that pops up page B to control the image changes on page? How can this problem be solved? At least 3 types
View reply reference

# Ibate86 on the 7th floor,. Thank you. Although I don't know if it's a new idea, it's the first time I heard about it. Thank you ~
View reply reference

#8 floor [main poster] inforasc @ ibate86
It is not a new idea, it is used in actual projects, and has been seen in some technical articles, as if in codeproject
View reply reference

# Building 9, stevenju

View Source

Print?

01 private void bbtOK_Click(object sender, EventArgs e)
02 {    string name,place;    
03 int age;    
04 name = txtName.Text;    
05 place = txtPlace.Text;    
06 Int32.TryParse(txtAge.Text, out age);  
07 if(PassDataBetweenForm != null)
08 {
09   PassDataWinFormEventArgs args = newPassDataWinFormEventArgs(name, place, age);    PassDataBetweenForm(this, args);   
10  this.Dispose();
11 }

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.