2014-12-03 3 views
0

WebBrowser 및 컨텍스트 메뉴가 포함 된 양식이 있습니다.C# Webbrowser 복사

사용자가 페이지의 텍스트를 마우스로 선택 (강조 표시)하지 않은 경우 컨텍스트 메뉴에서 복사 기능을 비활성화하려고합니다.

나는
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange; 
if (range != null || range.text.Trim() != "") 
    { 
     MessageBox.Show(range.text); 
     copyToolStripMenuItem.Enabled = true; 
    } 
    else 
    { 
     copyToolStripMenuItem.Enabled = false; 
    } 

을 사용하려고하지만 나를 위해 작동하지 않았다.

Selected Text Display Correctly

답변

0

Error: Empty Selection이 나는 ​​사람이

문제는 range.text했다 그것을 필요로하는 경우! = "null이

여기에 응용 프로그램을 디버깅 한 후이 문제에 대한 대답을 얻었다 "< - 문자열로 null null

private void contextMenu_Opening(object sender, CancelEventArgs e) 
     { 
      if (Clipboard.GetDataObject().GetFormats()[0] == System.Windows.Forms.DataFormats.StringFormat) 
      { 
       pasteToolStripMenuItem.Enabled = true; 
      } 
      else 
      { 
       pasteToolStripMenuItem.Enabled = false; 
      } 

      IHTMLDocument2 htmlDocument = MainBrowser.Document.DomDocument as IHTMLDocument2; 
      IHTMLSelectionObject currentSelection = htmlDocument.selection; 
      if (currentSelection.type == "Text") 
      { 
       IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange; 
       if (range.text != "null" || !String.IsNullOrEmpty(range.text.Trim())) 
       { 
        copyToolStripMenuItem.Enabled = true; 
       } 
       else 
       { 
        copyToolStripMenuItem.Enabled = false; 
       } 
      } 
      else 
      { 
       copyToolStripMenuItem.Enabled = false; 
      } 
     }