具有完整事件的用户控制? [英] User control with full events?

查看:40
本文介绍了具有完整事件的用户控制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为自己创建了一个诸如文本框之类的控件,但是它可以过滤输入字符,并且只允许用户键入某些字符(仅数字字符).但是我的问题是:
我通过在空白用户控件的表面上放置一个普通文本框来创建该自定义文本框,并编写了代码以过滤与该文本框的keydown和keyup事件相关联的字符.但是,当将该新控件编译为dll文件并在新项目中使用它时,用户引发的所有事件(例如鼠标悬停,按键...)都属于真实的文本框(放置在用户控件上的文本框),而不是我的自定义的文本框.因此,当用户希望运行某些代码(例如按下键时)时,他们通常无法通过将该代码与我的自定义文本框的KeyPress事件相关联来执行此操作,因为用户永远不会引发该代码(内部的真实文本框会接收并接收引发该事件).
我真的不知道该怎么解决?
我希望由实际文本框引发的所有事件也将由我的自定义文本框引发,以帮助用户轻松地通过控件及其附加功能(过滤字符)进行程序编程.有什么方法可以轻松,简单地做到这一点吗?否则我必须自己筹办所有活动. (我的意思是我必须通过为真实文本框的相应事件编写一些代码来引发自定义文本框的所有事件,这很难做到,不是吗?).
非常感谢!

I created for myself a control like a Textbox, but it can filter input characters and only allows user to type in certain characters (such as digital characters only). But my problem is:
I created that custom textbox by placing a normal textbox onto the surface of the blank user control and wrote codes to filter characters associating with keydown and keyup events of that textbox. But when compiling that new control into a dll file and using it in a new project, all events user raises (such as mouse hover, keypress ...) are belong to the real textbox (the textbox placed on the user control) not my customized textbox. So when user wants some code to be run such as when key pressed, they can''t do it normally by associating that code to KeyPress event of my customized textbox because it will never be raised by user (the real textbox inside will receive and raise that event).
I really don''t know how to solve this?
I want all events raised by the real textbox will be also raised by my customized textbox to help user program with my control easily plus its additional functionality (filtering characters). Is there any way to do that easily and simply? Or I have to raise all the events by myself. (I mean I have to raise all the events for my custom textbox by writing some code for the corresponding events of the real textbox, it''s hard to do , isn''t it?).
Thank you so much!

推荐答案

我认为我们已经在某个时间点完成了这一操作.
这是您的问题:"我是通过在空白用户控件的表面上放置一个普通文本框来创建该自定义文本框的" "

如果您只是在扩展这样的类,请不要创建新的用户控件.只需在代码视图中创建一个新类,然后指定它是从TextBox派生的.然后,将过滤与该文本框的keydown和keyup事件相关联的字符的代码"所需的所有其他代码添加到此派生类中.等

例如一个简单的文本框,其默认背景颜色为蓝色,以及覆盖按键事件等的示例

I think we''ve all done this one at some point in time.
Here''s your problem: "I created that custom textbox by placing a normal textbox onto the surface of the blank user control"

If you''re simply extending a class like this, don''t go creating a new user control. Simply create a new class in code view, then specify that it derives from TextBox. Then add into this derived class all the additional code you need for your "codes to filter characters associating with keydown and keyup events of that textbox." etc.

e.g. a simple textbox which has a default background colour of Blue, and an example of overriding keydown events etc

using System.Windows.Forms;
namespace DrummerWriter
{
    class Textbox2 : TextBox
    {
        public Textbox2()
            : base()
        {
            BackColor = System.Drawing.Color.Blue;
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);
            //Put additional code used for key down here...
        }
        //Other methods, overrides etc...
    }
}




希望对您有所帮助:)




Hope that helps :)


在创建userControl时,应将继承从"UserControl"替换为"TextBox"
锁好
when you create your userControl, you should replace inheritance from "UserControl" to "TextBox"
good lock


这篇关于具有完整事件的用户控制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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