473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Richtexbox format doesn't happen occasionally

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?
Nov 20 '05 #1
11 1391
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.

Nov 20 '05 #2

"Bharat Patel [MSFT]" <bh*****@online.microsoft.com> wrote in message
news:Jn**************@cpmsftngxa07.phx.gbl...
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!
Nov 20 '05 #3
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.

Nov 20 '05 #4

"Bharat Patel [MSFT]" <bh*****@online.microsoft.com> wrote in message
news:oo**************@cpmsftngxa07.phx.gbl...
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!
Nov 20 '05 #5
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.

Nov 20 '05 #6
> 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!
Nov 20 '05 #7

"Bharat Patel [MSFT]" <bh*****@online.microsoft.com> wrote in message
news:0W**************@cpmsftngxa07.phx.gbl...
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
Nov 20 '05 #8

"Noozer" <po********@127.0.0.1> wrote in message
news:7qpIb.882742$9l5.133251@pd7tw2no...

"Bharat Patel [MSFT]" <bh*****@online.microsoft.com> wrote in message
news:0W**************@cpmsftngxa07.phx.gbl...
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.


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

Nov 20 '05 #9
> > 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


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.
Nov 20 '05 #10
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

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.

Nov 20 '05 #11

"Bharat Patel [MSFT]" <bh*****@online.microsoft.com> wrote in message
news:iY**************@cpmsftngxa07.phx.gbl...
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!
Nov 20 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Claus Haslauer | last post by:
Hi, I want to create a crosstab query that looks like this Date | Elevation 1 | Elevation 2 | ... ______________________________________________________________________ Date 1 | xx.y | xx.y...
6
by: Servé Lau | last post by:
suppose I want to use sscanf get the functionname from a function prototype. Is the following format string correct then? char funcname; char *p = "func(void)"; sscanf(p, "%s", funcname);...
1
by: Carlos Lozano | last post by:
I need to merge the content of two richtextboxes. It can not be accomplished using the RichtTextBox.Text field because all format will be lost. So it has to be done using the RichtTextBox.Rtf...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
19
by: Neil Cerutti | last post by:
Where can I find documentation of what Python accepts as the filename argument to the builtin function file? As an example, I'm aware (through osmosis?) that I can use '/' as a directory...
3
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is...
10
by: DontellTrevell via AccessMonster.com | last post by:
HELP!!....I need to calculate the numer of days elapsed between two field. But, the date format is YYYYMMDD. How can i accomplsh this? -- Dontell Trevell Message posted via AccessMonster.com...
13
by: Neil Cerutti | last post by:
Many of the file formats I have to work with are so-called fixed-format records, where every line in the file is a record, and every field in a record takes up a specific amount of space. For...
1
by: Eng Teng | last post by:
How to control size of length in richtexbox per line ? Regards, Tee
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.