Formatting TextBox display (non-fixed width fonts)

  • Thread starter Thread starter jp2msft
  • Start date Start date
J

jp2msft

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?
 
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.
 
I think I found a work-around: I can use a RichTextBox instead of a standard

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.
 
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! :)
 
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
 
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.
 
AMercer said:
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,

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,

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)
 
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
 
Cor Ligthert said:
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>
 
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...


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>

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
 
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
 
Back
Top