2017-04-27 5 views
-1

私は前回と同様の質問がありますが、壁に向かってどれだけ頭を打っても解決策は出ません。問題は、メッセージボックスが一度だけ開く必要があるときに、メッセージボックスが何度も作成され、documentCompletedから退会してから終了することです。再度、感謝します!{again} C#MessageBoxが複数回作成されました

private void textBox4_TextChanged(object sender, EventArgs e) 
{ 
    if (textBox4.Text.Length >= 3) 
    { 
     timer1.Enabled = true; 
    } 
} 

private void timer1_Tick_1(object sender, EventArgs e) 
{ 
    if (textBox4.Text != "") 
    { 
     webBrowser1.ScriptErrorsSuppressed = true; 
     webBrowser1.Navigate("https://worldofwarcraft.com/en-gb/search?q=" + textBox4.Text); 

     webBrowser1.DocumentCompleted += GetImg; //sub here 
    } 
} 

private void GetImg(object sender, WebBrowserDocumentCompletedEventArgs e) 
{ 
    string img_url = ""; 
    foreach (HtmlElement el in webBrowser1.Document.GetElementsByTagName("div")) 
    { 
     if (el.GetAttribute("className") == "Avatar-image") 
     { 
      img_url = (el.OuterHtml).Substring(el.OuterHtml.IndexOf("https")); 
      img_url = img_url.Substring(0, img_url.IndexOf(")")); 
      pictureBox1.ImageLocation = img_url; 
     } 
     else if (el.GetAttribute("className") == "Character-level") 
     { 
      textBox5.Visible = true; 
      label7.Visible = true; 
      string lvl_url = ""; 
      lvl_url = (el.InnerHtml).Substring(3); 
      lvl_url = lvl_url.Substring(0, lvl_url.IndexOf("<")); 
      textBox5.Text = lvl_url; 
      DialogResult YESNO = MessageBox.Show("Is this your character?", "Select your char", MessageBoxButtons.YesNo, MessageBoxIcon.Question); 
      if (YESNO == DialogResult.Yes) 
      { 
       // clean up 
       webBrowser1.DocumentCompleted -= GetImg; //unsub here 
       pictureBox1.Enabled = false; 
       timer1.Dispose(); 
       break; 
      } 
     } 

    } 
} 

答えて

4

あなたはできるだけ早くあなたがtimer1_Tick_1メソッドを入力するか、タイマーが発火を維持し、あなたの方法たびに呼び出しますよう虚偽またはtimer1.Stop(呼び出しにtimer1.Enabledセット)のどちらかにする必要があります。

+0

私の友人は天才です:)。なぜこれが起こっているのかを説明するケア? – Nanyo

+0

もちろん、Start()またはEnable timer(タイマーを有効にする)を選択するまで、タイマーは永遠に起動します。 GetImgメソッドで "Dispose"を使用していますが、GetImgはDocumentCompletedでのみ呼び出されます(ダウンロードを開始してから時間がかかることがあります)。タイマーは複数回起動できますtimer1_Tick_1メソッドを複数回実行)。それが助けになりましたか? –

+0

ありがとう、解決済みとマークされています:) – Nanyo

関連する問題