简体   繁体   中英

Raising an event when button clicked and subscribing it to eventhandler in another class?

I have a form.cs which contains a button and a texbtox, and also a class which handles the event raised when the button is clicked.

basically, when the button is clicked, it should raise an event and the eventHandler in Print class should print text to TboxPrint in the form

this is how my code looks like:

//Declare the delegate
public delegate void EventHandler(object sender, EventArgs e);

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    //event
    public event EventHandler Print;

    //Event caller, protected to prevent calling from other classes
    protected virtual void OnPrint(System.EventArgs e)
    {
        if (Print != null) Print(this, e);
    }

    //raising event
    public bool print_action(object value)
    {
        OnPrint(EventArgs.Empty);
        return true;
    }

    public void BtnPrint_Click(object sender, EventArgs e)
    {
        PrintClass p = new PrintClass();
        Form1 s = new Form1();

        s.Print += p.printEventHandler;
        print_action(true);
    }
}

and the class with method handling the event is:

class PrintClass
{
    public void printEventHandler(object sender, EventArgs e)
    {

        string text = "Print Event occured";
    }
}

Apparently nothing is raised.. i believe the way im raising the event or subscribing to the event handler is wrong. and how i pass the text in eventhandler back to form?

any help is appreciated.. thanks

You simply need and extra event subscription to Button.Click event

   private void Form1_Load(object sender, EventArgs e)
   {
        p = new PrintClass();
        button1.Click += cls.printEventHandler;
   }

To handle all the buttons on the form you can write a simple snippet like

public Form1()
{
    InitializeComponent();

    foreach (Button btn in Controls.OfType<Button>())
    {
        btn.Click += cls.printEventHandler;
    }
}

To know which button was clicked you can write PrintClass as

class PrintClass
{
    public void printEventHandler(object sender, EventArgs e)
    {

         Button btn = (Button) sender;
         //btn.Name <-- Control name;
         //btn.Text<-- Control Text;

    }
}

One thing that I am not understanding is, why do you need a extra class if you need to output the result on the same form.

My suggest would be, not to create an extra class just for handling all the Button.Click event

This can work the way you want : I am removing the need for extra class

    public Form1()
    {
        InitializeComponent();

        foreach (Button btn in Controls.OfType<Button>())
        {
            btn.Click += HandleAllButtonClicks;
        }
    }

    private void HandleAllButtonClicks(object sender, EventArgs e)
    {
        Button btn = (Button) sender;
        TboxPrint.AppendText(String.Format("Button Clicked : Name = {0}, Text = {1}", btn.Name, btn.Text));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM