2017-01-11 9 views
-2

Windowsフォームを読み込んで、何らかの条件に基づいてラベルの値を変更します。しかし、フォームを実行すると、完全なロジックが実行され、フォームがロードされます。最後の条件に挿入されたラベル値が表示されます。c#Winforms UIを表示するだけでなくコードを実行します

フォームがロードされ、ロジックが実行されるときにラベル値が動的に変更される必要があります。私を案内してください。おかげ

bool filesPresent = false; 
string contentPath = "C:\\folder\\"; 
string downloadUrl = "http://a.com/file.rar"; 

public Loading() 
{ 
    InitializeComponent(); 
    Shown += Loading_Shown; 
} 

private void Loading_Shown(object sender, EventArgs e) 
{ 
    label1.text = "Checking Files..." 
    if (File.Exists(contentPath)) 
    { 
     if (File.Exists(contentPath + "config.xml")) 
     { 
      int fCount = Directory.GetFiles(contentPath, "*", SearchOption.TopDirectoryOnly).Length; 
      if (fCount >= 5) 
      { 
       filesPresent = true; 
      } 
      else filesPresent = false; 
     } 
     else filesPresent = false; 
    } 
    else filesPresent = false; 
    if (filesPresent == false) 
    { 
     label1.text = "Downloading Files..." 
     Directory.CreateDirectory(contentPath); 
     using (var client = new WebClient()) 
     { 
      client.DownloadFile(downloadUrl, contentPath + "aplus1.rar"); 
     } 
    } 
} 

private void Loading_Load(object sender, EventArgs e) 
{ 
} 
+0

'WebClient.DownloadFileAsync()'例えば:http://stackoverflow.com/questions/4172158/progress-bar-and-webclient –

+0

こんにちは、再び要件をチェックしてください。 –

+0

どのようなロジックですか?コードを表示するか、誰もあなたを助けることはできません。 – DonBoitnott

答えて

0
private void Loading_Shown(object sender, EventArgs e) 
     { 
       BeginInvoke(
      (MethodInvoker) delegate { label1.text = "Checking Files...";}); 

Task.Run(() => { 
      if (File.Exists(contentPath)) 
      { 
       if (File.Exists(contentPath + "config.xml")) 
       { 
        int fCount = Directory.GetFiles(contentPath, "*", SearchOption.TopDirectoryOnly).Length; 
        if (fCount >= 5) 
        { 
         filesPresent = true; 
        } 
        else filesPresent = false; 
       } 
       else filesPresent = false; 
      } 
      else filesPresent = false; 
      if (filesPresent == false) 
      { 
       BeginInvoke(
      (MethodInvoker) delegate { label1.text = "Downloading Files..."; }); 

       Directory.CreateDirectory(contentPath); 
       using (var client = new WebClient()) 
       { 
        client.DownloadFile(downloadUrl, contentPath + "aplus1.rar"); 
       } 
      }); 
     } 
+0

ありがとうございます...さらなる条件に同じものを使用できますか? –

+0

Iあなたが何を意味するのかわからない... –

+0

私は今、私は別のフォームを呼びたいと思っていました。だから私はthis.visible = false; エラーを示しました追加情報:クロススレッド操作が有効ではありません:作成されたスレッド以外のスレッドからアクセスされたコントロール '読み込み'。 –

関連する問題