2012-06-22 51 views
4

我有以下代碼:取消形式負載

該呼叫的第二形式

private void updateToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Update fm = new Update(); 
      fm.ShowDialog(); 
     } 

這是構造

public Update() 
    { 
     InitializeComponent(); 
    } 

這是負載

private void Update_Load(object sender, EventArgs e) 
    { 
     String ver = checkver(); 
     if (ver == "update") 
     { 
      if (RemoteFileExists(dlUrl) == true) 
      { 
       WebClient webClient = new WebClient(); 
       webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
       webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
       webClient.DownloadFileAsync(new Uri(dlUrl), ""); 
      } 
      else 
       MessageBox.Show("An error occurred. Please try later."); 
     } 
     else if (ver == "newest") 
     { 
      MessageBox.Show("You are currently using the newest version."); 
      this.Close(); 
     } 
     else 
     { 
      this.Close(); 
     } 
    } 

我的問題是,當功能n結果爲2或3,表單顯示爲毫秒,然後關閉(閃爍)。我希望表單不閃爍。可能嗎?

我試圖用this.Hide()THIS.VISIBLE =假但沒有任何幫助。

編輯:我把原來的代碼 EDIT2:把更多的代碼

+0

笑什麼情況?爲什麼我不能寫「你好人」? :) – a1204773

+0

將代碼放在窗體構造函數中,這會在form_load事件發生之前調用(取決於funct()中發生了什麼......如果它依賴於訪問尚未初始化的組件,那麼這會贏得'工作)。 –

+0

您應該只調用一次funct(),以便您切換的值保證是相同的,除非您特別想要它可以更改的可能性。 – 3Pi

回答

8

可以隱藏在裝貨前的表單,然後將其設置回看到你的if else條件。 e.g:

  MyForm myForm = new MyForm(); 
      myForm.Opacity = 0; 
      myForm.Show(); 

然後:

if (ver == "update") 
     { 
      if (RemoteFileExists(dlUrl) == true) 
      { 
       myForm.Opacity = 100; 
       ... 

      } 
      else 
       MessageBox.Show("An error occurred. Please try later."); 
     } 
     else if (ver == "newest") 
     { 
      MessageBox.Show("You are currently using the newest version."); 
      this.Close(); 
     } 
     else 
     { 
      this.Close(); 
     } 
+0

謝謝:) :) – a1204773

+3

@Loclip:雖然這個解決方案有效,但你真的不應該在窗體內部調用'checkver'。這是屬於一個單獨的類中的邏輯,在決定是否顯示錶單之前應該檢查它。將表單的不透明度設置爲0是一種醜陋的解決方法。 –

+0

但是沒關係......躲着同樣的東西 – a1204773

0

也許隱藏它,然後再只顯示它,如果本功能()== 「1」:

private void Form_Load(object sender, EventArgs e) 
{ 
    this.Close(); 

    if (funct() == "1") 
     MessageBox.Show("Something"); 
} 
1

你應該在你選擇首先打開表格之前,先做任何檢查你的表演。

因此,像:

​​
0

試試這個

private void Form_Load(object sender, EventArgs e) 
{ 
    switch(funct()) 
    { 
     case 2: 
     this.Close(); 
      break; 
     case 3: 
      this.Close(); 
      break; 
    default: 
      MessageBox.Show("Something"); 
    } 
} 
1

我認爲Update_Load是你FormLoad處理程序?在已經顯示您的表格後,這稱爲。如果你不想展示它,那就太遲了。更改updateToolStripMenuItem_Click這樣:

String ver = checkver(); 
if (ver == "update") 
{ 
    if (RemoteFileExists(dlUrl)) 
    { 
     Update fm = new Update(); 
     fm.ShowDialog(); 
    } 
    else 
     MessageBox.Show("An error occurred. Please try later."); 
} 
else if (ver == "newest") 
{ 
    MessageBox.Show("You are currently using the newest version."); 
} 

,改變你的Update_Load到:

WebClient webClient = new WebClient(); 
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged); 
webClient.DownloadFileAsync(new Uri(dlUrl), ""); 
2

這樣做的最好辦法:

private void Form_Load(object sender, EventArgs e) 
{ 
    switch(funct()) 
    { 
     case 2: 
     this.BeginInvoke(new MethodInvoker(this.Close)); 
      break; 
     case 3: 
     this.BeginInvoke(new MethodInvoker(this.Close)); 
      break; 
    default: 
      MessageBox.Show("Something"); 
    } 
}