Autosize Property in 2005's TxtBox

NeuralJack

Centurion
Joined
Jul 28, 2005
Messages
138
In the form designer Autosize is not listed for textboxes. But in code you can set the autosize prop to False. By default it's True I think.

Do i really have to go in and set this stuff up with code?

By default it seems you can size the width of the textbox, but not the height. I'm sure there's some simple solution that I dont know about.
 
NeuralJack said:
In the form designer Autosize is not listed for textboxes. But in code you can set the autosize prop to False. By default it's True I think.

Do i really have to go in and set this stuff up with code?

By default it seems you can size the width of the textbox, but not the height. I'm sure there's some simple solution that I dont know about.

I had a similar problem with autosizing the height of a label depending on it's contents. I'm not sure if it will be of any help, but cags posted some nice and simple managed code to do it. Here it is as a class:

C#:
public class AutoSizeLabel
{
	public static void AutoSizeLabelHeight(Label ctlLabel)
	{
		Graphics g = ctlLabel.CreateGraphics();
		Font font = new Font(ctlLabel.Font.Name, ctlLabel.Font.Size, ctlLabel.Font.Style);

		StringFormat sf = new StringFormat();

		SizeF stringSize = g.MeasureString(ctlLabel.Text, font, ctlLabel.Width, sf);

		ctlLabel.Height = (int) stringSize.Height + 2;

		g.Dispose();
		font.Dispose();
		sf.Dispose();
	}
}
 
I've never noticed before, but now that you mention it, AutoSize is MIA from the property window. I guess Microsoft decided to reduce clutter in the property window and AutoSize was one of the ones to go.

I suppose you will have to live with the fact that you have to set the property in code. If it is horribly inconvinient for you, you can subclass the TextBox class and create a new AutoSize property that will be visible in the property window. In fact, here it is for you:
Visual Basic:
'Textbox Plus AutoSize property in the property grid
Public Class TextBoxPlus
    Inherits TextBox
 
    <System.ComponentModel.Browsable(True)> _
    Public Shadows Property AutoSize() As Boolean
        Get
            Return MyBase.AutoSize
        End Get
        Set(ByVal value As Boolean)
            MyBase.AutoSize = value
        End Set
    End Property
End Class
 
Thanks for the input guys. Marble, forgive my ignorance but I'd like to use your class in the form designer, I think, if possible. How would I set that up?

That's the real issue here is not being able to set the Autosize in the form designer basically makes the form designer near worthless for textboxes. In my opinion if MS was going to remove some properties from the form designer they should have been non-design related properties.
 
I don't know about VB Express, but with C# Express any controls and components you declare in a project automatically appears at the very top of the toolbox (I know that this wasn't the case in .Net 2003).

As far as your comment about removing design-relate properties, if by design-related properties you mean those regarding the form's layout then I personally agree whole-heartedly, but I suppose that Microsoft had their reasons. Multi-line textboxes are never autosized and single-line textboxes that are oversized or undersized often simply don't look right with extra white below the text or hanging letters clipped on the bottom. I guess they deemed the property irrelevant. I, however, sometimes like to nudge things a pixel or two to make the elements of the UI line up.
 
Ya I cant seem to get TextBox Plus to show up in the tool box in the form designer, so i can't seem to plop it onto my form. I could be doing something wrong though because i've never added a control to the toolbox before. I tried copying the class to all of these places:
in my main forms class
outside my main forms class but in the same file form1.vb

And ya, a few things are irritating about design these days. Why is there such a huge space between what seems to be the end of the control and where you can first start seeing text, such as button text. There's like 2-5 pixels, it seems, that cant be used all around the edge of the control. And that autosize thing drives me nuts. Then the text-alignment really only seems to work right if your control is above a certain size. I often like to keep my controls small.

marble_eater said:
I don't know about VB Express, but with C# Express any controls and components you declare in a project automatically appears at the very top of the toolbox (I know that this wasn't the case in .Net 2003).

As far as your comment about removing design-relate properties, if by design-related properties you mean those regarding the form's layout then I personally agree whole-heartedly, but I suppose that Microsoft had their reasons. Multi-line textboxes are never autosized and single-line textboxes that are oversized or undersized often simply don't look right with extra white below the text or hanging letters clipped on the bottom. I guess they deemed the property irrelevant. I, however, sometimes like to nudge things a pixel or two to make the elements of the UI line up.
 
Back
Top