Pixels? What was Microsoft thinking?

J

John Bradley

I knew tackling VB.NET was going to be a huge ordeal.

I've been wasting days trying to do one simple thing -- determining
the "width" of a character in a RichTextBox. Sure, I can set the font
in the box to Courier New, 10 points -- but all the good that will do
me when VB.NET is configured in pixels!!??!!!

I've seen everything in here as an attempt to get my answers:

SizeF
PointP
CreateGraphics
Font(....)
....convert this to that and that to this
MeasureString (which there is apparently a bug)

What the XXXXX gives?!!

Try hitting the F1 key in VB.NET over the keyword "MeasureString" and
the "Call Statement" help topic comes up. So I'm back to square one.
In here trying to find a post that answers the simple question: "How
do I determine the width of a character in pixels if I'm using a
Courier New 10point font?"

With VB6, all I needed was a simple API call. Now it seems that the
simplest tasks in VB.NET requires hours of research on Google in hopes
that someone has come across the same problem. At this rate, my
simple program should be done in about 2 years!

ARRRRRGGGG!
 
F

Fergus Cooney

Hi John,

F1 can be frustrating at times. You get used to shuffling your code about
a bit. I sometimes find my self copying the code and then pasting it into the
index window of Help.

[A tip when looking for things in the index window, eg the methods of, eg,
the Graphics class. Type (no quotes) 'graphics cl'. Stopping at 'graphics'
will get you everything to do with graphics. Adding the space and 'cl' will
jump further to, usually, the class entry. Then, without needing any
scrolling, Enter will take you to the members page.]

In the case of MeasureString, the name on a line of its own will be
interpreted by F1 as a procedure call - hence its helpfulness in showing you
Call. Why? because that's what names at the start of a line are. And it hasn't
recognised MeasureString as being a keyword or a built-in function, etc. So it
knows that it must be a Call.

But why doesn't it know that MeasureString is a method of Graphics? Well
it does, but, being .NET, it also knows that you could have your <own> method
called MeasureString and be having trouble figuring out how to call it. Hence,
again, Call.

What you need to do with a method, if you want the correct help for it, is
put it in context.
Dim gr As Drawing.Graphics
gr.MeasureString

Now that MeasureString is correctly following a variable of the right
class, F1 will go to the correct topic.

If I had
Dim O As Object
O.MeasureString
then F1, not recognising MeasureString as an Object method, would go to
the list of methods for Object, hoping that I'd mistyped.

This 'context affect' is even more apparent when you consider a more
'versatile' method, eg ToString.
Me.ToString, when Me is a Form will go to Form.ToString
O.ToString will go to Object.ToString.

gr.ToString will also go to Object.ToString. This is because the Graphics
class doesn't override ToString for itself, but accepts the inherited version.
So F1 takes you to that one.

Ok, that's Help 101. That should reduce the project time by a few months.

I think you'll find that MeasureString gives you the size in pixels. ;-)

Regards,
Fergus
 
T

Tom Spink

Hi John,

Try this...

Dim g As Graphics = Me.CreateGraphics
Dim sfFontSize As SizeF = g.MeasureString("String Or Char To Measure",
RichTextBox1.Font)
g.Dispose

Now, you get the width and height, in sfFontSize

--
HTH,
-- Tom Spink, Über Geek

Please respond to the newsgroup,
so all can benefit

" System.Reflection Master "

==== Converting to 2002 ====
Remove inline declarations
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top