vb.net - Multicolor RichTextBox - vb.net

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:

Related

how to keep the scrollbar at the bottom visual basic

In my a form, I have a rich text box with vertical scrollbars. I have set it so that a line is read from a local text file, and added to the existing text in the text box. The following code makes it so that a line is read from the text file, there is 3 seconds of pause, and then another line is read and added to the existing text. This is done the emulate the feeling of a messenger application, so the 3 seconds of pause is absolutely necessary.
However, I have found that whenever a new line is added and there is enough text in the rich text box for the scrollbar to appear, the scrollbar will jump to the top. Every new line is added to the bottom, so this is extremely annoying, because it means the user is sent all the way up to the top of the text every single time there is a new line added.
Is there anything I can do to prevent this? i.e: locking the scrollbar to the bottom? Or having the scrollbar automatically scroll down to the bottom whenever there is a new line added?
If you need any pictures or further code, please let me know.
Dim i As Integer = 0
Call Pause(3)
RichTextBox1.Text = R.ReadLine()
Do Until i = lineCount
Call Pause(3)
RichTextBox1.Text = RichTextBox1.Text + vbCrLf + vbCrLf + R.ReadLine()
i = i + 1
Loop
The pause subroutine:
Public Sub Pause(ByVal seconds As Single)
Dim newDate As Date
newDate = DateAndTime.Now.AddSeconds(seconds)
While DateAndTime.Now.Second <> newDate.Second
Application.DoEvents()
End While
End Sub
After the TextChangedEvent you can try to set the caret to the last postiton and then use ScrollToCaret

Underline property of textbox not working like the others

I simply want to underline some text in a worksheet textbox using VBA, from a specific character to another. It should be extremely simple, and I can do it without problem with Bold and Italic.
I have the following sub
Sub ew()
Dim txt1 As Shape
Set txt1 = Sheet1.Shapes("txt_1")
txt1.TextFrame.Characters.Text = "Bold and Underline this"
txt1.TextFrame.Characters.Font.Bold = True
txt1.TextFrame.Characters.Font.Italic = True
txt1.TextFrame.Characters.Font.Underline = True
End Sub
The code fails on the last line, which is extremely strange because it worked for the 2 previous lines. the error (1004) says something like "Impossible to define the Underline function of the Font property".
To recreate the problem, take my sub to a new Excel document and create a textbox named "txt_1", that's all you need to run it.
If anyone has any idea why it fails, please help!
You need to define the Underline style. Taking your last line
txt1.TextFrame.Characters.Font.Underline = xlUnderlineStyleSingle
Use TextFrame2 for underline
txt1.TextFrame2.TextRange.Font.UnderlineStyle = msoUnderlineSingleLine

Table layout panel inc#

I have to create dynamic table layout panel with some controls with auto sized rows and and fixed columns size.
My problem is that i want to show whole checkbox text .
Any help
My code is
Dim textBox2 As New CheckBox()
textBox2.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
textBox2.AutoSize = True
textBox2.Dock = DockStyle.Top
'' textBox2.Size = New Point(200, 90)
Dim lbl1 As New Label()
lbl1.Location = New Point(10, 10)
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Location = New Point(120, 50)
lbl1.Dock = DockStyle.Top
'' dynamicTableLayoutPanel.Padding = New Padding(2, 17, 4, 5)
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(textBox2, 1, 0)
Me.dynamicTableLayoutPanel.SetColumnSpan(textBox2, 5)
If you mean you want the table to size to the controls within it, then:
dynamicTableLayoutPanel.AutoSize = True
I know this is old, but I stumbled across it and figured I'd throw my 2 cents in in case someone else comes along.
Note: I'm using Visual Studio 2015 with .NET 4.6. Functionality may differ between versions.
The problem is that the really long text is not word-wrapping to fit within the table or form. Instead, it is set to Dock = DockStyle.Top. This will cause it to make a single line that continues on and gets clipped, similar to a single-line textbox.
If you want it to automatically word wrap, you'll need to use Dock = DockStyle.Fill. Now, this doesn't completely resolve the problem if your row or table isn't large enough to display the text. Since all of the rows are set to AutoSize, it will only do the bare minimum to fit the control vertically. It doesn't care if text gets clipped off. The end result, using your example code against a 6-column, 10-row table, is this:
Since there isn't a word wrap property, you'll need to manually fit it. Now, to do this, you'll need to change the row to be Absolute instead of AutoSize. To figure out how big to make it, you can pretty much rely on PreferredSize. This reveals a much wider Width than the existing regular Width. From that, we can determine how many lines it would take if we wrap it.
This is what my code ended up looking like:
Dim h As Single = 0
Dim chk As New CheckBox()
chk.Text = "You forgot to add the ColumnStyles. Do this on a sample form first with the designer. Click the Show All Files icon in the Solution Explorer window. Open the node next to the form and double-click the Designer.vb file. "
chk.AutoSize = True
chk.Dock = DockStyle.Fill
Dim lbl1 As New Label()
lbl1.Text = "Yoer.vb"
lbl1.AutoSize = True
lbl1.Dock = DockStyle.Top
dynamicTableLayoutPanel.Controls.Add(lbl1, 0, 0)
dynamicTableLayoutPanel.Controls.Add(chk, 1, 0)
dynamicTableLayoutPanel.SetColumnSpan(chk, 5)
' Find the preferred width, divide by actual, and round up.
' This will be how many lines it should take.
h = Math.Ceiling(chk.PreferredSize.Width / chk.Width)
' Multiply the number of lines by the current height.
h = (h * chk.PreferredSize.Height)
' Absolute size the parent row to match this new height.
dynamicTableLayoutPanel.RowStyles.Item(0) = New RowStyle(SizeType.Absolute, h)
The changes included delaring a height variable, renaming the CheckBox variable, setting its Dock to Fill, removing the Location from lbl1, and adding in size calculation. The output:
This isn't perfect since the height includes the checkbox itself, and the checkbox takes up padding, so there can be too much or too little height calculated. There are other calculations that may need to be considered. But, this is a starting point.

Get RichTextBox Format and use it for a replace function

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

VB.NET - RichTextBox - Apply formatting to selected text

I have a RichTextBox control on my form. I also have this button, labeled Bold, that I want, if someone selects text in the RichTextBox, then presses the button, the selected text turns bold. Any way to do that? Simple, everyday task for end users. Thanks.
A variation on the above that takes into consideration switching bold on/off depending on the currently selected text's font info:
With Me.rtbDoc
If .SelectionFont IsNot Nothing Then
Dim currentFont As System.Drawing.Font = .SelectionFont
Dim newFontStyle As System.Drawing.FontStyle
If .SelectionFont.Bold = True Then
newFontStyle = currentFont.Style - Drawing.FontStyle.Bold
Else
newFontStyle = currentFont.Style + Drawing.FontStyle.Bold
End If
.SelectionFont = New Drawing.Font(currentFont.FontFamily, currentFont.Size, newFontStyle)
End If
End With
It may need cleaned up a bit, I pulled this from an older project.
You'll want to use the .SelectionFont property of the RichTextBox and assign it a Font object with the desired styles.
Example - this code would be in the event handler for the button:
Dim bfont As New Font(RichTextBoxFoo.Font, FontStyle.Bold)
RichTextBoxFoo.SelectionFont = bfont