Get RichTextBox Format and use it for a replace function - vb.net

I just started to learn VB and try to make a little WYSIWYG-HTML Editor. For that i already made a RichTextBox in which the user is able to change colour, fontsize etc.. Now I want to add for example a <b> -Tag before and a </b> -Tag after a word which is written in bold style, save it in a string and give back the new string in a second read-only-Textbox.
What's the best way to do this?

So you want to set read-only textbox's text to the richtextbox's text and then replace certain string with other strings?
If so, here's some code I wrote up. It's probably not the best but it works.
TextBox1.Text = RichTextBox1.Text 'Copies the text form the richtextbox to the normal textbox
If TextBox1.Text.Contains("<b>") Then 'Checks to see if Textbox1 contains the string "<b>"
TextBox1.Text = TextBox1.Text.Replace("<b>", "[b]") 'Replaces <b> with [b]
End If
If RichTextBox1.Text.Contains("</b>") Then 'Same thing as above but this checks to see if it contains "</b>"
TextBox1.Text = TextBox1.Text.Replace("</b>", "[/b]")
End If

Related

How to search only the first line of a multiline textbox in VB.NET

Is there any way to search only the first line of a Multiline Textbox without knowing exactly at what position the text is you're looking for?
If I knew the position of the text I was looking for I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Substring(0,4) = myText Then
' Do Something
End If
Or if I wanted to search the entire textbox I could do something like:
Dim myNotes As String = "The book has a lot of text"
Dim myText As String = "text"
If Not myNotes.Contains(myText) Then
' Do Something
End If
But I want to search only the first line of the textbox and I'm not sure at what position the text may be. Is there anyway to do a search like that?
This is another example of why you should ALWAYS read the relevant documentation. If you had read the documentation for the TextBox class then you'd know that it has a Lines property. To get the first line of text, you simply get the first element of that array:
Dim firstLine = myTextBox.Lines(0)
If Not filrstLine.Contains(myText) Then
'Do something
End If
Note that this only applies where the user has explicitly added a line break to the text. I assume that that is what you want, given that you have accepted another answer that does the same thing. If you mean the first line based on automatic word-wrap then that requires a bit more effort.
You could take the text and extract the first line.
int pos = text.IndexOfAny('\r', '\n');
if (pos >= 0)
text = text.SubString(0, pos);
// text now contains only the first line
Then you can search the resulting string.

How to make paragraphed text appear in a multi-line textbox when a treenode is selected

I reckon this is probably a simple thing to do but I can't seem to know how to pull it off as I am new to VB.Net.
On my form, I have a treeview control I have populated with nodes using the Nodes Collection TreeNode Editor. I also have a multi-line textbox. When a node is selected, some pre-defined text assigned to that particular node appears in my textbox.
What I want to achieve is for paragraphs of text with formatting, to be inputted into the textbox and not just single lines of text as shown in my code below.
For instance, when the user clicks a node titled 'Soccer', I would like a formatted body of text like:
“This is a sport.
It is a sport played between two teams of eleven players with a spherical ball.
It is played by 250 million players in over 200 countries.”
to appear in my textbox. Please how can I get the code to do this? I am using Visual Basic 2010 Express. The code I am using at the moment is shown below. Thank you in advance.
Private Sub TreeView1_AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim SelectedNode As TreeNode
SelectedNode = TreeView1.SelectedNode
If SelectedNode.Text = "Soccer" Then
TextBox1.Text = "This is a sport." 'I would like to have paragraphed text appear in 'textbox1 instead of a single line of text
Else
If SelectedNode.Text = "Moon" Then
TextBox1.Text = "This is the name of a car." 'I would like to have paragraphed text 'appear in textbox1 instead of a single 'line of text
End If
End If
End Sub
You should add '\n' in end of your line.

Text Is Selected After File Read Into A Textbox

So after I run the following code, all my text is highlighted in the textbox.
Dim ioFile As New System.IO.StreamReader("FilePath")
TextBox1.Text = ioFile.ReadToEnd()
Is there a way to not select all the text? BTW, its a multiline textbox.
Use the Select Method to remove the selection.
'Cursor at beginning of text.
TextBox1.Select(0, 0)
'Cursor at end of text.
TextBox1.Select(TextBox1.Text.Length, 0)

vb.net - Multicolor RichTextBox

I would like to make a line of text in my richtextbox multicolor. I have tried various implementations provided on the web and read up on SelectedText and other topics but can't seem to get it to work the way I would like to.
Here is what I have so far
RichTextBox1.Text = "This is black "
RichTextBox1.SelectionFont = New Font("Microsoft Sans Serif", 8.25, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.SelectedText = "[BOLD GREEN]"
RichTextBox1.Text = RichTextBox1.Text + " black again"
The colors I want are stated as the text. What happens is: the entire line turns green, "[BOLD GREEN]" appears at the beginning of the textbox instead of inline.
I want it to read like this: "this is black" as black. "[BOLD GREEN]" as green and "black again" as black.
It's not really clear what you're trying to achieve. I'm not sure I understand the bracketed formatting nearly as well as I would an image that you mocked up in Paint. But here goes anyway...
I suspect there are a couple of problems with your existing code. First up is the location of the cursor when you insert new text. What's supposed to come after the first snippet actually gets inserted before it because of where the insertion mark is located. To fix that, you need to move it manually.
You're also assigning a string of text to the Text property at the end of your code, which does not preserve the existing formatting information. I suspect that the simplest thing for you to do is to use the AppendText method, instead.
And finally, I recommend using the simpler overload to create a new font, since the only thing you want to change is the style. The advantage of using this instead is that you don't have to hardcode the name and size of the font in your code, in case you want to change it later.
Try rewriting your code to this instead:
' Insert first snippet of text, with default formatting
RichTextBox1.Text = "This is black "
' Move the insertion point to the end of the line
RichTextBox1.Select(RichTextBox1.TextLength, 0)
'Set the formatting and insert the second snippet of text
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Green
RichTextBox1.AppendText("[BOLD GREEN]")
' Revert the formatting back to the defaults, and add the third snippet of text
RichTextBox1.SelectionFont = RichTextBox1.Font
RichTextBox1.SelectionColor = RichTextBox1.ForeColor
RichTextBox1.AppendText(" black again")
The result will look like this:

Quick fix, Read text in a text box?

I have a simple text reading code for Visual Basic:
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(fileReader)
I have used this in the past, but I usually make the text display in a text box. I know this is sort of a "newb" question but I can't remember how to display the text in a textbox. If you guys could help me out that would be great!
You'd just do:
textBox1.Text = fileReader
This puts the contents of the string "fileReader" into the TextBox's Text, provided you're doing this from within your Form, and you have a text box on the form named "textBox1".
Also, make sure that your text box is set to be a multi line textbox, if your file has more than a single line within it.
myTextBox.text = fileReader