C#.NET Events & Delegates - Best Practice

Posted on 2008-02-11
Medium Priority
Last Modified: 2008-02-20
Hi there Experts,

I am trying to develop a role playing game (just a simple one to learn about design patterns) where a player can either move or attack an enemy.

So assume that I have 2 sets of controls on my form. Each set of control is made up
of 4 buttons which are labelled as up, down, left, right. the 1st control
set represent movement and the 2nd control set represents attack. hence in
my code I will my code have something like shown below

private void moveUp_Click(object sender, EventArgs e)
{ moveUp(); refreshData; }

private void moveDown_Click(object sender, EventArgs e)
{ moveDown(); refreshData; }

private void moveLeft_Click(object sender, EventArgs e)
{ moveLeft(); refreshData; }

private void moveRight_Click(object sender, EventArgs e)
{ moveLeft(); refreshData; }

private void attackUp_Click(object sender, EventArgs e)
{ moveRight(); refreshData; }

private void attackDown_Click(object sender, EventArgs e)
{ attackDown(); refreshData; }

private void attackLeft_Click(object sender, EventArgs e)
{ attackLeft(); refreshData; }

private void attackRight_Click(object sender, EventArgs e)
{ attackRight(); refreshData; }

As you can see, there is repetition. The question is how can I improve
this code using event and delegates.  Pls note that I am new to events and delegates.

Maybe I should pose the question as .... "What would be the best approach to implement the above code?"

Ideally, each of the button click event should automatically run its respective method which is up,down,left,right and followed by the refreshData() method. So that whenever one of these buttons
is click, i would want to run the following code

private void action_Button_Click(object sender, EventArgs e)
run appropriate method for the button that is clicked()



Question by:singhch

Accepted Solution

kirankumar_goura earned 750 total points
ID: 20864900
Try this out ...
this.moveUp.Click += new System.EventHandler(this.movement_Click);
this.moveDown.Click += new System.EventHandler(this.movement_Click);
this.moveLeft.Click += new System.EventHandler(this.movement_Click);
this.moveRight.Click += new System.EventHandler(this.movement_Click);
Implementation of the event movement_Click follows ...
private void movement_Click(object sender, EventArgs e)
    Button bttn = sender as Button;
    if ( bttn.Name = "MoveUp")
      //Your code goes here
    } else if ( bttn.Name == "MoveRight")
     // Your code goes here
   } ....

Same applies to 2 set of controls.
Let me know if you have any queries...

Assisted Solution

StanicDejan earned 750 total points
ID: 20866746
Assign the same event handler to all the buttons.
'sender' argument will contain the control that triggered the event.

For example:

    private void button_Click(object sender, EventArgs e)
      if (sender == button1) MessageBox.Show("A"); else
      if (sender == button2) MessageBox.Show("B"); else
      if (sender == button3) MessageBox.Show("C");


Or, perhaps, using switch statement:

    private void button_Click(object sender, EventArgs e)
      switch ((sender as Control).Name)
        case "button1":
        case "button2":
        case "button3":



Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question