2013-02-24 137 views
2

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

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

+0

你不能调用WebBrowser控件的Refresh方法吗? – rene 2013-02-24 16:44:16

+0

哪里?如果我在DisplayCaptcha()中调用id;它不工作。 – cikatomo 2013-02-24 19:38:16

回答

2

试试这个:

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

每当你按一下按钮,你应该得到一个新的形象。你可以把它放在你想要刷新的地方。

+0

当我尝试在我的DisplayCaptchas()方法中实现时,它只显示空白,但通常是有效的。我不知道你可以像Javascript一样使用它。 – cikatomo 2013-02-24 20:37:30