In WebBrowser control I have the text like:
webbrowser.DocumentText = "Fifth You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.";
When Mouse Hover event I want to get the specific word where the mouse position and the character position of that word.
Loading

VulpesRank #55 · 29.2K reputationPosted Aug 21, 2011, 6:52 AM EDT
", "\n"); // replace
tags with new line characters
VulpesRank #55 · 29.2K reputationPosted Aug 8, 2011, 11:21 AM EDT
// add this using directive
// add through designer
Nageswararao Dhulipalla74 reputationPosted Aug 8, 2011, 9:53 PM EDT
Thank you for giving replay, and it will use if there is single sentance. Sorry for my miss code in my previous post.
Here is the actual code which I am using in my project:-
webbrowser.DocumentText = "Fouth You can easily change the formatting of selected text in the document text by choosing a look for the selected text from the Quick Styles gallery on the Home tab.
Fifth You can also format text directly by using the other controls on the Home tab. Most controls offer a choice of using the look from the current theme or using a format that you specify directly.
Sixth To change the overall look of your document, choose new Theme elements on the Page Layout tab. To change the looks available in the Quick Style gallery, use the Change Current Quick Style Set command.".
This is for the text files and if there is any HTML type files like www.google.com etc we are binding through navigate the URI, in this case it will not work.
And also the display of the content for the Text file is also not proper.
Please check the attachment code how it will display in 2 types of binding.
VulpesRank #55 · 29.2K reputationPosted Aug 9, 2011, 6:28 AM EDT
tags:
I haven't done anything with the 'unformatted' text as I'm not sure how you want the tags to be treated.
Nageswararao Dhulipalla74 reputationPosted Aug 10, 2011, 4:48 AM EDT
Thank you very much, it was working fine for the Text files which we are binding the content like
webbrowser.DocumentText = "Content";
But another issue was we are binding data through DocumentText for Text file format and for the html file format we are binding through:
webbrowser.NavigatenewUri(
This is only one issue let, please help in this scenario also.
And in your code I have changed in tag i used because i don't want to change the style of the content.
VulpesRank #55 · 29.2K reputationPosted Aug 10, 2011, 1:09 PM EDT
However, if you don't want to change the appearance of the text you can use empty tags instead which do still work properly.
The same code should work OK when loading the document via a url rather than a string as long as it doesn't contain any tags.
Nageswararao Dhulipalla74 reputationPosted Aug 16, 2011, 9:52 AM EDT
You are correct when we use the Span tag that will not work properly, but when i use the font also the display is not proper the actual display font size and other details or not display as it is. Any way that other issue but my major issue is when we bind the data through the URI through Navigate in this case it is not work.
But as you said that will work and I tried but I am unlucky. Can you please send the sample code for this example.
Also how we will manage the Click and Double click events in Web Browser.
For Click there is direct event in Webbrowser.Document.Click but for Double click how we will manage can you please send some sample code for the same.
It's very urgent please help me out to fix these issues.
VulpesRank #55 · 29.2K reputationPosted Aug 16, 2011, 12:23 PM EDT
VulpesRank #55 · 29.2K reputationPosted Aug 17, 2011, 12:32 AM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 17, 2011, 1:18 AM EDT
Thanks for the reply. Which you suggest for Click and Double click events that I already tried but when user perform double click the click event automatically fired there I have the issue how to handle this, because I want both event and in single click I want to take the cursor position word and in double click event I want to take cursor position senatnce.
When perform the double click the single click event automatically fires and my actual functionality is disturb.
Also for the Navigate URI I have the mouse HOver problems.
Please see the attachment problem which i required all the functionality. I have the display problems. Please look and suggest the solution.
Nageswararao Dhulipalla74 reputationPosted Aug 17, 2011, 1:57 AM EDT
Also I have another Issue, according to my project we need to highlight the speaking word dynamically for that i have make changes as below:
StringBuilder
htmlHighlight = new StringBuilder(element.InnerText);String substitution = "" + selectedContent + "";try{
htmlHighlight.Replace(selectedContent, substitution, characterPosition, selectedContent.Length);
}
element.InnerHtml = htmlHighlight.ToString();
catch { }//element.InnerText = htmlHighlight.ToString();wbDocument.Update();
After these changes the font tags will be remove for the current playing then this gives an issue.
VulpesRank #55 · 29.2K reputationPosted Aug 17, 2011, 10:14 AM EDT
I've also changed things around so that loading the document from a uri now works properly in all circumstances.
There was a bug before in this part of the text:
"sample paragraph 2
Foutheen You can also format"
Because there's no full stop after '2', the regular expression was giving '2Foutheen' as a single word when the tags were removed. To deal with this I've replaced all the
tags with '\n' which counts as a character and the regular expression now correctly gives two words '2' and 'Foutheen'.
As far as the DoubleClick event is concerned, there's no way to suppress the Click event from firing first because this will fire as soon as the mouse goes down, so you'll just have to find a way to work around that.
If a single click is highlighting a word but a double click is highlighting a whole sentence including that word, then I can't really see why there's any conflict.
Anyway here's the revised code - I haven't done anything for the click events as you seem to have something worked out already:
public partial class frmWebBrowser : Form
{
#region Private Variables
private MatchCollection mc;
private List
private bool bindThroughUri;
Point mouseHoverClientPosition = new Point();
Point clickClientPosition = new Point();
#endregion
#region .ctor
public frmWebBrowser()
{
InitializeComponent();
}
#endregion
#region Handle Form Events
private void frmWebBrowser_Load(object sender, EventArgs e)
{
cmbHtmlFiles.Items.Add("-- Select --");
cmbHtmlFiles.Items.Add("fire_safety.html");
cmbHtmlFiles.Items.Add("My cat called Lucky.html");
cmbHtmlFiles.Items.Add("Alice in Wonderland.html");
cmbHtmlFiles.SelectedIndex = 0;
}
#endregion
#region Webbrowser Events
private void wbDocument_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (bindThroughUri)
{
bindThroughUri = false;
if (chkFormat.Checked)
{
wbDocument.DocumentText = FormatText(wbDocument.DocumentText);
return; // DocumentCompleted eventhandler will be re-entered
}
}
// get all elements with no 'id' attribute
wordElements = new List
HtmlElementCollection spanElements = wbDocument.Document.GetElementsByTagName("SPAN");
foreach (HtmlElement spanElement in spanElements)
{
if (spanElement.Id == null && spanElement.Style == null)
{
wordElements.Add(spanElement);
}
}
// We need to change the font according to the User Selection thus why this line.
wbDocument.Document.Body.Style = "font-size:18px;";
wbDocument.Document.MouseOver += new HtmlElementEventHandler(Document_MouseOver);
wbDocument.Document.Click += new HtmlElementEventHandler(Document_Click);
wbDocument.Document.Body.AttachEventHandler("ondblclick", Document_DoubleClick);
}
private void Document_DoubleClick(object sender, EventArgs e)
{
}
void Document_MouseOver(object sender, HtmlElementEventArgs e)
{
mouseHoverClientPosition = e.ClientMousePosition;
RetrieveWord();
}
void Document_Click(object sender, HtmlElementEventArgs e)
{
clickClientPosition = e.ClientMousePosition;
}
#endregion
#region Hanlde Click Events
private void btnLoadText_Click(object sender, EventArgs e)
{
string text = "..."; // abbreviated for posting
text = FormatText(text);
bindThroughUri = false;
wbDocument.DocumentText = text;
}
private void cmbHtmlFiles_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbHtmlFiles.SelectedIndex > 0)
{
BindThroughURI();
}
}
private void chkFormat_CheckedChanged(object sender, EventArgs e)
{
if (cmbHtmlFiles.SelectedIndex > 0) // make sure a file is selected
{
BindThroughURI();
}
}
private void btnUnFormat_Click(object sender, EventArgs e)
{
string text = "..."; // abbreviated for posting
bindThroughUri = false;
wbDocument.DocumentText = text;
}
#endregion
#region Helper Methods
private string FormatText(string text)
{
string text2 = Regex.Replace(text, @"
", "\n"); // replace
tags with new line characters
text2 = Regex.Replace(text2, "<.*?>", ""); // remove all remaining tags
string pattern = @"(?=/])"; // match words which are not in tags
string replacement = "$0";
mc = Regex.Matches(text2, @"\b\w+\b"); // match all words in text2
text = Regex.Replace(text, pattern, replacement); // surround words in text with tags
return text;
}
private void RetrieveWord()
{
HtmlElement element = wbDocument.Document.GetElementFromPoint(mouseHoverClientPosition);
string word = element.InnerText;
if (!word.Contains(" ") && element.TagName == "SPAN" && element.Id == null && element.Style == null)
{
int index = -1;
for (int i = 0; i < wordElements.Count; i++)
{
if (wordElements[i].Equals(element))
{
index = i;
break;
}
}
txtMouseHoverWord.Text = String.Format("{0} : {1}", mc[index].Index, word);
}
else
{
txtMouseHoverWord.Text = ""; // show nothing otherwise
}
}
private void BindThroughURI()
{
string folderPath = Path.Combine(Application.StartupPath, "Files");
string filePath = Path.Combine(folderPath, cmbHtmlFiles.SelectedItem.ToString());
bindThroughUri = true;
wbDocument.Navigate(new Uri(filePath));
}
#endregion
}
Nageswararao Dhulipalla74 reputationPosted Aug 18, 2011, 5:21 AM EDT
Thank you very much for your help and your quick reply.
Unfortunatly I am not able to find 100% solution regards this. Thus why what I required I have make sample application for your understand what i final required and uploading the code along with some screen shots which I am facing issues.
Please don't mind please look once and give proper solution.
VulpesRank #55 · 29.2K reputationPosted Aug 18, 2011, 2:03 PM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 19, 2011, 4:35 AM EDT
Thank you very much it is working perfect.
But only one small request the URI navigate is not working properly at my end after format the display content. Can you please recheck it once. I have upload images the difference and what we need along with the update code. Please look and solve this issue also.
VulpesRank #55 · 29.2K reputationPosted Aug 19, 2011, 5:52 AM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 19, 2011, 7:11 AM EDT
But for me the firesafty also gives problem please check the attacted images the probelm.
Nageswararao Dhulipalla74 reputationPosted Aug 20, 2011, 5:50 AM EDT
Did you get the issue which I am facing. Waiting for your reply, please send solution for this.
Sam HobbsRank #107 · 20.5K reputationPosted Aug 21, 2011, 6:28 PM EDT
In HTML, there are elements in elements and at the lowest level there are text elements. I suspect that it is the contents of the text elements, and only text elements that are needed. If so then making that clear could significantly limit the work necessary to accomplish it.
I don't parse HTML using regular expressions. If it were me, I would sure look for a more precise solution.
VulpesRank #55 · 29.2K reputationPosted Aug 22, 2011, 2:15 AM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 22, 2011, 4:24 AM EDT
I have posted lot question to fullfil my requirement. But as you said I have tested a lot html files, every thing works fine but only one issue with one type of html file. Here I am attaching the html file for this the click and double click events are not working properly except every thing is working fine as my project requires.
Nageswararao Dhulipalla74 reputationPosted Aug 22, 2011, 4:36 AM EDT
wbDocument.AllowNavigation =
false; in the WebBrowser_DocumentCompleted eventVulpesRank #55 · 29.2K reputationPosted Aug 23, 2011, 11:01 AM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 23, 2011, 6:33 PM EDT
Sorry for that, Please find the attachment for the same.
VulpesRank #55 · 29.2K reputationPosted Aug 24, 2011, 10:18 AM EDT
Nageswararao Dhulipalla74 reputationPosted Aug 24, 2011, 10:48 AM EDT
And for the attachment html files one is s20210fulltext.html and for this the performance issue come because of below steps in DocumentCompleted Event
wordElements = new List();
HtmlElementCollection spanElements = wbDocument.Document.Body.GetElementsByTagName("SPAN");
foreach (HtmlElement spanElement in spanElements)
{
if (spanElement.InnerText != null && spanElement.Children.Count == 0 && regWord.IsMatch(spanElement.InnerText))
{
wordElements.Add(spanElement);
}
}
VulpesRank #55 · 29.2K reputationPosted Aug 24, 2011, 3:01 PM EDT
", "\n"); // replace
tags with new line characters
Nageswararao Dhulipalla74 reputationPosted Aug 25, 2011, 5:21 AM EDT
The problem is not with the MouseHOver event, the problem is with the Click and DoubleClick events of the Web browser document.
The problem come for that files is when we set the
wbDocument.AllowNavigation =
false;VulpesRank #55 · 29.2K reputationPosted Aug 25, 2011, 3:04 PM EDT
Sam HobbsRank #107 · 20.5K reputationPosted Aug 25, 2011, 7:57 PM EDT
Something else I will be very surprised if it is true is that you are not frustrated. I expect significant frustration with something such as this. The DOM and related software is cryptic and kludgy. The documentation is poor and many answers seem to be intentionally kept secret. There are a few things that I have tried doing that are supposed to work but no one can tell me why it does not. As far as Microsoft is concerned, what I have tried is supposed to work but it does not. There is one or two I should post here in a C# Corner forum to get at least confirmation that I am doing it as documented.
VulpesRank #55 · 29.2K reputationPosted Aug 26, 2011, 7:25 AM EDT
However, I'd certainly agree with Sam that the WebBrowser control is frustrating to work with and I'm surprised that this exercise is working as well as it does for most documents.
Nageswararao Dhulipalla74 reputationPosted Sep 9, 2011, 5:25 AM EDT
Thanks for the reply's. I was out of station to visit my native place thsu why I can't able to give reply.
Anyway we required this tag html files to manage and the only issue is click and double click events.
Please suggest the solution for this issue.