Fradam

Basically I am trying to make a console type application. And I am trying to figure out how to make it so the window is like 100 characters wide. I am using a monospaced font (Courier BT) so I was wondering if there was a way to figure out the width of each character given the font and the font point size.

Thanks in advance


Re: Visual C# General How to get the width of a character?

Keith Rome

TextRenderer.MeasureText() will provide you with the knowledge that you seek.

HTH






Re: Visual C# General How to get the width of a character?

Fradam

That didnt work for me. I do the following...

Font f = new Font("Courier New", 8.25F);
Size s = TextRenderer.MeasureText("1", f);
this.Width = s.Width * 100;

To try and make the width of the form to be the width of 100 characters of this font. But it says the width of a single character is 14, which makes the form a width of 1400. Which just doesn't make sense for a size 8 font.

Am i doing something wrong

Thanks




Re: Visual C# General How to get the width of a character?

Fradam

Nevermind I figured it out. For some reason TextRenderer.MeasureText() measures the first char as double the real size of it. So like TextRenderer("M", FONT) will return 14, but TextRenderer("MM", FONT) will return 21. I don't know if this is how they intended it to be but I just took the result of TextRenderer("M", FONT), divided by 2 and multiplied by the number or characters and it worked well.

Thanks for your help