All pastes #924207 Raw Edit

Something

public text v1 · immutable
·published 2008-03-01 03:58 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        TestClass testClass = new TestClass();

        public Form1()
        {
            InitializeComponent();
            testClass.TestEvent += new EventHandler<TestEventArgs>(TestClass_TestEvent);
        }

        private void TestClass_TestEvent(object sender, TestEventArgs e)
        {
            // CHeck if we're on the right thread
            if (InvokeRequired)
            {
                // Invoke the method we're in on the form's thread
                Invoke(new EventHandler<TestEventArgs>(TestClass_TestEvent));
            }

            MessageBox.Show(e.TestString);
        }
    }

    public class TestClass
    {
        public event EventHandler<TestEventArgs> TestEvent;

        public void Test()
        {
            OnTestEvent("Test arg...");
        }

        private void OnTestEvent(string testString)
        {
            EventHandler<TestEventArgs> handler = TestEvent;
            if (handler != null)
            {
                // Raise the event
                handler(this, new TestEventArgs(testString));
            }
        }
    }

    public class TestEventArgs : EventArgs
    {
        private readonly string _testString;

        public TestEventArgs(string testString)
        {
            _testString = testString;
        }

        public string TestString
        {
            get { return _testString; }
        }
    }
}