Hi,
I have a requirement to Enable/Disable a Button based on Selecting/UnSelecting respectively of WebBrowser Document text. I am using 'HTMLDocumentEvents_onselectionchangeEventHandler' to achieve this but sometimes the Button is not getting Enabled on Selecting the text in the document, sometimes the button is Enabled even No text is selected in the document and sometimes it works absolutely fine.
I am using the below code,
private void webBrowser_LoadCompleted(object sender, NavigationEventArgs e)
{
IHTMLDocument2 htmlDocument;
htmlDocument = GetSelectedWebBrowser().Document as IHTMLDocument2;
HTMLDocumentEvents_Event iEvent;
iEvent = (HTMLDocumentEvents_Event)htmlDocument;
iEvent.onselectionchange += newHTMLDocumentEvents_onselectionchangeEventHandler(SelectionChangeEventHandler);
}
private void SelectionChangeEventHandler()
{
StyleMyButton();
}
public void StyleMyButton()
{
string textToAdd = null;
IHTMLDocument2 htmlDocument = GetSelectedWebBrowser().Document as IHTMLDocument2;
if (htmlDocument != null && GetSelectedWebBrowser().Visibility != Visibility.Collapsed)
{
IHTMLSelectionObject currentSelection = htmlDocument.selection;
if (currentSelection.type == "Text")
{
selectedText = ((IHTMLTxtRange)currentSelection.createRange()).text;
}
}
if (selectedText!= null)
{
//Enable the Button
MyButton.Opacity = 1;
MyButton.IsEnabled = true;
}
else
{// Disable the Button
MyButton.Opacity = 0.15;
MyButton.IsEnabled = false;
}
}
}
Chetan Rajakumar