Richtexbox format doesn't happen occasionally

  • Thread starter Thread starter Noozer
  • Start date Start date
N

Noozer

I have the following Subs defined to place text into a richtextbox with a
specific font and colour, depending on context. Occasionally, the font just
doesn't get applied and the text appears in the default style and not the
one I've defined in the variables...

'Send incoming text to the display.
Private Sub DisplayIn(ByVal text As String)
rtbDisplay.SelectionColor = uInColor
rtbDisplay.SelectionFont = uInStyle
rtbDisplay.AppendText(text)
End Sub

'Send outgoing text to the display.
Private Sub DisplayOut(ByVal text As String)
rtbDisplay.SelectionColor = uOutColor
rtbDisplay.SelectionFont = uOutStyle
rtbDisplay.AppendText(text)
End Sub

....is there a better/easier way to apply a font style and colour to specific
text in a rich textbox?
 
Noozer,

When you set SelectionFont for the richtextbox control, it applies to the
selection (selected Text). Since in your code you are not selecting anything,
it does not work all the times. To make sure it applies your fonts to the
appended text, select the text after appending it as follows and then set the
appropriate properties:

Dim PositionbeforeAppend As Integer = Me.RichTextBox1.TextLength

Me.RichTextBox1.AppendText(vbCrLf & "This is new Text." & vbCrLf)
RichTextBox1.SelectionStart = PositionbeforeAppend
Me.RichTextBox1.SelectionLength = Me.RichTextBox1.TextLength -
PositionbeforeAppend

RichTextBox1.SelectionFont = New Font("Arial", 16, FontStyle.Bold)
RichTextBox1.SelectionColor = Color.Blue


Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Bharat Patel said:
Noozer,

When you set SelectionFont for the richtextbox control, it applies to the
selection (selected Text). Since in your code you are not selecting anything,
it does not work all the times. To make sure it applies your fonts to the
appended text, select the text after appending it as follows and then set the
appropriate properties:

I actually figured out what it was last night...

Since you can't set the maximum size of a RichTextBox, I was grabbing the
text from the box, trimming it down, and reapplying it. This would lose any
formatting that it held. It all happened so fast that I didn't catch it
right away.

Now I need a way to set a richtextbox to a specific size (4000 characters
max, for example)

Thx!
 
You can set the MaxLength property of the RichTextbox to the desired value.

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
Bharat Patel said:
You can set the MaxLength property of the RichTextbox to the desired
value.

Maxlength only affects the amount that can be pasted/entered. The textbox
itself is not restricted.

Create a RTB and set maxlength to 20... Now execute
rtb.appendtext("123456789012345678901234567890") and the textbox contains 30
characters. There is also no way to strip extra characters out of the RTB
without losing the formatting (at least that I know of).

Thanks!
 
Noozer,

That makes sense. MaxLength limitation is needed only for the User interaction.
You want your Users to restrict by that limit.
When you are doing AppendText, you are doing it programatically and you have
full control at that point so you as a programmer can check it and restrict it
in the code before adding the text via AppendText. When you use AppendText
check the length of the existing Text and new Text being added. Strip the new
Text if it is longer than the MaxLength and then call AppendText. Once you put
the Text in the RTB, you can strip the text without loosing formating as
follows:

If Me.RichTextBox1.TextLength > 15 Then
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Substring(0, 15)
End If

Doesn't this work for you?

Hope this helps!
Bharat Patel
Microsoft, Visual Basic .NET

This posting is provided "AS IS" with no warranties, and confers no rights.
Please reply to newsgroups only. Thanks.
 
That makes sense. MaxLength limitation is needed only for the User
interaction.
You want your Users to restrict by that limit.
When you are doing AppendText, you are doing it programatically and you have
full control at that point so you as a programmer can check it and restrict it
in the code before adding the text via AppendText. When you use AppendText
check the length of the existing Text and new Text being added. Strip the new
Text if it is longer than the MaxLength and then call AppendText. Once you put
the Text in the RTB, you can strip the text without loosing formating as
follows:

If Me.RichTextBox1.TextLength > 15 Then
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Substring(0, 15)
End If

Doesn't this work for you?

Appreciate the help!

I'm pretty sure I tried this, but I will take a look at what I currently
have. Probably did something stupid.

Take care!
 
Bharat Patel said:
Noozer,

That makes sense. MaxLength limitation is needed only for the User interaction.
You want your Users to restrict by that limit.
When you are doing AppendText, you are doing it programatically and you have
full control at that point so you as a programmer can check it and restrict it
in the code before adding the text via AppendText. When you use AppendText
check the length of the existing Text and new Text being added. Strip the new
Text if it is longer than the MaxLength and then call AppendText. Once you put
the Text in the RTB, you can strip the text without loosing formating as
follows:

If Me.RichTextBox1.TextLength > 15 Then
Me.RichTextBox1.Text = Me.RichTextBox1.Text.Substring(0, 15)
End If

Doesn't this work for you?

This removes ALL the formatting from the text.

What I really need is a way to tell a RTB to be only 100 lines or 5000
characters and have it drop the oldest characters as new ones are added.

Thanks
 
Noozer said:
the you

This removes ALL the formatting from the text.

What I really need is a way to tell a RTB to be only 100 lines or 5000
characters and have it drop the oldest characters as new ones are added.

What I don't understand is why the following doesn't work:

If rtbDisplay.Text.Length > uDisplaySize Then
rtbDisplay.Text.Remove(0, rtbDisplay.Text.Length - uDisplaySize)
End If
 
What I really need is a way to tell a RTB to be only 100 lines or 5000
What I don't understand is why the following doesn't work:

If rtbDisplay.Text.Length > uDisplaySize Then
rtbDisplay.Text.Remove(0, rtbDisplay.Text.Length - uDisplaySize)
End If

Nevermind... REMOVE is a function, not a method, so it returns the new value
instead of modifying the current value.

So... still the issue of keeping the contents of a RTB down to a certain
amount while retaining formatting.
 
Bharat Patel said:
Hi Noozer,

Unfortunately, removing the text using string functions does not delete the old
formating which was there at the begining of the RTB.
To work around this, you can save the new RichTextbox in a temporty buffer of
another instance of RTB. Try the code below and see how it works.

Dim uDisplaySize As Integer = 50
If Me.RichTextBox1.TextLength > uDisplaySize Then
Dim rtbTemp As New RichTextBox
RichTextBox1.Select(RichTextBox1.TextLength - uDisplaySize + 1,
uDisplaySize)
rtbTemp.Rtf = RichTextBox1.SelectedRtf
RichTextBox1.Clear()
RichTextBox1.Rtf = rtbTemp.Rtf
rtbTemp = Nothing
End If

Thanks! This seems to work, but it's definately a kludge and looks awful. It
will have to do until I can cobble something else together.

Thanks again!
 
Back
Top