I have to pass data using the collection for two objects ( email, mobile). 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..