Displaying HTML content inside of a textbox - vb.net

I'm using the following code, to get my raw HTML string from the URL and display it inside of a (rich) textbox form:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TransparencyKey = Me.BackColor
Dim client As WebClient = New WebClient()
RichTextBox1.Text = client.DownloadString("http://myurl.com/raw.php")
End Sub
The problem is that, the HTML string isn't converted and I can see the HTML tags inside of a textbox text I mean the Hello <strong>World</strong> isnt converted into the "Hello World".
I know I can use a WebBrowser object, but I would like to set its background color from the white to transparent and that is not allowed as far as I know. Another reason I dont want to use WebBrowser are links, because when the downloaded string has some ... tags, it would be converted but when I would like it to be opened with a default browser, instead of a typical location change in a box.
Is there any solution for this?

A TextBox cannot render HTML. What you need is a modified RichTextBox control that handles HTML. This might help you get started atleast.
Then there's the HtmlTextbox for Windows.Forms control which might suite your needs.

Related

How can I save the content of a rich text box even when the form closes?

This is my first post here, so please don't judge me if I write something wrong ^^
Anyways, I've recently run into an issue with richtextboxes in Visual Basic .NET and WinForms.
Let's say I have a Main form and a Log form. The log form contains a richtextbox which functions as a log. From the main form I'm writing text to the log and I also format the lines, so they have different colors (blue for information, red for error).
Unfortunately, whenever I close and reopen the log form all text that has been written to it is lost.
I've tried saving it to a text file, but that doesn't save the colors of the text.
Is there any way I can save the text and the colors of the lines even when the form closes?
Here's what the excellent suggestion from Shrotter might look like:
Public Class Form1
Private RtfPath As String
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folder As String = System.IO.Path.GetDirectoryName(Application.ExecutablePath)
RtfPath = System.IO.Path.Combine(folder, "RtbData.rtf")
If System.IO.File.Exists(RtfPath) Then
RichTextBox1.LoadFile(RtfPath)
End If
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
RichTextBox1.SaveFile(RtfPath)
End Sub
End Class
Of course, you should always wrap the loading/saving of the file in a Try/Catch block in case anything goes wrong.

How do I set the Richtextbox.rtf to a rich text string?

I am trying to add a header to the rich text in a RichTextBox, using VB.NET and Visual Studio 2017. According to the documentation, Richtextbox.rtf should allow me to get or set the rich text including control codes. However, I am unable to set *.rtf to a string containing rich text. I know that the rich text is correct because if I paste it into a *.rtf file, it is displayed correctly.
The test code looks like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim hdr As String = "{\header This is a header}"
Dim s As String = RichTextBox1.Rtf
s = s.Insert(s.LastIndexOf("}"c) - 1, hdr)
MsgBox(s)
With RichTextBox1
RichTextBox1.Rtf = s
MsgBox(RichTextBox1.Rtf)
End With
End Sub
The string s is correctly formatted as rich text, but RichTextBox1.Rtf is unchanged after the assignment. What am I missing? If I can't assign RichTextBox1.Rtf this way, is there an alternative?
Thanks again #PerpetualStudent!
The problem appears to be that the RichTextBox1.RTF field does not accept the "{\header This is a header}" control code. That is probably by design because a RichTextBox cannot display a header. I tried putting the control code in a different location in the rich text string, but that didn't work either.
I can edit the rich text in other ways (see below), but I cannot insert the header control code. That's unfortunate because it is part of the rich text standard. Anyhow, now that I know what the problem is, I can come up with a solution. A workaround might be to modify the print and save code of my rich text box print control form to add the header and footer in the print or save actions.
This works:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
With RichTextBox1
Dim s As String = .Rtf
s = s.Replace("Hello", "Good morning")
MsgBox(s)
.Rtf = s
MsgBox(.Rtf)
End With
End Sub

Manipulate text in a loaded html-page of the Webbrowser control in the DocumentCompleted event (vb.net)

I already tried some solutions provided here, but I cannot get it to work. On my winform I have a webbrowser control which should load a webpage (aspx). In case the webpage isn't found, I want to let the user know that this page isn't found. To get this to work I use the following code:
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
If (WebBrowser1.Document.Url.ToString().StartsWith("res:")) Then
Dim curDir As String = Directory.GetCurrentDirectory()
Dim Url As Uri = New Uri(String.Format("file:///{0}/Html/PageNotFound.html", curDir))
WebBrowser1.Navigate(Url)
End If
End Sub
This is working fine. The page PageNotFound.html is shown. However, I would like to provide the user with some additional information which I want to insert into the PageNotFound.html at realtime (i.e. using document.getElementById to manipulate a Label-tag). I just don't know how I can do this, or if it is even possible. Maybe I use the wrong event. something I also tried is:
With WebBrowser1
.Navigate("about:blank")
.Document.OpenNew(False)
.Document.Write(HtmlString)
.Refresh()
End With
Where the HtmlString contains a complete webform.(like: "")
Maybe someone put me the right direction? TIA

How to get HTML from the URL and display it in certain place?

I'm in progress with my first application in visual basic, and I'm using the visual basic studio - My question is, how can I get raw HTML data from specified URL, and then display it in my form on the certain position?
I havent tried anything yet, because I havent found much solutions about this on the internet.
You need to use the HttpClient class or the WebClient class to get the raw HTML as a string. Then, you can simply display the string in any control on your form such as a TextBox or Label control.
For instance:
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim client As WebClient = New WebClient()
Label1.Text = client.DownloadString("http://www.stackoverflow.com")
End Sub

Sending Keys to webbrowser vb.net?

I have a webbrowser in my vb.net application and I would like to enter text into a textbox on a site. When button 1 is clicked, it programmatically finds the text box and types the message in.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SendKeys.Send("{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}{TAB}")
SendKeys.Send("The text I want to send to the control.")
End Sub
End Class
However, it doesn't work. The tab keys do get the curser in the right place but when the text is pasted in the application crashes. Whats gone wrong?
To send keys to WebBrowser, get WebBrowser focus and later SendKeys. Use the following code:
Me.WebBrowser1.Document.Body.Focus()
System.Windows.Forms.SendKeys.Send("...") 'Whatever keys combination you want
Since you are using a webbrowser control, you can access the element by name. For example, this will put text into Google's search box and then click the Google Search button:
WebBrowser1.Document.All("q").SetAttribute("Value", "Text value.")
WebBrowser1.Document.All("btnK").InvokeMember("click")