Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have designed a C# form application with one button in which by each click on the button, 1 is added to int variable t. I want the program to give me a message when t=5. The codes are written in the following way. But my question: how can I complete this code without writing any future code in button1_Click function, in "event delegate" way or other ways?
namespace NumEv
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
         ++t;
        }
        ....
    }
}


What I have tried:

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

        private void EvHppn(object sender,EvArg e)
        {
            MessageBox.Show(sender.ToString() + e.NUMget);
            label1.Text = t.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            NumTstrObj.NumEv += EvHppn;
            NumTstrObj.NumTest(++t);
            NumTstrObj.NumEv -= EvHppn;
        }

        NumTstr NumTstrObj = new NumTstr();
        int t = 0;
    }

    public class EvArg : EventArgs
    {
        private int _Num;
        public EvArg(int num) { this._Num = num; }

        public int NUMget
        {
            get { return _Num; }
        }
    }

    public delegate void NumEvHndlr(object sender, EvArg e);

    class NumTstr
    {
        public event NumEvHndlr NumEv;

        public void NumTest(int n)
        {
            if (n==5 && this.NumEv != null)
                NumEv(this, new EvArg(n));
        }
        
    }

}
Posted
Updated 1-Jan-17 22:09pm
v2

1 solution

I think you need to create Operator(++) overloading class with Events..
try this:-
using System;

namespace WindowsFormsApplication1
{
    class PlusPlusOperatorOver
    {
       int num;
       public event NumEvHndlr NumEv;
        public PlusPlusOperatorOver(int num)
        {
            this.num = num;
        }
        public void NumTest(int n)
        {
            if (n == 5 && this.NumEv != null)
                NumEv(this, new EvArg(n));
        }
        public static PlusPlusOperatorOver operator ++(PlusPlusOperatorOver ppo)
        {
            ppo.num++;
            ppo.NumTest(ppo.num);
            return ppo;
        }       
        public delegate void NumEvHndlr(object sender, EvArg e);
         public void Reset() {
            num = 0;
        }
    }
    public class EvArg : EventArgs
    {
        private int _Num;
        public EvArg(int num) { this._Num = num; }

        public int NUMget
        {
            get { return _Num; }
        }
    }
}

Use in the form class:-
using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            plus.NumEv += new PlusPlusOperatorOver.NumEvHndlr(this.EvHppn);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            plus++;
            //plus.NumEv -= this.EvHppn;
        }
        PlusPlusOperatorOver plus = new PlusPlusOperatorOver(0);
        int t = 0;
        private void EvHppn(object sender, EvArg e)
        {
            MessageBox.Show(sender.ToString() + e.NUMget);
            label1.Text = t.ToString();
            plus.Reset();            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            plus++;
        }
       
    }
}


I hope this will help you..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900