0
private: System::Void link1_Click(System::Object^  sender, System::EventArgs^  e) 
     {
        navigate(url1);
     }


private: System::Void navigate(System::String^ url)
     {
             for each ( System::Windows::Forms::HtmlElement^ webpageelement in webBrowser->Document->All )
             {
                 if (webpageelement->GetAttribute("u"))
                    this->webBrowser->Document->GetElementById("u")->SetAttribute("value", url);
             }

             for each ( System::Windows::Forms::HtmlElement^ webpageelement in webBrowser->Document->All )
             {
                 if (webpageelement->GetAttribute("value") == "Go")
                     webpageelement->InvokeMember("click");
             }
     }

我有许多其他按钮调用函数 navigate() 但我只会发布一个因为除了 url 的值之外它们都是相同的。我的问题是,如果我单击按钮,即使表单中不存在网页元素(“u”),如何让我的应用程序停止退出/出现错误。因为如果我单击它,即使表单尚未完全加载,我也会收到消息框说未处理的异常错误,我想将其更改为其他内容或忽略它并让我的应用程序再试一次。谢谢

4

2 回答 2

1

对这种简单的检查使用异常处理是一种矫枉过正的做法。只需执行以下操作:

HtmlElement ele = this->webBrowser->Document->GetElementById("u");
if (ele != null)
  ele->SetAttribute("value", url);
于 2013-04-03T08:57:29.800 回答
0

使用tryandcatch可以给你一些基本的方法......例如

for each ( System::Windows::Forms::HtmlElement^ webpageelement in webBrowser->Document->All )
{
    try 
    {
        if (webpageelement->GetAttribute("u"))
        this->webBrowser->Document->GetElementById("u")->SetAttribute("value", url);
    }
    catch (Exception^ ex)
    {
        // Do something here, can be blank...
        // This will try the above code, if it doesn't work it will continue without any error popup
    }
}
于 2013-04-03T08:53:28.160 回答