Reputation: 131
Here is my web browser:
<WebBrowser viewmodel:BrowserBehavior.Html="{Binding SelectedNode.ContentData.FileName, Converter={StaticResource converter}, Mode=OneWay}" />
In fact, text in my WebBrowser Control can be Selected.
I wonder if I can retrieve the Selected portion in a string object?
PS: When right-clicking the Selected portion , I'd noticed that user can copy selected text. So my second question will be "Can we get the copied text?" I mean the copied portion should be saved somewhere in some environment variable, can we get it in c#?
Upvotes: 1
Views: 2321
Reputation: 33364
You cannot bind selected text from WebBrowser
but you can get it manually like so:
var doc = webBrowser.Document as mshtml.HTMLDocument;
if (doc != null)
{
var currentSelection = doc.selection;
if (currentSelection != null)
{
var selectionRange = currentSelection.createRange();
if (selectionRange != null)
{
var selectionText = selectionRange.Text;
//do something with selected text
}
}
}
where webBroswer
is your browser control
<WebBrowser x:Name="webBroswer" ... />
but you'll need to add reference to Microsoft.mshtml
and to answer your second question you can get copied text from clipboard with Clipboard
class
var copiedText = Clipboard.GetText();
Upvotes: 2