Exercise: Create delegates for moving buttons round a form

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

The answer to the exercise will be included and explained if you attend the Wise Owl course listed below!

Category ==> Visual C Sharp 2010  (65 exercises)
Topic ==> Delegates and events  (2 exercises)
Level ==> Average difficulty
Course ==> Adv C# | Classes & LINQ
Before you can do this exercise, you'll need to download and unzip this file (if you have any problems doing this, click here for help).

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Add the existing form called frmMovableButtons from the above folder into a new or existing project, as well as the class called MovableButton.

Make this the default form.  When you run your project, you should be able to move the random button round the screen by a given amount:

Moving button right

Clicking on this button would move the button 40 units to the right.

Time to introduce a delegate!  Delete and rename controls so that your form looks like this:

Single button movement in form

Clicking on the Move this amount button for this diagram will - when you've finished the exercise - move the random button 10 units right and down.

Add a distance field to your class:

class MovableButton

{

// distance to go down and right

int distance = 0;

public int Distance

{

get { return distance; }

set { distance = value; }

}

Now create a delegate in your class and a method to run it.  You should then be able to attach the following code to the Move this amount button:

// create a new movable button

MovableButton b = new MovableButton(btn);

 

// say how to move it

b.Distance = (int)nudMovement.Value;

 

// create new instance of delegate

MovingButton iWantToMoveItMoveIt = null;

 

// get it so that this delegate has two methods assigned to it

iWantToMoveItMoveIt += b.MoveDown;

iWantToMoveItMoveIt += b.MoveRight;

 

// now run the events associated with delegate

b.Move(iWantToMoveItMoveIt);

When you click on the Move this amount button, the random button should move by the entered distance!

Adding an adapter to the class

If you still have time and energy, you could try adding a MoveHome method to your class, which doesn't take any additional arguments, and writing an adapter so you can add this method to your delegate.  More help available overleaf!

First create an additional button in your form to allow movement to be relative to the top left of the screen:

From top left button

The From top left button will behave differently.

Add a new method called MoveHome to your MovableButton class:

public void MoveHome()

{

// move the button home

button.Top = 0;

button.Left = 0;

}

Now attach code to the From top left button so that it adds the MoveHome method to the delegate before adding calls to MoveRight and MoveDown.

You'll need to create an adapter for the MoveHome method which adapts the signature to take a single integer argument.

Test your system to check both buttons work!

This page has 0 threads Add post