[英]passing data using delegate in window form c# (subscribe and unsubscribe)
I have to pass data using the collection for two objects ( email, mobile).我必须使用两个对象(email,移动设备)的集合来传递数据。 If I click the button subscriber or unsubscribe, the values that I enter should be stored and showed to another form if I click publish button.
如果我单击按钮订阅者或取消订阅,我输入的值应该被存储并在我单击发布按钮时显示到另一个表单。 However, I require to use 'delegate' for this task.
但是,我需要为此任务使用“委托”。
This is the form that I handle value I entered这是我处理输入值的表格
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace Q1
{
public partial class Form2 : Form
{
// declare delegate
public delegate void PublishMessageDel(ArrayList publist);
public PublishMessageDel publist = null;
public void PublishMessage(ArrayList subscribers)
{
publist.Invoke(subscribers);
}
public Form2()
{
InitializeComponent();
}
private void btnBackToMain_Click(object sender, EventArgs e)
{
// back to first form when the buttom is clicked
this.Hide();
Form1 mainWindow = new Form1();
mainWindow.ShowDialog();
this.Close();
}
private void txtEmailTo_Validating(object sender, CancelEventArgs e)
{
if (ckbEmailTo.Checked)
{
IsEmailFormMatch();
}
else if (!ckbEmailTo.Checked)
{
btnSubscribe.Enabled = false;
btnUnsubscribe.Enabled = false;
}
}
private void txtTextTo_Validating(object sender, CancelEventArgs e)
{
if (ckbTextTo.Checked)
{
IsMobileFormMatch();
}
else if (!ckbTextTo.Checked)
{
btnSubscribe.Enabled = false;
btnUnsubscribe.Enabled = false;
}
}
// check valid email and button visibility
public void IsEmailFormMatch()
{
string emailForm = (@"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
if (!Regex.IsMatch(txtEmailTo.Text, emailForm))
{
MessageBox.Show("Invalid Email");
btnSubscribe.Enabled = false;
btnUnsubscribe.Enabled = false;
}
else
{
btnSubscribe.Enabled = true;
btnUnsubscribe.Enabled = true;
}
}
// check valid mobile and button visibility
public void IsMobileFormMatch()
{
string mobileForm = (@"\d{3}-\d{3}-\d{4}");
if (!Regex.IsMatch(txtTextTo.Text, mobileForm))
{
MessageBox.Show("Invalid Phone Number");
btnSubscribe.Enabled = false;
btnUnsubscribe.Enabled = false;
}
else
{
btnSubscribe.Enabled = true;
btnUnsubscribe.Enabled = true;
}
}
private void btnSubscribe_Click(object sender, EventArgs e)
{
ArrayList subscribers = new ArrayList();
if (ckbEmailTo.Checked)
{
subscribers.Add(txtEmailTo.Text);
}
else if (ckbTextTo.Checked)
{
subscribers.Add(txtTextTo.Text);
}
foreach (ArrayList list in subscribers)
{
PublishMessage(list);
}
}
private void btnUnsubscribe_Click(object sender, EventArgs e)
{
ArrayList subscribers = new ArrayList();
if (ckbEmailTo.Checked)
{
subscribers.Remove(txtEmailTo.Text);
}
else if (ckbTextTo.Checked)
{
subscribers.Remove(txtTextTo.Text);
}
}
}
}
I think this part has a problem.我觉得这部分有问题。
private void btnSubscribe_Click(object sender, EventArgs e)
{
ArrayList subscribers = new ArrayList();
if (ckbEmailTo.Checked)
{
subscribers.Add(txtEmailTo.Text);
}
else if (ckbTextTo.Checked)
{
subscribers.Add(txtTextTo.Text);
}
foreach (ArrayList list in subscribers)
{
PublishMessage(list);
}
}
private void btnUnsubscribe_Click(object sender, EventArgs e)
{
ArrayList subscribers = new ArrayList();
if (ckbEmailTo.Checked)
{
subscribers.Remove(txtEmailTo.Text);
}
else if (ckbTextTo.Checked)
{
subscribers.Remove(txtTextTo.Text);
}
}
This is the form if I click publish button, the textbox should have to show me the values that subscribed in the previous form.如果我单击发布按钮,这是表单,文本框应该向我显示在前一个表单中订阅的值。
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Q1
{
public partial class Form3 : Form
{
Form2 fm2;
public Form3()
{
InitializeComponent();
}
private void btnBackToMain3_Click(object sender, EventArgs e)
{
this.Hide();
Form1 mainWindow = new Form1();
mainWindow.ShowDialog();
this.Close();
}
// change button visibility when the textbox is empty or not
public void btnPublish_Click(object sender, EventArgs e)
{
Form2 fm2 = new Form2();
var value = fm2.publist += Subscribe;
txtSubscribers.Text += value;
//fm2.PublishMessage();
}
public void Subscribe(ArrayList list)
{
ArrayList subscribers = list;
txtSubscribers.Text += subscribers.ToString();
}
}
}
I think I used delegate not properly and stuck to link with it..我想我没有正确使用委托并坚持与它联系..
This is using delegates microsoft guide .这是使用 delegates microsoft guide 。
"A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of the delegate." "A delegate is a type that safely encapsulates a method, similar to a function pointer in C and C++. Unlike C function pointers, delegates are object-oriented, type safe, and secure. The type of a delegate is defined by the name of代表。”
So, before invoking arrSubscribers.Invoke(list);
所以,在调用
arrSubscribers.Invoke(list);
之前you should make a method in Form3
which takes the signature of your delegate(you receive as a parameter your ArrayList arrSubscribers
) and add the reference to fm2.arrSubscribers
.您应该在
Form3
中创建一个方法,该方法采用您的委托签名(您收到ArrayList arrSubscribers
作为参数)并添加对fm2.arrSubscribers
的引用。
public void YourMethod(ArrayList arrSubscribers)
公共无效您的方法(ArrayList arrSubscribers)
{{
// Your code with subscribers here// 这里有订阅者的代码
}}
fm2.arrSubscribers = YourMethod;
or fm2.arrSubscribers += YourMethod;
或
fm2.arrSubscribers += YourMethod;
Example:例子:
private ArrayList subscribers;
public Form2(Form1 form)
{
InitializeComponent();
form.arrSubscribers = MyMethod;
}
public void MyMethod(ArrayList list)
{
subscribers = list;
}
private void Click(object sender, EventArgs e)
{
var query = from string item in subscribers
select item;
label1.Text = string.Join(", ", query);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.