Click here to Skip to main content
15,892,809 members
Articles / Programming Languages / C#
Article

Dartboard User Control in C#

Rate me:
Please Sign up or sign in to vote.
4.70/5 (17 votes)
26 Oct 20032 min read 96.8K   2.8K   26   12
Shows a dartboard on which you can click and which returns the score thrown

Image 1

Introduction

When I play darts with my friends at home we have a laptop with us to hold scores and statistics. We used Excel to do the job, but I was encouraged to build a more sophisticated solution. So, I came up with the idea to click on a dartboard picture to calculate the scores thrown and the statistics. So this article shows the usercontrol and the delegates/events I used.

Using the code

There are 2 interesting parts in this challenge:

  • How to determine the score based on a click somewhere on the dartboard;
  • Using delegates and events;

For the first part I used the middle of the bull (that is the middle of the picture) as my base point (0,0). Since I know the X and Y coordinate I can calculate the degrees by using the atan() function.

C#
double  dblHoekInRadians  = System.Math.Atan2(  posY ,  posX  );
double  dblHoekInGraden  = dblHoekInRadians * 180 / System.Math.PI;

When I know the degrees I know which number was clicked. The dartboard has 20 slices of equal size. So, each slice is of 360 / 20 = 18 degrees. Remember, the middle is my base, so number 13 is between 9 and 27 degrees and number 4 is between 27 and 45.

The next thing to do is to check which ring was clicked (bull, triple, double or else just single). I calculated the distince from the base (0,0) to the clicked X,Y coordinate. Now, the Pythagoras guy is entering our life!

C#
double  dblLengte  = System.Math.Sqrt(  posX * posX  +  posY * posY );
int  intLengte  = (int)System.Math.Floor(  dblLengte  );

Well, this was the hard part.

The next thing is to develop some events to be raised. I chose to have 4 events: NoScoreThrown, SingleScoreThrown, DoubleScoreThrown and TripleScoreThrown. I declared them like this:

C#
public event NoScoreThrownEventHandler  NoScoreThrown ;
protected virtual void OnNoScoreThrown( DartbordEventArgs  e  )
{
  if(  NoScoreThrown  != null )
  {
    NoScoreThrown( this,  e  );
  }
}

For these events I used 4 delegates:

C#
//Declare the delegates for each event
public delegate void NoScoreThrownEventHandler( object  sender , 
  DartbordEventArgs  e  );
public delegate void SingleThrownEventHandler( object  sender , 
  DartbordEventArgs  e  );
public delegate void DoubleThrownEventHandler( object  sender , 
  DartbordEventArgs  e  );
public delegate void TripleThrownEventHandler( object  sender , 
  DartbordEventArgs  e  );

I also defined my own EventArgs with specific data about the scores and throws.

C#
/// <SUMMARY>
/// Holds data about the score and throw
/// </SUMMARY>
public class  DartbordEventArgs  : EventArgs
{
  private readonly int  mintScore ;
  private readonly string  mstrScore ;
  private readonly int  mintThrow ;

  //Constructor
  public DartbordEventArgs( int  Score , string  ScoreText , int  Throw  )
  {
    this.mintScore =  Score ;
    this.mstrScore =  ScoreText ;
    this.mintThrow =  Throw ;
  }

  #region Properties
  public int  Score
  {
    get
    {
      return mintScore;
    }
  }

  public string  ScoreText
  {
    get
    {
      return mstrScore;
    }
  }

  public int  Throw
  {
    get
    {
      return mintThrow;
    }
  }
  #endregion

}

Based on the score thrown I raise the right event. I used a class variable (boolean) to know whether I hit a single, double, triple or noscore.

C#
//Raise the event(s)
DartbordEventArgs  dea  = new DartbordEventArgs(  mintScore ,  
  mstrScoreText ,  mintThrow  );

if(  mIsTriple  ) OnTripleThrown( dea );
if(  mIsDouble  ) OnDoubleThrown( dea );
if(  mIsSingle  ) OnSingleThrown( dea );
if(  mIsNoScore  ) OnNoScoreThrown( dea );

That's basically it. Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer Mavention
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Praiseexcellent! Pin
Southmountain17-Jun-22 7:05
Southmountain17-Jun-22 7:05 
QuestionTablet PC Support? Pin
iansinke4-May-07 8:51
iansinke4-May-07 8:51 
GeneralDartBoard Control on VB.Net Pin
Spongebob19736-Oct-05 19:04
Spongebob19736-Oct-05 19:04 
Generalvery nice but suggestion on code Pin
mat0014-Mar-05 22:53
mat0014-Mar-05 22:53 
GeneralProblem Pin
Member 5387422-Jun-04 6:12
Member 5387422-Jun-04 6:12 
GeneralReally nice! Pin
Jon Newman28-Oct-03 16:00
Jon Newman28-Oct-03 16:00 
GeneralCool idea, one suggestion Pin
Heath Stewart27-Oct-03 6:23
protectorHeath Stewart27-Oct-03 6:23 
GeneralRe: Cool idea, one suggestion Pin
Roger Alsing27-Oct-03 19:59
Roger Alsing27-Oct-03 19:59 
GeneralRe: Cool idea, one suggestion Pin
Heath Stewart28-Oct-03 2:15
protectorHeath Stewart28-Oct-03 2:15 
GeneralReally cool idea! Pin
Rogier Reedijk26-Oct-03 22:11
Rogier Reedijk26-Oct-03 22:11 
GeneralRe: Really cool idea! Pin
O c t a v i e26-Oct-03 23:55
O c t a v i e26-Oct-03 23:55 
GeneralRe: Really cool idea! Pin
Rogier Reedijk27-Oct-03 20:11
Rogier Reedijk27-Oct-03 20:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.