Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

General Events Question

$
0
0
Hi,

I'm following tlhIn`toq's tutorial on 'Quick and easy custom events', I'm just trying to move the event out of the form into another class however I think I have couppled the two class's together by doing this.

I am unsure if I am passing the strings from my 'ProcessData' class to my form correctly? I was thinking that really my '
ReturnProcessData()
method in the ProcessData class should be private which would mean that I’m going about accessing the event entirely wrong (which I suspect I am since I reference the Feedback event from my class also).

namespace events
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();  
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        void Form1_Feedback(object sender, TextEventArg e)
        {
            listBox1.Items.Add(e.Message);
        }

        private void EventBtn_Click_1(object sender, EventArgs e)
        {
            ProcessData pd = new ProcessData();
            pd.Feedback += new EventHandler<TextEventArg>(Form1_Feedback);
            pd.ReturnProcessData();
        }
    }
}



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace events
{
    class ProcessData
    {
        public event EventHandler<TextEventArg> Feedback;

        private void RaiseFeedback(string p)
        {
            EventHandler<TextEventArg> handler = Feedback;

            if (handler != null)
            {
                handler(null, new TextEventArg(p));
            }
        }

        public void ReturnProcessData()
        {
            RaiseFeedback("Data Process Started");
            RaiseFeedback("Second Process Started at" + DateTime.Now.ToString());
        }
    }
}



namespace events
{
    public class TextEventArg : EventArgs
    {

        private string szMessage;

        public TextEventArg(string TextMessage)
        {
            szMessage = TextMessage;
        }

        public string Message
        {
            get { return szMessage; }
        }
    }
}


Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>