km9

Hi,

I am trying to create a method that determines the width of a textbox in pixels based on the max length of a string.

The method takes a string and the font to be used by the textbox.

Before calling the method, I call some code that creates a string based on some user determined parameters, for example the user wants to set the box for 5 character, so I build a string with 5 zeros: "00000".

However whenever I use this method I manually enter the reqired number of zeros into the textbox, and the text never quite fits.

Looking around on MSDN, this seems a reasonable way of calculating the required length, but I cant quite get it to work.

Has anyone else attemted this method/ can they see where I am at fault or is there a betterway to do this as I am not too happy with basing it on a string oz Zeros.

Code Block

private int FindStringWidth(string measurementString, Font f)

{

// Create a graphics object

System.Windows.Forms.Control graphicsControl = null;

Graphics measureGraphics = null;

int width = 0;

try

{

graphicsControl = new System.Windows.Forms.Control();

measureGraphics = graphicsControl.CreateGraphics();

measureGraphics.TextRenderingHint = TextRenderingHint.AntiAlias;

// Create a StringFormat

StringFormat format = new StringFormat();

format = StringFormat.GenericTypographic;

format.Trimming = StringTrimming.None;

// Create a rectangle and regionto hold the output size

RectangleF rectangle = new RectangleF(0.0f, 0.0f, MaxWidthValue, MaxWidthValue);

Region[] regions = new Region[1];

// Create a character range to measure

CharacterRange[] ranges = { new CharacterRange(0, measurementString.Length) };

format.SetMeasurableCharacterRanges(ranges);

// Measure the string

regions = measureGraphics.MeasureCharacterRanges(measurementString, f, rectangle, format);

rectangle = regions[0].GetBounds(measureGraphics);

width = (int)(rectangle.Width + 0.5f);

}

finally

{

// Clean up the Graphics object.

measureGraphics.Dispose();

graphicsControl.Dispose();

}

// Return the string length

return width;

}

Thank you,

Kenny



Re: Windows Forms General Programatically setting a text box to fit maximum width

Kenny99

sorry.. forgot to account for the textbox border;

also appologies for wrong forum (originaly posted in c# gen),

However, I would still appreciate comments on a 'better' way of doing this,

Reagards,

km





Re: Windows Forms General Programatically setting a text box to fit maximum width

Fábio

I don't think that is possible, this is because every character may have a different width. So "abcde" width is different

than "ABCDE" width. You can set the width correctly if you know the string value. But, for instance, if you want

a textbox that holds 5 characters be the width of any 5 characters string you will never have a fixed size for that.

"AbCdE" has different width than "abcDE", than "1bCdE", than "ABCDE", than "AbCdl" and so on.

The best you could do about it is to use a font that has all its UpperCase characters (Not counting numbers) with the

same width. And let the user only type uppercase characters. And use Graphics.MeasureString(string, font) to get

the correct size.






Re: Windows Forms General Programatically setting a text box to fit maximum width

Kenny99

What I used does appear to work, if I account for the width of the border. If the user has stated 5 chars, I supply a string with 5 Zeros for the seleced font, as generaly speaking '0' is the widest character (I should have mentioned, the box is to be used for numerical input only), however, thanks for pointing out Graphics.MeasureString(string, font), maybe I could make use of that to cut down on the amount of code;

Thanks

Km