The article was first published on Codeyou.com-- "Let's talk about C# Winforms desktop application to achieve cross-form delegate value transfer (example)"
foreword
Passing values across forms in C# Winforms desktop applications is actually an old-fashioned question. I have also written a number of example articles on C# Winforms desktop applications that pass values across forms on Codeyou.com, such as:
"Passing values between C# WINFORM forms through delegation and events (custom event parameters)--Detailed example"
"Using delegates and events in C#/.NET WINFORM to update form UI controls in classes"
Then why "talk about" C# Winforms desktop application cross-form delegate pass value? Because for the vast majority of C#&.NET novices, it is difficult to learn and master C#'s delegation, events, etc., and developers need to constantly learn and practice projects.
In addition, there is more than one scheme for implementing value transfer between C# Winforms forms. This article will demonstrate the use of a special delegate Action
for C#&.NET developers. A form pass-by-value instance.
Effect preview
This example mainly demonstrates contact management, including functions such as creating a new contact and a contact list.
The final preview of the instance is as follows:
Create solutions and projects
Open Visual Studio 2022, create a solution for testing, name it: WindowsFormsApp1 , and create a project called WindowsFormsApp1 in the solution.
Create three new Winform forms: FrmMain , FrmCreate , FrmList and a contact class Contact.cs
Contact Human (Contact.cs) is defined as follows:
using System;
namespace WindowsFormsApp1.Models
{
/// <summary>
/// 联系人
/// </summary>
public class Contact
{
public Guid Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
}
Main form FrmMain.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using WindowsFormsApp1.Models;
namespace WindowsFormsApp1
{
public partial class FrmMain : Form
{
private List<Contact> _contacts;
public FrmMain()
{
_contacts = new List<Contact>();
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new FrmCreate();
frm.OnContactCreated = (contact) =>
{
_contacts.Add(contact);
};
frm.ShowDialog();
}
private void button2_Click(object sender, EventArgs e)
{
var frm = new FrmList(_contacts);
frm.ShowDialog();
}
}
}
In the main form, in the [New Contact] button event, an instance of --- FrmCreate
is created frm
, and a callback (delegate) is set for the frm
instance OnContactCreated
, this step is the key to delegating value .
New contact form FrmCreate.cs
using System;
using System.Windows.Forms;
using WindowsFormsApp1.Models;
namespace WindowsFormsApp1
{
public partial class FrmCreate : Form
{
public FrmCreate()
{
InitializeComponent();
}
/// <summary>
/// 联系人创建成功的回调(委托)
/// </summary>
public Action<Contact> OnContactCreated;
private void btnSubmit_Click(object sender, EventArgs e)
{
var contact = new Contact
{
Id = Guid.NewGuid(),
Name = textBox1.Text.Trim(),
Email = textBox2.Text.Trim()
};
OnContactCreated?.Invoke(contact);
Close();
}
}
}
In the [New Contact] form, we define the callback (delegation) for the successful creation of the contact. When the "Save Contact" button is clicked, if the caller sets the OnContactCreated
callback, it will be executed. method in the callback. Among them, the statement OnContactCreated?.Invoke(contact);
is the key.
Contact list form FrmList.cs
using System.Collections.Generic;
using System.Windows.Forms;
using WindowsFormsApp1.Models;
namespace WindowsFormsApp1
{
public partial class FrmList : Form
{
private List<Contact> _contacts;
public FrmList(List<Contact> contacts)
{
_contacts = contacts;
InitializeComponent();
}
private void FrmList_Load(object sender, System.EventArgs e)
{
dataGridView1.DataSource = _contacts;
}
}
}
If you are interested in the source code of this demo, please go to the original text to get it.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。