C#:如何防止在Enter上向上滚动文本框的内容? [英] C#: How to prevent a textbox' content from being scrolled up on Enter?

查看:93
本文介绍了C#:如何防止在Enter上向上滚动文本框的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户按下"Enter"键时,我正在尝试在运行时调整文本框的高度.

I'm trying to resize at run-time a textbox' height when the user presses the "Enter" key.

调整大小效果很好,但是问题是由于按下"Enter"键使文本"up"仅一行一行.

The resizing works well, but the problem arises from the fact that pressing "Enter" brings the text "up" by one line.

换句话说,如果我的文本框包含两行,则在按下"Enter"键后,只有第二行可见.

In other words, if my textbox contained two lines, only the 2nd one would be visible after pressing the "Enter" key.

将正确调整文本框的大小,并将carret放置在新行上,但是直到文本框失去焦点之前,文本框的全部内容才可见.

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.

有人知道为什么会这样吗?有什么方法可以防止按Enter键时文本框自动向上滚动文本? (在此过程中,隐藏第一行并在文本框中创建无法访问的行).

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).

谢谢

推荐答案

我假定您正在处理文本框的KeyPress事件,以更改大小.确保取消处理程序中的事件(e.Handled = true)-这将防止将Enter击键也应用于文本框内的文本(Enter键在文本框的Text属性的末尾添加了CRLF,这就是导致向上滚动的原因.

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).

这篇关于C#:如何防止在Enter上向上滚动文本框的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆