Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expandable WinForms TextBox

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?

like image 204
Stephen Fletcher Avatar asked Dec 10 '22 19:12

Stephen Fletcher


1 Answers

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.

like image 183
Pondidum Avatar answered Jan 09 '23 10:01

Pondidum