Font Size on Winforms - c#

If I have a Control, I can know the Font it is using by the Font Property. Now how can I know the average Size of the Font( or lets say I would like to know what would it be the width of the 'M' char)
I know that Graphics.MeasureString() would solve my problem but implies creating a graphic object since I'm not in any paint event.. or wouldn't like to use the CreateGraphics. or nothing like that. I'm raising a grid and would like to set my column Width since I have all my columns text information.
(THis is for Winforms..... NOT WPF)

I am afraid there might not be a guaranteed accurate measurement, you can try TextRenderer.MeasureText.
A good post in SO here on "Accuracy of TextRenderer.MeasureText results"

Related

C# string.Format alignment issue

I have a problem with the alignment of a few names (changed for this question).
with this code I display the names in a richtextbox where it should be in one row with the "Spalte:" after the Names...but it doesn't. Can anybody help me please?
(only the first for-loop is necessary for my question, the next one does another job)
I added a pic what it looks like and how it should be looking. I know I can do it like its in the commented area (doesn't work in every case), but I need to change the code for a few other things so this needs to work....
Here is what I have:
And here is what I want:
Thanks for helping me :D
You have four options:
Change the code for the RichTextBox to use a fixed-width font. Most fonts have variable widths for each character, meaning you can't line things up neatly based on spacing alone.
Use tabs instead of spaces for the layout (and make sure the tab size is large enough to account for variances in your text).
Use a grid control of some type (DataGrid, GridView, etc)
Use a custom control for each row with labels at specific places (and either calculate positions yourself to place them on the form or use something like a FlowLayoutPanel).

richtextbox is shrinking at run time however textbox is not

i'm at a loss as to why my parameter richtextbox is resizing its height at runtime. I'm assuming that it has something to do with my high def displays.
all other fields are simply textbox.
any suggestions as to fields I can check? Form.AutoScale is set to font.
http://imgur.com/a/S0dq0
It seems that this issue is directly related to the use of the font "Microsoft YaHei". Changing the font to San Serif immediately resolved the issue.

Check if Control.Text is to long to be shown

I'm trying to do some automatic tests if the strings of a translated Application still fits the existing UI. The translation process just takes an existing resource assembly translates the contained resources and creates a new resources assembly for the new language. Easy but that way there is no garantee that the translations still fits into the UI (the UI is not involved in the translation process) and might get truncated all over the place. So for an automatic check i would need an idea on how to find truncated Text on the UI.
I tried so far:
Measuring the client size of a control, measuring the text length and
compare them. Doesn't work since there seem to be no way to find out
the ~real~ client size of a control that is used for putting text on
it (For a Button its not just Size minus Padding for example)
Setting AutoSize to true and checking if the control grows. That
would only work for non-wordwrapping controls and there seem to be
no sharp limit here. A control might grow to fit the Text on its
surface when setting autosize but the Text was fitting before
also. The margins might have been pretty narrow but the Text where
fitting.
Are there more idea that might work? Or are there some tweeks that might make the above mentioned ways work? Would be great if there where a simple Win API method i could call that would just give me the actual shown text of a control not the text a programmer/programm whishes to be shown on a control.
Get the amount of the characters from the non translated version and differentiate them against the length of the translated version then multiply the current controls width by the difference. It could look something like this.
float scalingAmount = 0.1 //This is just an example value, you'd probably want to adjust this yourself
float difference = oldLabel.Text.Length - label.Text.Length;
label.Width = oldLabel.Width * (difference/scalingAmmount)

Limit text to width of TextBox

I'm using a TextBox to create an editable title on something that looks like a post-it note. I changed to a TextBox from a RichEditBox to see if that could solve my problem, which it didn't, so I'm willing to change back if that helps.
My problem is that I don't want the user to be able to enter more characters than fit in the set width of the TextBox, because I want the whole title to be visible. Setting a fixed limit to the amount of characters that can be entered doesn't really work since for example 10 large M's would fill the width of my TextBox, but other characters will only fill half of it. So I would like to compare the width of the text to the width of the TextBox, so I can restrict input beyond that point.
Edit: I'm using the Segoe UI font, and I don't really consider changing the font to one with characters of equal width as a solution.
Set the TextBox Font to a Courier Font, then all characters are equal width and calculating max becomes trivial.
I am suck in programming world. i m try help you..

Accuracy of TextRenderer.MeasureText results

Calling TextRenderer.MeasureText as follows:
TextRenderer.MeasureText(myControl.Text, myControl.Font);
and comparing the result to the size of the control to check if text fits. The results are sometimes incorrect. Have observed the following two issues:
Often when a Label is set to AutoSize, TextRenderer will report a width that is 1 pixel wider than the auto-sized width of the Control.
False negative where TextRenderer reports a width smaller than the control's but the text is still cut off. This occurred with "EstaciĆ³n de trabajo" -- not sure if the accent could somehow affect the width calculation?
Is there any way to improve the accuracy of the MeasureText method? Should I be calling one of the overrides that accepts a device context and/or format flags?
I know it's probably no actual anymore. Yet for future readers here is a simple yet accurate method of measuring text in a control:
Graphics g=Graphics.FromHwnd(YOUR CONTROL HERE.Handle);
SizeF s=g.MeasureString("YOUR STRING HERE", Font, NULL, NULL, STRING LENGTH HERE, 1)
Is there any way to improve the accuracy of the MeasureText method? Should I be calling one of the overrides that accepts a device context and/or format flags?
You have answered your question by yourself. Actually MeasureText based on Win32 DrawTextEx, and this function cannot work without valid device context. So when you call MeasureText override without hdc, it internally create desktop compatible hdc to do measurement.
Of course measurement depends on additional TextFormatFlags. Also keep in mind that Label painting (and measurement) depends on UseCompatibleTextRendering.
So general conclusion you should use MeasureText for your own code, for example when you then call DrawText with exactly same parameters, in all other cases size returned by MeasureText cannot be treated as precise.
If you need to get expected Label size, you should use GetPreferredSize method.
Check out the TextFormatFlags parameter to this function:
TextRenderer::MeasureText(String, Font, Size, TextFormatFlags)
http://msdn.microsoft.com/en-us/library/8wafk2kt.aspx
"The Size, in pixels, of text drawn on a single line with the specified font. You can manipulate how the text is drawn by using one of the DrawText overloads that takes a TextFormatFlags parameter. For example, the default behavior of the TextRenderer is to add padding to the bounding rectangle of the drawn text to accommodate overhanging glyphs. If you need to draw a line of text without these extra spaces you should use the versions of DrawText and MeasureText that take a Size and TextFormatFlags parameter. For an example, see MeasureText(IDeviceContext, String, Font, Size, TextFormatFlags)."
hth
I don't know if I have a perfect solution but I ran into this when I was doing WinForms a few years back. The way I ended up compensating was by adjusting the returned measurement by a percentage. I cannot recall what I used (maybe 5% or 105?), but I do recall that I ended up using a constant percentage across the app and always rounded up.
I haven't got enough points to comment yet, so I've had to put this as an answer:
Perhaps ClearType affects measurement accuracy, because although a character has a known width calculated from its glyph, its rendering and position are adjusted to place axial lines on whole pixels.
Just a thought.

Categories

Resources