How do I write a application that create a code. I want if a user open the application a other user don't can use the application and get a MessageBox
with the username who use the application. For this I use a Form_Load
event with try catch and it works. The second user get a message with the name of the other user but the form open after this. I want that the form not open after this message.
my code:
private void Form1_Load(object sender, EventArgs e)
{
try
{
var stream = File.Open("lock", FileMode.OpenOrCreate,FileAccess.Read);
global_stream = stream;
string username = User.GetUsername();
string machine = Computer.GetMachineName();
TextDatei datei = new TextFile();
datei.WriteFile("user_log", "Username: " + username + " Invoicennumber: "
+ machine);
CreateCode();
}
catch (Exception)
{
TextFile file = new TextFile();
string info = datei.ReadFile("user_log");
MessageBox.Show(info);
Application.Exit();
}
}
I'd make the loading of the form itself conditional instead of canceling it:
try
{
// Do your validation stuff here...
// The form will only show if the validation didn't throw.
Application.Run(new Form1());
}
catch (Exception)
{
TextFile file = new TextFile();
string info = datei.ReadFile("user_log");
MessageBox.Show(info);
}
This is more efficient since the loading of the form is skipped altogether.
I won't comment on what you're doing...
...but to close the form is simple:
private void Form1_Load(object sender, EventArgs e)
{
Boolean someCondition = true; //whatever check you're doing
if (someCondition)
{
MessageBox.Show("Some Condition Met");
this.Close();
}
else
{
this.InitialiseTheFormEtc();
}
}
One suggestion for you, You could do the necessary loading at Init() itself and close the form there itself than Load(). That's the way better to go with.
this.Close();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.