问题
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 prevent 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
回答1:
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).
来源:https://stackoverflow.com/questions/5314995/c-how-to-prevent-a-textbox-content-from-being-scrolled-up-on-enter