0

コードを作成するアプリケーションを作成するにはどうすればよいですか。ユーザーがアプリケーションを開いた場合、他のユーザーはアプリケーションを使用できず、アプリケーションMessageBoxを使用するユーザー名を取得できます。このためにForm_Load、try catch を使用してイベントを使用すると、機能します。2 番目のユーザーは、他のユーザーの名前のメッセージを受け取りますが、この後フォームが開きます。このメッセージの後、フォームが開かないようにしたいです。

私のコード:

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

1 つの提案として、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

あなたが何をしているかについてはコメントしません...

...しかし、フォームを閉じるのは簡単です:

    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 に答える