Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
546 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Software ==> | Visual C# (55 exercises) |
Topic ==> | Delegates and events (2 exercises) |
Level ==> | Average difficulty |
Subject ==> | C# training |
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.
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:
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:
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!
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 below!
First create an additional button in your form to allow movement to be relative to the top left of the screen:
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!
You can find other training resources for the subject of this exercise here:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.