Muhammad Hassan Muhammad Hassan - 1 year ago 105
C# Question

How To Get Caret Cursor Height In TextBox Using C#?

I have some textBox having multiple line feature on with having different font size. Now I want to know the caret cursor height of this textbox text as its a simple text box so caret cursor will be able to resize as per font change etc and in other words I want to know the line height of the textBox or the font size of textBox.

int fontHeight ;
using (Graphics g = textBox1.CreateGraphics()) {
float points = textBox1.Font.SizeInPoints;
fontHeight = Convert.ToInt16(points * g.DpiX / 72);
MessageBox.Show("myFont size in pixels: " + fontHeight );


int fontHeight = Convert.ToInt32(textBox1.Font.Size);

For this I am using the above codes but not giving the full results so How to make it more perfect?

Answer Source

After trying some codes and snippets and edit them I got the below solution for every size of text that is shared below and its working perfectly.

int lineHeight;
using (Graphics g = textBox1.CreateGraphics())
    lineHeight = Convert.ToInt32(g.MeasureString("A", textBox1.Font).Height);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download