Click here to Skip to main content
Licence 
First Posted 26 Oct 2003
Views 71,435
Bookmarked 24 times

Dartboard User Control in C#

By | 26 Oct 2003 | Article
Shows a dartboard on which you can click and which returns the score thrown

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.

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!

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:

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

For these events I used 4 delegates:

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

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

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

About the Author

O c t a v i e

Web Developer
Mavention
Netherlands Netherlands

Member

Follow on Twitter Follow on Twitter


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionTablet PC Support? Pinmemberiansinke8:51 4 May '07  
GeneralDartBoard Control on VB.Net PinmemberSpongebob197319:04 6 Oct '05  
Generalvery nice but suggestion on code Pinmembermat00122:53 4 Mar '05  
GeneralProblem Pinmemberm@gic6:12 22 Jun '04  
GeneralReally nice! PinmemberJonny Newman16:00 28 Oct '03  
GeneralCool idea, one suggestion PineditorHeath Stewart6:23 27 Oct '03  
GeneralRe: Cool idea, one suggestion PinmemberRoger J19:59 27 Oct '03  
GeneralRe: Cool idea, one suggestion PineditorHeath Stewart2:15 28 Oct '03  
GeneralReally cool idea! PinsussRogier Reedijk22:11 26 Oct '03  
GeneralRe: Really cool idea! PinmemberO c t a v i e23:55 26 Oct '03  
GeneralRe: Really cool idea! PinsussRogier Reedijk20:11 27 Oct '03  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 27 Oct 2003
Article Copyright 2003 by O c t a v i e
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid