1

I am using a 2 web browsers to compare texts, and when the lines become to long it wraps my text, i would like to remove this or lower my font size to avoid this.

Can some one please advise me on how to remove the word warp or change font size on web Browsers?

The word wrap below totally miss alines my compare:

enter image description here

Pomster
  • 14,567
  • 55
  • 128
  • 204
  • Assuming you'd like to link both "lists" anyway, have you thought about using a `ListView` with custom element drawing? That way you could use one row per line/block and draw them the way you'd like, without having a scroll bar inbetween. – Mario Jun 26 '12 at 08:50

3 Answers3

1

As far as I know, there is no property of the web browser control that can do this. The presentation of the web page is controlled entirely by the web page itself.

If you cannot change the web page itself, the best you can do is to possibly cache a local version and change its styling using WebBrowser.Document.ExecCommand(). WebBrowser.Document.InvokeScript() also works but requires already-defined JS, so we have to go ahead and add in the script manually.

HtmlElement head = webBrowser.Document.GetElementsByTagName("head")[0];
HtmlElement script = webBrowser.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)script.DomElement;
element.text = "function adjust { document.getElementById('yourIdHere').style.whiteSpace='nowrap'; }";
head.AppendChild(script);
webBrowser.Document.InvokeScript("adjust");

Just find the div id of the text by looking at the web page's source code and you should be golden. If it doesn't have a div id, you can use other JS methods (such as getElementsByTagName) to find it.

Chris Dworetzky
  • 940
  • 4
  • 9
  • No problem. I just had to re-edit my post because I accidentally put the wrong method name in. – Chris Dworetzky Jun 26 '12 at 08:00
  • So some thing like WebBrowser.Document.ExecCommand(white-space: nowrap) ?? How would i invokeScript or ExecCommand? – Pomster Jun 26 '12 at 08:04
  • This question has a good working example of using InvokeScript: http://stackoverflow.com/questions/153748/how-to-inject-javascript-in-webbrowser-control. I'll edit my answer to put some code in. – Chris Dworetzky Jun 26 '12 at 08:06
  • 1
    I would prefer not having a JS file and just ExecCommand a little line of text if that is possible ? but will check out that link now :) – Pomster Jun 26 '12 at 08:10
  • I edited my answer again. Check it out to see if it works for you. I'm sure there is also a way to do this with ExecCommand but I'm not too well-versed with it. – Chris Dworetzky Jun 26 '12 at 08:16
  • @Pommy It uses System.Windows.Forms. But I think it may be deprecated. I haven't used it in a long time and the documentation for it isn't the greatest. – Chris Dworetzky Jun 26 '12 at 08:19
  • How would i do this?? i'm trying webbrowser.Document.ExecCommand("white-space:nowrap;", false, false); What do each of these parameters mean? – Pomster Jun 26 '12 at 08:28
  • @Pommy That's isn't how Document.ExecCommand works. You can only use the commands here: http://msdn.microsoft.com/en-us/library/ms533049(VS.85).aspx – Chris Dworetzky Jun 26 '12 at 08:31
  • I am Having problems with IHTMLScriptElement missing useing or assemblt referace. – Pomster Jun 26 '12 at 10:35
1

You can not adjust font size or wrapper text from the web control its self. So you should adjust the styling of the elements inside the web browser.

I adjusted the styling at the same place i styled the green and red highlighting.

    html.Append("<ins style=\"white-space:nowrap; display:inline; background:#e6ffe6;\">")
        .Append(text)
        .Append("</ins>");

"white-space:nowrap; display:inline;

The above code is what i added to remove word wrapping.

Pomster
  • 14,567
  • 55
  • 128
  • 204
1

Inspired by Pomster's Answer, that's what I did to get the best result:

String.Format("<div style='white-space:nowrap; display:inline;'>{0}</div>",text);

c# 6.0 version:

$"<div style='white-space:nowrap; display:inline;'>{text}</div>";
ericmas001
  • 603
  • 6
  • 12