How to determine the width of a single character in TextBox?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a Windows Forms application using Visual Studio.NET in C#. I am creating a control derived from TextBox that will need to invoke a block cursor (caret) during certain edit modes. The derived control is intended to contain numeric values; integers and floating point. I understand how to create and position a caret using CreateCaret() and SetCaretPos(), available in user32.dll.

The control's font may vary from instance to instance so I want to make the caret have the correct width and height. I can get the height of the font by using either the TextBox.FontHeight property or the Font.Height property. However, I have not found a way to get the width of a character so that I can size the caret width appropriately. Since the control can contain a decimal point and a minus sign I would really like to determine the width of any individual character so that the caret does not overlap adjacent characters.

How can I determine the width of a character at a given position in a TextBox?

Thanks,
Dave
 
* "=?Utf-8?B?RGF2ZSBMZWFjaA==?= said:
I am writing a Windows Forms application using Visual Studio.NET in C#. I am creating a control derived from TextBox that will need to invoke a block cursor (caret) during certain edit modes. The derived control is intended to contain numeric values; integers and floating point. I understand how to create and position a caret using CreateCaret() and SetCaretPos(), available in user32.dll.

P/invoke on 'DrawText' + 'DT_CALCRECT' ('DT_WORDBREAK').
 
Hi Babel,

You may use Graphics.MeasureString to get the width of a string in a
certain font format. Basically you needn't mesure each single character,
just measure the string before the caret should get the x offset for the
caret. For more information you may read the doc of Graphics.MeasureString
method.

Let me know if you have anything unclear about this issue.

Have a nice day!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
* (e-mail address removed) ("Ying-Shen Yu[MSFT]") scripsit:
You may use Graphics.MeasureString to get the width of a string in a
certain font format. Basically you needn't mesure each single character,
just measure the string before the caret should get the x offset for the
caret. For more information you may read the doc of Graphics.MeasureString
method.

'MeasureString' will AFAIK use GDI+ to measure the string, but the
string is drawn using GDI, which will return slightly different results.
 
Hi,

I think I missed this difference.
Thanks for pointing it out, Herfried!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Community Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, please remove the word "online"
before sending mail.
 
Back
Top