-1

I am trying to make a serial key registration program. How do I make it so that if they type in a serial key from a text document in my resources an action will happen? I want it sonthat if someone types in a certain serial key that it is in a text document, and presses register, and action will be triggered.

pnuts
  • 58,317
  • 11
  • 87
  • 139
Evan H.
  • 149
  • 1
  • 2
  • 12
  • Code wil differ depending on whether your application is in ASP.NET, WinForms, WPF, ... The idea is always the same though: Add an eventhandler to the TextField.Change(d) event. (or on KeyPress, KeyDown, ... if you want to check on every keystroke) – Laoujin Sep 02 '12 at 16:55
  • Could you help me do this. I need the code. And it is a windows forms application. – Evan H. Sep 02 '12 at 17:07

2 Answers2

2

In WinForms ist is very easy to create event handlers. In the forms designer select the serial key textbox. In the properties window switch to events by clicking the button with lightning symbol. Here you have the choice between many events. The TextChanged event is a good one. It fires each time a character is entered or deleted. Depending on what you want to do the different Key... events the Leave event or Validated or Validating events may be useful. If you are using a button, just double click on the button to get a handler for the Click event.

Double click in the empty field next to the event name. Visual Studio automatically creates an event handler for you.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
0

Creating a program that checks if a certain TextBox entry is present in an embedded resource is pretty easy in .NET. But...

Note that .NET is not a good platform for this sort of thing altogether. Implementing it would be very easy compared to ensuring that no one can read your document/resource (which would contain all valid keys) with freely available software such as ILSpy

Also check this question.

A simple implementation could be something like this:

TextBox ValidationKey = new TextBox();

Binding the EventHandler:

ValidationKey.KeyPress += new KeyPressEventHandler(ValidationKey_KeyPress);

Implementation of the EventHandler:

private void ValidationKey_KeyPress(object sender, KeyPressEventArgs e)
{
    bool isKeyValid = MyResourceFile.Keys.Contains(ValidationKey.Text);
    MessageBox.Show(string.Format("Is it a valid key: {0}", isKeyValid.ToString()));
}

This is a very naïve implementation in that when the user enters just one character it is very likely to pass the test. You might want to use a RegExp or first check that the ValidationKey.Text.Length equals the length of your valid keys.

Community
  • 1
  • 1
Laoujin
  • 9,962
  • 7
  • 42
  • 69
  • 3
    .NET is not a language and it is very easy to handle events. – Olivier Jacot-Descombes Sep 02 '12 at 17:24
  • @Olivier Jacot-Descombes: The OP seriously changed the intial question a few times. True, binding an event is *very* easy, certainly when compared to creating a registration key form where there will be a resource containing all valid keys. He will want to do this in ASP.NET and not winforms or in a completely different language altogether. That aside you are correct: Should I replace "language" with "platform" or what? – Laoujin Sep 03 '12 at 07:24
  • .NET is a platform. The two main languages are C# and VB but there are compilers for many other languages. Why the heck should .NET not be good for handling events? (By the way, I did not downvote you.) – Olivier Jacot-Descombes Sep 03 '12 at 13:49
  • Rereading that sentence I understand the confusion: The OP initially asked *how* to bind an event in .NET. I didn't mean binding is difficult, I meant that .NET is not a good choice for a registration key validation program since .NET assemblies can so easily be reverse engineered... You gave some good pointers about the **HOW**, I tried to convince the OP to try a different approach since a .NET solution will be terribly easy to 'hack'. – Laoujin Sep 03 '12 at 13:58
  • After your edit your statement is clearer now. However, relaying on a secret algorithm does not create true security. A good security algorithm should be known publicly and its security should be inherent to the algorithm and not to the fact that people do not know the algorithm. The algorithms of the worlds most secure cryptographic procedures are known publicly. – Olivier Jacot-Descombes Sep 03 '12 at 14:11