【问题标题】:Reloading or refreshing WebBrowser control and Form in general一般重新加载或刷新 WebBrowser 控件和表单
【发布时间】:2013-02-24 16:32:16
【问题描述】:

我有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,还有其他解决方案吗?

【问题讨论】:

  • 不能在 WebBrowser 控件上调用 Refresh 方法吗?
  • 在哪里?如果我在 DisplayCaptcha() 中调用 id;它不工作。

标签: c# winforms webbrowser-control


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 在我的 DisplayCaptchas() 方法中尝试实现时它只显示空白,但通常它可以工作。我不知道你可以像 Javascript 一样使用它。
猜你喜欢
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 2010-12-04
  • 1970-01-01
  • 2016-05-28
  • 2012-05-18
  • 1970-01-01
  • 2020-12-12
相关资源
最近更新 更多