如何取消Form_Load?

时间:2013-07-16 08:29:43

标签: c# xcode forms stream messagebox

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

我的代码:

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

3 个答案:

答案 0 :(得分:0)

我将表单的加载设置为有条件而不是取消它:

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

由于完全跳过了表单的加载,因此效率更高。

答案 1 :(得分:0)

我不会评论你正在做什么 ...

...但要关闭表单很简单:

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

答案 2 :(得分:0)

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

this.Close();
相关问题