Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

Making an ActiveX Control for Dice

Lesson #1 - Concept & Design Overview

This is a tutorial on how to make an ActiveX Control (.OCX) that you can use in dice-related games (like Yahtzee) as well as other programs.  As we create the dice control, we'll explore some of the basics of creating any ActiveX Control.  Even if you're not interested in a dice control, you can use most of the techniques for an ActiveX Control of your own creation.

This tutorial does assume you have a basic grasp of some VB concepts otherwise this tutorial would have to be many time larger to define all related issues. In particular we use one of VB's wizards to generate some code. If I was to create all the code by hand, or explain it line by line, I'd be writing a book instead of a few short lessons. So if you know that the 'wizard' I'm talking about isn't a funny little guy living in Oz you should be ok. Feel free to ask any questions in the ActiveX forum in the VB Forums section of this site.

Let's start by outlining some basic features and functionality that we'll want the dice control to provide.  For purposes of this project, we'll assume a standard, six-sided cube with values ranging from one to six.  At the very least, the control should display the appropriate number of "spots", which represent the ordinal values on each face of the dice.  Additionally, these spots should be placed in a familiar pattern.  I've chosen the following spot placements for each of the six ordinal values:

The control will need a Value property to represent the ordinal value of the dice.  Any time the Value property is changed, we'll change the spots to match the new value.  It would also be nice if the control could randomly assign itself a value.  This would be of great help when rolling the dice.  We'll include a Random method that will set the Value property to a random value from one to six.

To keep it interesting we can add some color properties.  We'll allow the users of the control (called "clients") to assign color values for the border, background, spots and the inside of each spot.  We'll call these properties BorderColor, BackColor, SpotColor and FillColor, respectively.  This will allow us to make some colorful dice:

If the user assigns the same color to the border as the background, it will appear as if there isn't a border.  Even so, it's customary to expose a property to control the presence (or absence) of a border.  This property is often named BorderStyle, so we'll use that name too.  

We'll use an enumeration to assign values to the BorderStyle property.  We'll call the enumeration EBorderStyles and include the members bsNone and bsSolid.

We'll implement two events - Click and DblClick.  As expected, the control will raise these events when the user clicks or double-clicks the mouse while over the dice control.  To disable click events, we'll also include an Enabled property.  When set to False, this will prevent any click events from firing.

The last feature we'll implement is the ability to re-size the control.  As the dice get bigger, the spots should get bigger and the border should get thicker.  This feature will be the most difficult to implement.  We'll talk more about it when we add this feature to the control.  For now, take a look at a small sampling of what the dice control can be sized to look like:

Design Summary

To recap, the following is a list the properties, methods and events that the dice control will expose:
    Properties:
    BackColor
    The color of the dice's background.
    BorderColor
    The color of the dice's border.
    BorderStyle
    Determines if the dice displays a border.
    Enabled
    Determines if either of the mouse click events can be fired.
    FillColor
    The color of the inside of the dice's spots.
    SpotColor
    The color of the border of the dice's spots.
    Value
    The ordinal dice value - in the range of 1 to 6.
    Methods:
    Random
    A function that randomly selects a value from 1 to 6, assigns it to the Value property and returns the value.
    Events:
    Click
    Fired when the user clicks the dice control.  This event won't fire if the Enabled property is set to False.
    DblClick
    Fired when the user double-clicks the dice control.  This event won't fire if the Enabled property is set to False.
We'll use this list as we incorporate each feature - to be sure we don't forget anything.

What's Next?

In the next lesson, we'll talk about how to create ActiveX Controls and take a closer look at some of the events that VB raises when controls are created and destroyed.

© 2000, Brian P. Duckworth


[Dice 1] [Dice 2] [Dice 3] [Dice 4] [Dice 5] [Dice Bitmaps] [Dice Project] 




Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.