Value transfer between the child form and the parent form -- implemented by Delegation

Source: Internet
Author: User

In the past two days, the WinForm program needs to be implemented, and an ASP inventory change function module needs to be converted into a Winform program. Therefore, this situation occurs when values are transferred between forms, I used to do this before when I went to work in ZhaoQing. At that time, I started the Winfom program, mainly for Machine Communication, which is closely related to the single-piece mode. After about four months, I really forgot about it. I started to do this, so I had no choice but to start learning again. At that time, the technical manager told me about the Commission. I understood the general idea during the incident. It was just a waste of time. I didn't understand it. Now I have spent some time doing a DEMO and applying it to the actual development environment. Although many problems have occurred during the application process, I cannot predict it, maybe the foundation is not reliable, the short circuit or ideology of the brain is vague, and some basic concepts are not remembered, which leads to the failure of passing values by the child to the parent, the parent form cannot display the desired data. The DEMO made by myself can be displayed on the contrary. It is very strange, but I also know that the problem is that the constructor in the parent form has never been closely linked with the instance. As a result, I always fail to pass the value myself.

Here I mainly have three WINFORM forms: MainParent, ChildForm, ThirdForm, MainParen ChildForm, ChildForm, and ThirdForm. Now I have an entity class, for more information, see:

First, I define the delegate and the delegate event in my third form,

In addition, I need to receive events triggered by the third form in the 2nd forms ChildForm, and then fill in the required values based on the events.

The Code is as follows:

 

Code
 Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;

Namespace WinFormSendValue
{
Public partial class ThirdForm: Form
{
Public delegate void SetValueSend (UserInfo userinfo); // defines the delegate
Public SetValueSend sendEvent; // declare a delegate-type event
Public ThirdForm (string aa)
{
InitializeComponent ();
}

Private void button#click (object sender, EventArgs e)
{
UserInfo model = new UserInfo (); // entity class
Model. UserName = this. textBox1.Text;
Model. PassWord = this. textBox2.Text;
If (sendEvent! = Null)
{
SendEvent (model );
}
This. Visible = false;
}
}
}

 

The code above is the delegate and event defined in Form 3, and the time when the event triggers the value transfer.

 

Then, you need to obtain the value passed in Form 3 in Form 2, and you must subscribe to the event, and then reference the conditional function after the event is triggered.

For more information, see the code:

 

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WinFormSendValue
{
public partial class ChildForm : Form
{

private ThirdForm thirdfrm = new ThirdForm("aa");
public ChildForm()
{
Initialize

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.