How to override the preferred size of a WinForms TextBox?

后端 未结 3 1912
春和景丽
春和景丽 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:42

    I found a solution for the Rich text box height issues.. i have modified it a for general use..

    Create following structs in your application....

    [StructLayout(LayoutKind.Sequential)]
        public struct RECT {
            public Int32 left;
            public Int32 top;
            public Int32 right;
            public Int32 bottom;
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct SCROLLBARINFO {
            public Int32 cbSize;
            public RECT rcScrollBar;
            public Int32 dxyLineButton;
            public Int32 xyThumbTop;
            public Int32 xyThumbBottom;
            public Int32 reserved;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)]
            public Int32[] rgstate;
        }
    

    Create following private variables in your class for form (where ever you need to calculate rich text height)

    private UInt32 SB_VERT = 1;
            private UInt32 OBJID_VSCROLL = 0xFFFFFFFB;
    
            [DllImport("user32.dll")]
            private static extern
                Int32 GetScrollRange(IntPtr hWnd, UInt32 nBar, out Int32 lpMinPos, out Int32 lpMaxPos);
    
            [DllImport("user32.dll")]
            private static extern
                Int32 GetScrollBarInfo(IntPtr hWnd, UInt32 idObject, ref SCROLLBARINFO psbi);
    

    Add following method to your Class for form

    private int CalculateRichTextHeight(string richText) {
                int height = 0;
                RichTextBox richTextBox = new RichTextBox();
                richTextBox.Rtf = richText;
                richTextBox.Height = this.Bounds.Height;
                richTextBox.Width = this.Bounds.Width;
                richTextBox.WordWrap = false;
                int nHeight = 0;
                int nMin = 0, nMax = 0;
    
                SCROLLBARINFO psbi = new SCROLLBARINFO();
                psbi.cbSize = Marshal.SizeOf(psbi);
    
                richTextBox.Height = 10;
                richTextBox.ScrollBars = RichTextBoxScrollBars.Vertical;
    
                int nResult = GetScrollBarInfo(richTextBox.Handle, OBJID_VSCROLL, ref psbi);
                if (psbi.rgstate[0] == 0) {
                    GetScrollRange(richTextBox.Handle, SB_VERT, out nMin, out nMax);
                    height = (nMax - nMin);
                }
    
                return height;
            }
    

    You may need to modify above method to make it work as per your requirement... Make sure to send Rtf string as parameter to method not normal text and also make sure to assign available width and height to the Richtextbox variable in the method...

    You can play with WordWrap depending on your requirement...

提交回复
热议问题