How to override the preferred size of a WinForms TextBox?

后端 未结 3 1914
春和景丽
春和景丽 2021-01-14 07:49

A TextBox in .NET does not let you adjust its height (there isn\'t even an AutoSize option).

i have a textbox that is cutting off the bottom of text in

3条回答
  •  说谎
    说谎 (楼主)
    2021-01-14 08:36

    Have you tried doing an override of

    protected override System.Drawing.Size DefaultSize
    { get { return new System.Drawing.Size( 145, 52 ); } }
    

    or whatever size you need

    Depending on your purposes, and something I've done in the past was to create my own sub-classes for certain controls and did an override of things like FontSize, FontName, Size, etc and made them all as read-only (only applying an externally visible "getter" ). By doing this, any instances of my class that were put onto a WinForms form that had the design-time serialization would nag at me to get rid of the "read-only" property from the designer. After doing so early on in the development, these controls never had a problem again. If I ever changed it, the form would get the newest value/size/font in the designer without having to go through every form and every instance of the control.

    I found it to work well with things like a textbox that would be showing date only, or date/time fields, additionally applied to labels, command buttons, and other "common" purpose elmeents. So, throughout the app, it was almost like applying a cascading style sheet, but at a WinForms level.

    Just a thought and hope it opens you to another approach to your dilemma.

提交回复
热议问题