自定义RichTextBox事件句柄 [英] Custom RichTextBox Event Handle

查看:114
本文介绍了自定义RichTextBox事件句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Custom Controls的新手,我正在寻找一些帮助。

I'm new to Custom Controls and I'm looking for some help.

我想知道是否可以在我的自定义类中的事件(如Key_Press)添加验证,而不是通过表单代码中的事件添加验证。我的目标是阻止使用回归输入控件的键。

I want to know if it is possible to add validation on an event such as a "Key_Press" within my Custom Class rather than through an Event in my form code. I aim to block the use of the Return & Enter keys for the control.

我已经创建了一个自定义RichTextBox,代码如下: -

I have created a custom RichTextBox, code below :-

public class CustomRTB : RichTextBox
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {

            if ((keyData == (Keys.Control | Keys.V)))
            {
                IDataObject iData = Clipboard.GetDataObject();

                if (iData.GetDataPresent(DataFormats.Text))
                {
                    string contents = Clipboard.GetText().Replace("\r\n", " "); 
                    Clipboard.SetData(DataFormats.Text, contents);
                    this.Paste();
                }

                return true;
            }
            else
            {
                return base.ProcessCmdKey(ref msg, keyData);
            }
        }
    }


推荐答案

通过简单地覆盖OnKeyDown()方法来阻止Enter键。一个适用于任何RTB的普通KeyDown事件的例子:

Block the Enter key by simply overriding the OnKeyDown() method. An example of a plain KeyDown event that works for any RTB:

    private void richTextBox1_KeyDown(object sender, KeyEventArgs e) {
        if (e.KeyData == Keys.Enter) e.Handled = e.SuppressKeyPress = true;
    }

这篇关于自定义RichTextBox事件句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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