473,320 Members | 1,845 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,320 software developers and data experts.

Simple Text Extent Question

Font font = new Font("Courier New", 8);
Graphics g = listBox1.CreateGraphics();
int width1 = g.MeasureString(" ", font).Width; // a space
int width2 = g.MeasureString("S", font).Width;

width1 does not equal width2. Since it's a monospace font, why not?

That's the question boiled down to its essence. The background is
this: I'm trying to display a tooltip based on the character the mouse
is hovering over in a DataGridView. For unrelated reasons, I'm not
using the built-in tooltip support, and I have implemented the
CellMouseMove event for the datagridview as follows (simplified):

private ToolTip tt = new ToolTip();
private Font f = new Font("Courier New", 8);

private void grid_CellMouseMove(object sender,
DataGridViewCellMouseEventArgs e)
{
Graphics g = grid.CreateGraphics();

// Since we're using a monospace font, one character should have
// the same width of as all the others, but it isn't working that
way.
// Why is that?
float charWidth = g.MeasureString("1", f).Width;

tt.SetToolTip(grid, "Hovering over character " + (int)
(e.Location.X / charWidth));
}

Mar 16 '07 #1
1 4347
MeasureString does not work very well with single characters, as it takes in
account aliasing, kerning, extras and many other things that can fool it.

You might try TextRenderer.Measure, or handle a special case when you have
just a blank to use another char, or btter not to measure single chars but
at least two chars.

<dv*****@gmail.comha scritto nel messaggio
news:11**********************@p15g2000hsd.googlegr oups.com...
Font font = new Font("Courier New", 8);
Graphics g = listBox1.CreateGraphics();
int width1 = g.MeasureString(" ", font).Width; // a space
int width2 = g.MeasureString("S", font).Width;

width1 does not equal width2. Since it's a monospace font, why not?

That's the question boiled down to its essence. The background is
this: I'm trying to display a tooltip based on the character the mouse
is hovering over in a DataGridView. For unrelated reasons, I'm not
using the built-in tooltip support, and I have implemented the
CellMouseMove event for the datagridview as follows (simplified):

private ToolTip tt = new ToolTip();
private Font f = new Font("Courier New", 8);

private void grid_CellMouseMove(object sender,
DataGridViewCellMouseEventArgs e)
{
Graphics g = grid.CreateGraphics();

// Since we're using a monospace font, one character should have
// the same width of as all the others, but it isn't working that
way.
// Why is that?
float charWidth = g.MeasureString("1", f).Width;

tt.SetToolTip(grid, "Hovering over character " + (int)
(e.Location.X / charWidth));
}

Mar 16 '07 #2

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

Similar topics

19
by: Svennglenn | last post by:
I'm working on a program that is supposed to save different information to text files. Because the program is in swedish i have to use unicode text for ÅÄÖ letters. When I run the following...
3
by: Christopher Benson-Manica | last post by:
I need a simple HTML element whose text I can change dynamically. What is it? (it doesn't seem to be <div>, since I can't seem to find what properties a <div> has...) -- Christopher...
6
by: Pete | last post by:
Hello everybody -- Forgive my multi-posting my question. I posted first to ciwah, but I learned that ciwas is the better group for this CSS question. The problem has me stopped. The page: ...
7
by: J. Hall | last post by:
Hi dudes, Got a simple webpage, with three numeric text input boxes, the idea being that the user is asked to insert percentages of their business around the world... UK, Europe, Other ...
9
by: Racerx | last post by:
Hi All: Can anyone suggest me how to determine the optimised size for the following: 1.Extends 2.Tablespaces 3.Bufferpools Regards,
3
by: Asphalt Blazer | last post by:
Can anybody explain to me the relation between extentsize and pagesize. When I am creating a new tablespace what do I need to be careful of? Say pagesize is 8K is keeping extentsize(pages) 64 too...
0
by: boeledi | last post by:
I'm new in GDI programming and I am facing the following problem. I try to dynamically create "thumbnail"-like images (JPEG) in which I need to draw text. This works fine, except that the text is...
2
by: Jared | last post by:
Normally when one is creating a table in a striped tablespace one can, with a little trouble, force extent allocation in a round-robin fashion. I never do things the easy way :-); what I want to...
14
by: Stanimir Stamenkov | last post by:
I've found some contradiction I want to resolve. <http://www.w3.org/MarkUp/2004/xhtml-faq#mime11states: I've noted "_disallowed_ text/html" and "_must_ be sent with an XML-related media...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.