2013-07-16 2 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(); 
관련 문제