Tag Archives: event handlers

Remove all event handlers from an event in C#

I found this technique on this Microsoft forum. When you click the program’s Add button, the following code installs several event handlers. private void btnAdd_Click(object sender, EventArgs e) { btnClickMe.Click += btnClickMe_Click; picCanvas.Click += picCanvas_Click; picCanvas.MouseClick += picCanvas_MouseClick; picCanvas.MouseDown += … Continue reading

Posted in events | Tagged , , , , , , | Comments Off on Remove all event handlers from an event in C#

Make a class define and raise events in C#

This example uses a BankAccount class to manage a bank account (and to show how to define and raise events). If the program tries to remove more money from the account than its current balance, the BankAccount object raises an … Continue reading

Posted in classes, events | Tagged , , , , , , , , , , , , | 5 Comments

Make a standard color palette in C#

The MakeColorPalette method shown below builds an array of PictureBoxes with BackColor properties set to a standard color palette. // Make a color palette on the given parent. private void MakeColorPalette(Control parent, int x, int y, System.EventHandler event_handler) { Color[] … Continue reading

Posted in controls, drawing, graphics | Tagged , , , , , , , , , , , | 1 Comment

Let controls share event handlers in C#

Sometimes it would be useful to let controls share event handlers. Often a program has several controls that do almost the same thing. It would be nice if they could share the same event handler to increase code reuse, but … Continue reading

Posted in controls, methods | Tagged , , , , , , , , , , , , | Comments Off on Let controls share event handlers in C#

Use an event handler for multiple controls in C#

Suppose you have several controls with event handlers that perform similar complex tasks. There are two general approaches for minimizing duplicated code. The first is to move the common code into a separate method and make all of the event … Continue reading

Posted in controls, Event | Tagged , , , , , , , | 2 Comments