C#.NET Events & Delegates - Best Practice

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()



Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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":


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.