Here's one very rudimentary way - write a file to a known location when they activate, then check for the existence of that file each time you load the form. If it's there, immediately show Form2. If not, give them the chance to activate.
Differing methods would be to save the activation status in the Registry or in a Database, or somewhere else, but the overall process is about the same
Sample code:
First, a method to get the path to the file we're going to create:
private string GetActivatedFilePath()
{
var appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var thisExeName = Path.GetFileNameWithoutExtension(
System.Reflection.Assembly.GetEntryAssembly().Location);
return Path.Combine(appDataPath, thisExeName, "Activated.txt");
}
Then a couple of methods to create the file (Activate()
), check if the file exists (IsActivated
), and delete the file (Deactivate()
):
private void Activate()
{
if (!IsActivated())
{
var filePath = GetActivatedFilePath();
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
File.Create(filePath);
}
}
private bool IsActivated()
{
return File.Exists(this.GetActivatedFilePath());
}
private void Deactivate()
{
if (IsActivated())
{
File.Delete(GetActivatedFilePath());
}
}
Then we can also create a method to show the second form, since we will be calling this in more than one place. I've modified this to first hide the current form, then show the second form as a dialog (which means they can't switch back to the main form and code will pause in the first form until the second one closes), and then close the first form when the second one closes:
private void ShowForm2()
{
Form2 n = new Form2();
this.Hide();
n.ShowDialog();
this.Close();
}
Now we can check if they're activated in our Form_Load
event, and if they are then immediately show the second form:
private void Form1_Load(object sender, EventArgs e)
{
// If they user is already activated, show the second form immediately
if (IsActivated())
{
ShowForm2();
}
}
And then your current code can also make use of these functions to activate the user. I'm assuming the code lives behind an Activate
button:
private void btnActivate_Click(object sender, EventArgs e)
{
bool activated = false;
if (textBox1.Text.Length > 7)
{
string mac = textBox1.Text;
string str1 = mac.Substring(4, 1);
string str2 = mac.Substring(5, 1);
string str3 = mac.Substring(7, 1);
string str4 = mac.Substring(2, 1);
string pattern = str1 + str2 + str2 + str3 + str4;
activated = textBox2.Text == pattern;
}
if (activated)
{
MessageBox.Show("Program activated!!!");
Activate();
ShowForm2();
}
else
{
MessageBox.Show("Wrong key");
}
}