C#: How to prevent a textbox' content from being scrolled up on Enter?
I'm trying to resize at run-time a textbox' height when the user presses the "Enter" key.
The resizing works well, but the problem arises from the fact that pressing "Enter" brings the text "up" by one line.
In other words, if my textbox contained two lines, only the 2nd one would be visible after pressing the "Enter" key.
The textbox would be resized correctly, and the carret positionned at the new line, but the whole content of the textbox won't be visible until the textbox has lost focus.
Anyone knows why this is happening ? Is there any way to 开发者_如何学Goprevent the textbox from automatically scrolling up the text when I press Enter ? (in the process hiding the first line and creating an unreachable line in the textbox).
Thanks
I assume you're handling the KeyPress event of the Textbox in order to change the size. Make sure you cancel the event (e.Handled = true
) in your handler - this will prevent the Enter
keystroke from also being applied to the text inside the Textbox (the Enter key is adding a CRLF to the end of the Textbox's Text property, which is what's causing the scroll-up).
精彩评论