Click here to Skip to main content
15,894,314 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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!
Posted
Updated 2-Apr-11 18:00pm
v2

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

C#
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 :)
 
Share this answer
 
Comments
Shahin Khorshidnia 3-Apr-11 7:07am    
Good answer but:
If he places 2 controls on it, for example a label and a textbox, then what may he do to have all textbox events?
Sergey Alexandrovich Kryukov 5-Apr-11 0:26am    
There is no "if"; why anyone would answer? The approach was wrong, that's it. Two control is completely different situation (and not a problems).
--SA
Shahin Khorshidnia 5-Apr-11 13:19pm    
And what's your exact problem ? ! !
Lee Reid 3-Apr-11 7:17am    
If one wanted to do that, I believe you would either have to:
1) Manually match make event in the textbox set off the matching event in the custom control (a little messy and time consuming, as he's stated)
or
2) simply make the textbox 'public', so that he could access its event handlers directly from other classes (ie access it outside of the custom control).
Shahin Khorshidnia 3-Apr-11 9:08am    
Ok Thanks
when you create your userControl, you should replace inheritance from "UserControl" to "TextBox"
good lock
 
Share this answer
 
Comments
[no name] 3-Apr-11 10:03am    
Thank you, your answer is kindly the same to Lee Reid's!
Thank you and good luck too!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900