允许用户插入TAB到文本框,但不换行 [英] Allow users to insert a TAB into a TextBox but not newlines

查看:203
本文介绍了允许用户插入TAB到文本框,但不换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个TextBox它不接受TAB键(并将一个TAB,ASCII×09,使用\ t相应地进入文本框),而不是跳到下一个控件。该文本框有一个属性AcceptsTab,我已经设置为true,但是这并没有得到期望的结果。原来,AcceptsTab属性只能那么多行设置为true也是如此。不过,我想有一个单行文本框不接受换行。

I want to have a TextBox which does accept the TAB key (and places a TAB, ASCII 0x09, \t accordingly into the textbox) instead of jumping to the next control. The TextBox has a property AcceptsTab, which I have set to true but this does not give the desired result. It turns out the AcceptsTab property only works then Multiline is set to true as well. However I want to have a one-line TextBox which doesn't accept newlines.

推荐答案

下面就是你要做的。创建您自己的类,它继承文本框。在构造函数中,设置多行为true,并AcceptsTab为true。然后,重写的WndProc和使用code:

Here's what you do. Create your own class that inherits from TextBox. In the constructor, set multiline to true, and AcceptsTab to true. Then, override WndProc and use this code:

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == (int)0x102 && m.WParam.ToInt32() == 13)
            {
                return;
            }

            base.WndProc(ref m);
        }

这将阻止被接受,因此新的生产线将不会创建回车键。哈克?是的,但它会工作。

This will block the enter key from being accepted and therefore a new line won't be created. Hacky? yes, but it'll work..

编辑:我会解释这是什么code一样。每个Windows窗体和控件得到有事时,如油漆,关键下来等等等等......在这种情况下,Windows消息,我找了一个WM_CHAR消息(这是0x102),这是它告诉文本框的消息,该消息关键是pressed。如果是这样的消息,wParam参数== 13,则意味着进入了pressed,在这种情况下,返回,什么也不做。否则,继续如预期。有道理?

I'll explain what this code does. Every windows form and control get a windows message when something happens, like paint, key down etc etc... In this case, I'm looking out for a WM_CHAR message (which is 0x102) which is the message that tells the TextBox which key was pressed. If that's the message, and the WParam == 13, then that means Enter was pressed, in which case, return, do nothing. Else, resume as expected. Makes sense?

这篇关于允许用户插入TAB到文本框,但不换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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