Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Product Key Dialog

0.00/5 (No votes)
12 Sep 2013 1  
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

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

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

Code Behind : Your Code

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:

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