3

我有Form以编程方式创建的控件。其中之一是WebBrowser显示验证码图像。然后用户在文本框中输入验证码,如果错误,表单应该用新的验证码图像刷新。然后我再次尝试Form.Refresh()调用DisplayCaptcha(),但它没有用,所以我在这个(简化的)代码中解决了它:

public partial class Form3 : Form
{

    public Form3()
    {
        InitializeComponent();
        DisplayCaptchas();
    }

    private void DisplayCaptcha()
    {
        string captcha = "<style>html, body{{padding:0; margin:0 }}</style>" + 
            "<img src=\"http://www.reddit.com/captcha/{0}.png\"></img>";


            WebBrowser webBrowserNofap = new WebBrowser();
            webBrowserNofap.DocumentText = String.Format(captcha, iden);
            ......//rest of the code


    }


    private void button1_Click(object sender, EventArgs e)
    {

        if (wrongCaptcha)
        {
            this.Close();
            Form3 form3 = new Form3(); //this is how I solved the refreshing
            form3.Show();
        }
        else
        {
            Form4 form4 = new Form4();
            this.Close();
            form4.Show();
        }
    }
}

它有效,但这并不是真正令人耳目一新。我当时正在考虑DisplayCaptcha()再次删除控件,但不知道该怎么做。简而言之,除了关闭然后重新加载之外还有其他解决方案Form吗?

4

1 回答 1

3

尝试这个:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            webBrowser1.Navigate("about:blank");
            webBrowser1.Document.Write("<html><head><style>html, body{{padding:0; margin:0 }}</style></head><body><div id='divMain'>&nbsp;</div></body></html>");
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Random r = new Random();
            int number = 0;
            number = r.Next(0, 999999);
            string captcha = "<img src=\"http://www.reddit.com/captcha/{0}.png\"></img>";

            webBrowser1.Document.GetElementById("divMain").InnerHtml = string.Format(captcha, number);
        }
    }
}

每次单击按钮时,您都应该得到一个新图像。你可以把它放在你想刷新的地方。

于 2013-02-24T16:46:00.993 回答