By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Formatting TextBox display (non-fixed width fonts)

P: n/a
I want to plant an Easter Egg in our software.

We have a TextBox that is multiline and used to display all sorts of
messages on the screen for our operators based on database queries and such.

The Easter Egg I want to create would send a dump of the data for a
particular part number to the screen when a certain secret combination of
characters is pressed. If it works out well, I can actually impliment it on
our production floor.

I can read in the data and split it up in the fields it needs to be in, but
the TextBox's font is Arial, and not very good at displaying tablular data.

If I change the font to something fixed like Courier, the change will be
very obvious.

Does anyone know of a technique that I could use to make sure each field
that I want to display in the MultiLine TextBox only takes up set amount of
space?

SubString came to mind at first, but padding a field with spaces would not
get my next field to the correct position.

Tabs seem nice, but how can I tell exactly how much space a vbTab is going
to consume? What if text runs over?
Oct 8 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
I think I found a work-around: I can use a RichTextBox instead of a standard
TextBox.

If anyone has a better solution, I'm still here.

"jp2msft" wrote:
I want to plant an Easter Egg in our software.

We have a TextBox that is multiline and used to display all sorts of
messages on the screen for our operators based on database queries and such.

The Easter Egg I want to create would send a dump of the data for a
particular part number to the screen when a certain secret combination of
characters is pressed. If it works out well, I can actually impliment it on
our production floor.

I can read in the data and split it up in the fields it needs to be in, but
the TextBox's font is Arial, and not very good at displaying tablular data.

If I change the font to something fixed like Courier, the change will be
very obvious.

Does anyone know of a technique that I could use to make sure each field
that I want to display in the MultiLine TextBox only takes up set amount of
space?

SubString came to mind at first, but padding a field with spaces would not
get my next field to the correct position.

Tabs seem nice, but how can I tell exactly how much space a vbTab is going
to consume? What if text runs over?
Oct 8 '08 #2

P: n/a
I think I found a work-around: I can use a RichTextBox instead of a standard
TextBox.
You beat me to the punch - an RTB is a good solution. You can set the RTB's
DefaultFont property to an Arial font, and you can insert your tabular data
in a monospace font (like Courier New). You will also be able to add
highlighting (eg color) and so forth as new needs arise. I suggest you make
your app work as it does now with an RTB replacing the textbox, and that
should not take too long since RTB and TextBox both inherit TextBoxBase.

Oct 8 '08 #3

P: n/a
I'm currently on a mission to find out how to include mixed fonts in a RTB,
but if you'd care to help, that would be fantastic!

Say my RTB contains several lines of existing text in Arial font, and I want
to append some new text to it in Courier New. How would that be done? Is it
similar to formatting HTML (i.e. <font name='courier new'>New Text</font>) or
....?

This could open up a new venu for my application!

Can a RTB create/display a table?

If you know of a nice online source for this, that's all I need! :)

"AMercer" wrote:
I think I found a work-around: I can use a RichTextBox instead of a standard
TextBox.

You beat me to the punch - an RTB is a good solution. You can set the RTB's
DefaultFont property to an Arial font, and you can insert your tabular data
in a monospace font (like Courier New). You will also be able to add
highlighting (eg color) and so forth as new needs arise. I suggest you make
your app work as it does now with an RTB replacing the textbox, and that
should not take too long since RTB and TextBox both inherit TextBoxBase.
Oct 8 '08 #4

P: n/a
On Oct 8, 8:08*pm, jp2msft <jp2m...@discussions.microsoft.comwrote:
I'm currently on a mission to find out how to include mixed fonts in a RTB,
but if you'd care to help, that would be fantastic!

Say my RTB contains several lines of existing text in Arial font, and I want
to append some new text to it in Courier New. How would that be done? Is it
similar to formatting HTML (i.e. <font name='courier new'>New Text</font>) or
...?
Related to this question, i can show a tiny snippet that is used to
make RichTextBox have multiple text with multiple font styles using
its Select method:

'----------------------------------------------------------------------
With RichTextBox1
.AppendText("Visual Basic")
.Select(0, 12)
.SelectionFont = New Font("Arial", 12)

' From now on, append text using
' different font style eg:Courier New
.AppendText(" is a good language")
.Select(12, 19)
.SelectionFont = New Font("Courier New", 12)
End With
'--------------------------------------------------------------------

Remeber you need to pass proper StartIndex and Length parameters of
Select method while using it.

Hope it gives some idea,

Onur Güzel
Oct 8 '08 #5

P: n/a
kimiraikkonen has it right. The general idea is to use the RTB tricks
associated with selection. Once some text written into the RTB and is also
selected, you can change many of its characteristics including font,
alignment, indent, color, etc.

Oct 8 '08 #6

P: n/a
"AMercer" <AM*****@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
kimiraikkonen has it right. The general idea is to use the RTB tricks
associated with selection. Once some text written into the RTB and is
also
selected, you can change many of its characteristics including font,
alignment, indent, color, etc.
But... does it /have/ to be that way? The original VB6 RTB supported syntax
like this (just tried it in VB6, and it works as expected)

'========
Private Sub Command1_Click()

With RichTextBox1
.Text = "" 'Clear everything for the test

'Preset the font and size.
'This effects all text added to the control /after/ this line.
'But, only if SelText is used to append the text.
.SelFontName = "Courier"
.SelFontSize = 14
'
.SelText = "This is Courier 14" & vbCrLf
'
'New font/size/color - effects all text added after this line.
.SelFontName = "Verdana"
.SelFontSize = 10
.SelBold = True
.SelColor = vbBlue
'
.SelText = "This is Verdana 10, in Bold Blue"
End With

End Sub

'========

So... while similar to the code posted, it doesn't require pre-selecting
(or, re-selecting, which ever way you want to look at it) the text to be
manipulated. When the font/color/size/etc is set at the end of the control's
buffer, it assumes all new text will take on those characteristics.

Of course, pre-selecting/re-selecting is also supported... just trying to
save a little work for someone, that's all....
--
Ken Halter
Part time groupie
Oct 8 '08 #7

P: n/a
Ken,

What is the difference with the sample from Onur, beside that it is in VB6
code, while the code from Onur is as far as I see in fact shorter?

Cor

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.comschreef in bericht
news:%2****************@TK2MSFTNGP06.phx.gbl...
"AMercer" <AM*****@discussions.microsoft.comwrote in message
news:4F**********************************@microsof t.com...
>kimiraikkonen has it right. The general idea is to use the RTB tricks
associated with selection. Once some text written into the RTB and is
also
selected, you can change many of its characteristics including font,
alignment, indent, color, etc.

But... does it /have/ to be that way? The original VB6 RTB supported
syntax like this (just tried it in VB6, and it works as expected)

'========
Private Sub Command1_Click()

With RichTextBox1
.Text = "" 'Clear everything for the test

'Preset the font and size.
'This effects all text added to the control /after/ this line.
'But, only if SelText is used to append the text.
.SelFontName = "Courier"
.SelFontSize = 14
'
.SelText = "This is Courier 14" & vbCrLf
'
'New font/size/color - effects all text added after this line.
.SelFontName = "Verdana"
.SelFontSize = 10
.SelBold = True
.SelColor = vbBlue
'
.SelText = "This is Verdana 10, in Bold Blue"
End With

End Sub

'========

So... while similar to the code posted, it doesn't require pre-selecting
(or, re-selecting, which ever way you want to look at it) the text to be
manipulated. When the font/color/size/etc is set at the end of the
control's buffer, it assumes all new text will take on those
characteristics.

Of course, pre-selecting/re-selecting is also supported... just trying to
save a little work for someone, that's all....
--
Ken Halter
Part time groupie

Oct 9 '08 #8

P: n/a
On 9 Oct, 07:36, "Cor Ligthert[MVP]" <notmyfirstn...@planet.nlwrote:
Ken,

What is the difference with the sample from Onur, beside that it is in VB6
code, while the code from Onur is as far as I see in fact shorter?

Cor
The difference is that Onur's code includes hardcoded values for the
lengths of the text (as he remarked at the end of his post). Ken's
code doesn't need hardcoded lengths so is more reusable. And BTW I
think it's similar length to Onur's if you count carefully (Ken
cleared the text to start with & also has to set individual font
properties in individual lines in VB6)
Oct 9 '08 #9

P: n/a
Hi mark,

Do I miss something, here the code I saw were I removed all comments.

Onur his code
\\\
With RichTextBox1
.AppendText("Visual Basic")
.Select(0, 12)
.SelectionFont = New Font("Arial", 12)
.AppendText(" is a good language")
.Select(12, 19)
.SelectionFont = New Font("Courier New", 12)
End With
///
Ken his code
\\\
With RichTextBox1
.Text = "" 'Clear everything for the test
.SelFontName = "Courier"
.SelFontSize = 14
.SelText = "This is Courier 14" & vbCrLf
.SelFontName = "Courier New"
.SelFontSize = 10
.SelBold = True
.SelColor = vbBlue
.SelText = "This is Verdana 10, in Bold Blue"
End With
////
The only difference I see is that Onur does it in one line and Ken in two.

Can you explain more to me what you mean?

Cor

Oct 9 '08 #10

P: n/a
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5B**********************************@microsof t.com...
The only difference I see is that Onur does it in one line and Ken in two.

Can you explain more to me what you mean?

Cor
Despite the differences in the languages, my comments were based on the fact
his code either needs to calculate or hard-code the selection points.... as
in...
.Select(0, 12)
..
..
..
.Select(12, 19)
The font/color settings were just an attempt to show that, if you set them
*before* adding new text, there's no reason to know the selection points at
least that's the case with the VB6 control, which is an ActiveX wrapper for
riched32.dll. The 'one line versus two' wasn't the point of my comments.

I'm just wondering if the .Net control supports this functionality as
well.... I assume it does, but have been surprised before.

...but, we've already used more keystrokes talking about it than it would've
saved in a year, so.... <g>

--
Ken Halter
Part time groupie
Oct 9 '08 #11

P: n/a
On Oct 9, 8:13 pm, "Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.com>
wrote:
"Cor Ligthert[MVP]" <notmyfirstn...@planet.nlwrote in message

news:5B**********************************@microsof t.com...
The only difference I see is that Onur does it in one line and Ken in two.
Can you explain more to me what you mean?
Cor

Despite the differences in the languages, my comments were based on the fact
his code either needs to calculate or hard-code the selection points.... as
in...
.Select(0, 12)

.
.
.
.Select(12, 19)

The font/color settings were just an attempt to show that, if you set them
*before* adding new text, there's no reason to know the selection points at
least that's the case with the VB6 control, which is an ActiveX wrapper for
riched32.dll. The 'one line versus two' wasn't the point of my comments.

I'm just wondering if the .Net control supports this functionality as
well.... I assume it does, but have been surprised before.

..but, we've already used more keystrokes talking about it than it would've
saved in a year, so.... <g>

--
Ken Halter
Part time groupie
It doesn't matter that it's shorter or longer if it just works.
However, i revised my previous code to make 2 lines shorter
here(without comments) by omitting Select method, and the thing i want
to point out is that, in fact, you don't need to select any existing
text to format it with newer font type, just use "SelectionFont"
property just before the new text that you're appending. When you
specify a font type using SelectionFont property, you're ready to
append text using that font without needing to use Select method as
follows:

' That outputs same as previous version
' without using Select method

With RichTextBox1
'Append text using Arial font
.SelectionFont = New Font("Arial", 12)
.AppendText("Visual Basic")
' From now on, append text using
' different font style eg:Courier New
.SelectionFont = New Font("Courier New", 12)
.AppendText(" is a good language")
End With

So, with the code above, it's not required to use hard-coded selection
index points and length which outputs the same as my previous code
posted.

Hope it's better and flexible,

Onur Güzel
Oct 9 '08 #12

P: n/a
Ken,

This was and is still for me one of the things I find confusing in the newer
versions of VB

\\\
New Font("Courier New", 12)
///

Cor

"Ken Halter" <Ken_Halter@Use_Sparingly_Hotmail.comschreef in bericht
news:uL**************@TK2MSFTNGP02.phx.gbl...
"Cor Ligthert[MVP]" <no************@planet.nlwrote in message
news:5B**********************************@microsof t.com...
>The only difference I see is that Onur does it in one line and Ken in
two.

Can you explain more to me what you mean?

Cor

Despite the differences in the languages, my comments were based on the
fact his code either needs to calculate or hard-code the selection
points.... as in...
> .Select(0, 12)
.
.
.
> .Select(12, 19)

The font/color settings were just an attempt to show that, if you set them
*before* adding new text, there's no reason to know the selection points
at least that's the case with the VB6 control, which is an ActiveX wrapper
for riched32.dll. The 'one line versus two' wasn't the point of my
comments.

I'm just wondering if the .Net control supports this functionality as
well.... I assume it does, but have been surprised before.

..but, we've already used more keystrokes talking about it than it
would've saved in a year, so.... <g>

--
Ken Halter
Part time groupie

Oct 10 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.