Hi,

I have two buttons (button1 and button2) in a form which does two different things.
button1 instantiates class A and does something and similarly button2 instantiates
class B and does something.

private void button1_Click(object sender, EventArgs e)
        {
            A a = new A();
            a.DoSumthingWithA();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            B b = new B();
            b.DoSumthingWithB();
        }


public class A
    {
        public void DoSumthingWithA()
        { 
            // do something and 
            // fire a event to display a text in the form in a label all the subscribed clases should do this function.
        }
    }

    public class B
    {
        public void DoSumthingWithB()
        {
            // do something and 
            // fire a event to display a text in the form in a label all the subscribed clases should do this function.
        }
    }

Now I want that whenevr anyone of the method gets execute an event should get triggered just to change the font of
a text in a label in the form.

now from where which class I should write the code to publish the event and where should I write the code to subscribe it.

I may have many more buttons in future which should also change the font.

Please help.

Regards,

I have done something but is my way of doing is the best ??

Please see my code below:

public interface ICommon
    {
        event EventHandler ChangeFont;
    }

 public class A : ICommon
    {
        public void DoSumthingWithA()
        { 
            // do something and 
            // fire a event to display a text in the form in a label all the subscribed clases should do this function.

            FireEvent();
        }

        public event EventHandler ChangeFont;

        private void FireEvent()
        {
            if (ChangeFont != null)
            {
                ChangeFont(this, new EventArgs());
            }
        }
    }

    public class B : ICommon
    {
        public void DoSumthingWithB()
        {
            // do something and 
            // fire a event to display a text in the form in a label all the subscribed clases should do this function.
            FireEvent();
        }

        private void FireEvent()
        {
            if (ChangeFont != null)
            {
                ChangeFont(this, new EventArgs());
            }
        }
        public event EventHandler ChangeFont;
    }

the winform which 'should' subscribe to seeing the fired events



 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            A a = new A();
            a.ChangeFont += new EventHandler(a_ChangeFont);
            a.DoSumthingWithA();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            B b = new B();
            b.ChangeFont += new EventHandler(b_ChangeFont);
            b.DoSumthingWithB();
        }

        void b_ChangeFont(object sender, EventArgs e)
        {
            label1.Text = "Edited";
        }

        void a_ChangeFont(object sender, EventArgs e)
        {
            label1.Text = "Edited";
        }

    }

it is working but is this the right way to do ?? is the design fine ???
Actually like class A and B there will be many classes.

any thought wud be very much appreciated.

Thanks.

Seems OK to me. If in more doubt just google, lots of explanations and tutorials out there. Example

actually what I was thinking was, since like class A and B there will be many new classes.
So is there any other better way to design the class ??

Thanks for reading and reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.