none
Understanding event RRS feed

  • Question

  •  

    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,

    Monday, July 22, 2013 4:23 AM

Answers

  • I get the feeling your a trying to do work that is way above your level of understanding and planing. Partially because you use words out of context. Something like that is never going to work, no matter how much you ask.

    First you have to understand classes (inlcuding properties) and how to make your own.
    Then you have to understand Events and how to make and Raise them in your own classes.
    Then and only then you learn about UI Binding.

    There are tons of Toutorials for each of the 3 Steps out there. Make them in sequence. Don't start the next before you fully understand the previous one. It just won't work.

    We don't have the skill, the time or the motivation (we kinda have a job and live on the sidelines) to teach you that, you have to learn and understand it on your own.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Tuesday, July 23, 2013 8:38 AM

All replies

  • 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 ???

    any thought wud be very much appreciated.

    Thanks.

    Monday, July 22, 2013 5:01 AM
  • You are trying to fire an Event from an Event? By istantiaing a Class?

    Okay, I go out on a limb and that what you do there is propably a very wrong thing.

    How about you tell us the problem you want to solve, instead of throwing out a solution that you yourself don't seem to understand?


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Monday, July 22, 2013 9:17 AM
  • Hi,

    Thanks for the reply.

    my problem : user will click or say change some parameter in the UI. Each
    parameter have a saparate classes and assemblies.

    On click of any of the parameter I have to fire an event and change a lavel in UI
    to "Edited" showing that a parameter is edited.

    COuld you please tell me how to achive this.

    The parameter may increase in future. So I was thinking to use events for this.
    Plese let me know if you have a better idea.

    Thanks a lot.

    Monday, July 22, 2013 4:33 PM
  • I get the feeling your a trying to do work that is way above your level of understanding and planing. Partially because you use words out of context. Something like that is never going to work, no matter how much you ask.

    First you have to understand classes (inlcuding properties) and how to make your own.
    Then you have to understand Events and how to make and Raise them in your own classes.
    Then and only then you learn about UI Binding.

    There are tons of Toutorials for each of the 3 Steps out there. Make them in sequence. Don't start the next before you fully understand the previous one. It just won't work.

    We don't have the skill, the time or the motivation (we kinda have a job and live on the sidelines) to teach you that, you have to learn and understand it on your own.


    Let's talk about MVVM: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/b1a8bf14-4acd-4d77-9df8-bdb95b02dbe2 Please mark post as helpfull and answers respectively.

    Tuesday, July 23, 2013 8:38 AM
  • A friendly suggestion... In my early days I've found several free tutorial sites... I like the following because the way he explains concepts.

    http://www.blackwasp.co.uk/CSharpFundamentals.aspx

    I could post more for you but that one is a good start.

    Tuesday, July 23, 2013 10:01 PM