{again} C#MessageBox创建了不止一次

时间:2017-04-27 01:07:22

标签: c# forms winforms messagebox

我有一个类似的问题,但无论我多少撞到墙上,解决方案都没有到来。问题是消息框创建的次数太多,只应打开一次,取消订阅documentCompleted然后退出。再次感谢!

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

    }
}

1 个答案:

答案 0 :(得分:4)

您需要将timer1.Enabled设置为false或在输入timer1_Tick_1方法时立即调用timer1.Stop(),否则计时器将每次都停止并调用您的方法。

相关问题