0

如何编写创建代码的应用程序。我希望如果用户打开应用程序,其他用户不能使用该应用程序并获得使用该应用程序MessageBox的用户名。为此,我使用Form_Load带有 try catch 的事件并且它有效。第二个用户收到一条带有其他用户名称的消息,但在此之后表单打开。我希望在此消息之后表单不打开。

我的代码:

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();
    }
}
4

3 回答 3

0

给你的一个建议是,你可以在 Init() 本身进行必要的加载,然后关闭表单而不是 Load()。这是更好的方式。

this.Close();
于 2013-07-16T08:47:45.180 回答
0

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.

于 2013-07-16T08:40:22.243 回答
0

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();
        }
    }
于 2013-07-16T08:41:19.827 回答