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

最后都变了- 提交于 2019-12-08 13:55:39

问题


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 );

And...

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?


回答1:


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);
}


来源:https://stackoverflow.com/questions/38520640/how-to-get-caret-cursor-height-in-textbox-using-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!