2013-07-16 69 views
0

如何編寫創建代碼的應用程序。我希望如果用戶打開應用程序,其他用戶不能使用該應用程序,並獲得使用該應用程序的用戶名的MessageBox。爲此,我使用帶有try catch的事件並且它可以工作。第二個用戶收到帶有其他用戶名稱的消息,但在此之後打開表單。我希望表單在此消息後不能打開。我如何取消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(); 
    } 
} 

回答

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

因爲形式的加載一起跳過這是更有效。

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

一個建議,你可以這樣做Init()本身的必要加載並關閉Load本身的表單本身。這是最好的方式。

this.Close();