开发者

Winform Textbox CanGrow?

开发者 https://www.devze.com 2022-12-11 16:07 出处:网络
I don\'t find a CanGrow property on the Textbox control. This is common in some other controls, and what it does is expand the control to acomodate more data. Anyway to get this feature 开发者_C百科in

I don't find a CanGrow property on the Textbox control. This is common in some other controls, and what it does is expand the control to acomodate more data. Anyway to get this feature 开发者_C百科in the TextBox?


Well, I came up with this:

Private Sub TextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox.TextChanged
        'check to see if textbox has text
        If (TextBox.TextLength > 0) Then
            'resize height of textbox by count of lines (plus add some padding)
            TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32((TextBox.Lines.Length * TextBox.Font.Height) + (TextBox.Font.Height * 0.5)))
        Else
            'resize to one line height (plus padding)
            TextBox.ClientSize = New Size(TextBox.ClientSize.Width, Convert.ToInt32(TextBox.Font.Height + (TextBox.Font.Height * 0.5)))
        End If
End Sub

Note: it doesn't work with word-warp.


I'm not familiar with CanGrow. Are you looking for Anchor property perhaps?


Anyway to get this feature in the TextBox?

Well, yes, but, you may need to look into doing this manually. The Graphic.MeasureString() function may be what you are looking for in order to set the width properly.

Keep in mind that MeasureSting may have issues measuring multiline strings.


If you set the anchor properties to top,left,bottom,right then the control will grow as the form resizes.

I think a better option is to use docking though. I usually set up a panel layout with one docked to client, then I put the control I want resized in the panel docked to client, and set the control to dock to client as well.

0

精彩评论

暂无评论...
验证码 换一张
取 消