2013-04-03 55 views
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"); 
      } 
    } 

我有很多其他按鈕,調用函數的導航(),但我只會發佈一個coz他們都是相同的,除了url的值。我的問題是我如何使我的應用程序停止退出/有錯誤,如果我點擊按鈕即使網頁元素(「你」)不存在的形式。因爲如果我點擊它即使表單還沒有完全加載我得到messagebox說未處理的異常錯誤,我想改變它到別的東西或只是忽略它,讓我的應用程序再試一次。 THXC++ winform錯誤處理

+0

你將不得不修復你的代碼,它不能通過設計工作。將代碼移至DocumentCompleted事件的事件處理程序。 –

回答

0

使用trycatch可以給你一些基本的方法...例如

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 
    } 
} 
+0

喜有錯誤THX的答案,但即時通訊,當我試試這個: 「無法拋出或通過值或引用抓管理對象」和 「不能被捕獲的析構函數和/或拷貝構造函數是不可訪問「 –

+1

您需要在catch子句中放置一個'^':'catch(Exception^ex)'。請參閱編輯。 –

1

使用異常對於這樣簡單的檢查處理是一個矯枉過正。只需做到以下幾點:

HtmlElement ele = this->webBrowser->Document->GetElementById("u"); 
if (ele != null) 
    ele->SetAttribute("value", url);