Click here to Skip to main content
15,885,216 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Product Key Dialog

Rate me:
Please Sign up or sign in to vote.
4.24/5 (5 votes)
12 Sep 2013CPOL1 min read 16.8K   377   8   6
Dialog for product key entry
ProductKeyBox dialog control

Introduction

This control allows for a simple and easy way to allow the user to enter a product key.

Using the Code

Image 2

Constructor : The Code

ProductKeyBox(1,2,3)
KeyPartLength int Controls text length of each textbox ('1' above)
Icon Image Controls the image displayed in the top left corner ('2' above)
Title string Controls the dialog title ('3' above)
KeyParts string[] Returns the text from a given textbox based on array index value
Submit Button Button Exits the dialog and returns DialogResult.OK, uses the textboxes to set KeyParts
Clear Button Button Clears all textbox values in dialog

If you don't have an icon or title, you can pass in null.

Class View : The Control

Image 3

This control has three parameters you can pass in: KeyPartLength, Icon, DialogTitle. KeyPartLength controls how long each section of the key can be. You can redesign the dialog to add more or less KeyParts and TextBoxes based on the length of your product key. Icon is the image displayed in the top left of the dialog which inherits from System.Drawing.Image namespace.

Design : Your UI

Image 4

Code Behind : Your Code

C#
private void button1_Click(object sender, EventArgs e)
{
	ProductKeyBox.ProductKeyBox box = new ProductKeyBox.ProductKeyBox(6, null, "");
        if (box.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
        	label1.Text = "KeyPart[0]: " + box.KeyParts[0];
                label2.Text = "KeyPart[1]: " + box.KeyParts[1];
                label3.Text = "KeyPart[2]: " + box.KeyParts[2];
                label4.Text = "KeyPart[3]: " + box.KeyParts[3];
        }
}

Points of Interest

I attempted to remake this control for use in WPF projects but I discovered it was a bit more challenging than originally thought. I also thought about using a separate class instead of a string array to store the product key sections but found it difficult to add in. Here's the code if you wish to try:

C#
public class ProductKey
    {
        private static DateTime endDate, startDate;
        public ProductKey()
        {

        }
        public ProductKey(DateTime startDate)
        {
            StartDate = startDate;
        }
        public ProductKey(DateTime startDate, DateTime endDate)
        {
            StartDate = startDate;
            EndDate = endDate;
        }
        public ProductKey(DateTime startDate, DateTime endDate, string[] keyParts)
        {
            StartDate = startDate;
            EndDate = endDate;
            KeyParts = keyParts;
        }
        public ProductKey(string[] keyParts)
        {
            KeyParts = keyParts;
        }
        public string[] KeyParts
        {
            get;
            set;
        }
        public DateTime StartDate
        {
            get
            {
                return startDate;
            }
            set
            {
                startDate = value;
            }
        }
        public DateTime EndDate
        {
            get
            {
                return endDate;
            }
            set
            {
                endDate = value;
            }
        }
        public int DaysRemaining
        {
            get
            {
                if (startDate != null && endDate != null)
                    return endDate.Subtract(startDate).Days;
                else
                    return 0;
            }
            set
            {
                endDate = startDate.Add(new TimeSpan(value, 0, 0));
            }
        }
    }

History

  • Initial post: 9/11/2013

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
United States United States
I started learning HTML in 7th grade (2007) then went on to learn any and everything I could.
HTML/CSS/JavaScript/ActionScript/ASP.Net (6 years)
Basic (5 years)
C/C++ (4 years)
Java (4 years)
C# (3 years)
VB (2 years)

Out of all of these I have to say C# is by far my favorite since its wide spread, works for almost any solution and allows me to use Visual Studio.

Comments and Discussions

 
QuestionAuto tab? Pin
Ravi Bhavnani13-Sep-13 5:13
professionalRavi Bhavnani13-Sep-13 5:13 
AnswerRe: Auto tab? Pin
shelby6713-Sep-13 5:41
shelby6713-Sep-13 5:41 
GeneralRe: Auto tab? Pin
Ravi Bhavnani13-Sep-13 5:49
professionalRavi Bhavnani13-Sep-13 5:49 
GeneralMy vote of 2 Pin
ascgui12-Sep-13 19:51
ascgui12-Sep-13 19:51 
GeneralRe: My vote of 2 Pin
shelby6712-Sep-13 20:02
shelby6712-Sep-13 20:02 
SuggestionRe: My vote of 2 Pin
Maciej Los13-Sep-13 2:17
mveMaciej Los13-Sep-13 2:17 

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.