C#: How to prevent a textbox' content from being scrolled up on Enter?

假装没事ソ 提交于 2019-12-11 03:32:43

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!