I have created a textbox in a Windows Forms application that starts out at a height for entering text in a single line. But I would like the textbox to automatically increase its height if the user enters text that is wrapped within the control.
Currently, for this textbox, I have the properties multiline and wordwrap set to true. I've tried using the TextChanged event to determine when the text has been wrapped but I'm unable to find any property that will help me with this. The Lines property does not provide any help with wrapped text; only for text that the user has hit enter to begin a new line.
How can I get my textbox to expand its height each time the text wraps past the width of the textbox?
Same kind of idea as others have posted, put this in your textChanged event:
Dim s As SizeF = TextRenderer.MeasureText(txt.Text, txt.Font, txt.ClientRectangle.Size, TextFormatFlags.WordBreak)
txt.Height = CInt(s.Height)
You will need some kind of minimum height, and possibly to specify some padding, but this does work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With