inherit TextBox control

Y

Yoavo

Hi,
I have a dialog with 2 TextBox controls.
I want to add some functionality to one of the them.
I created a class MyTextBox which inherits from TextBox.
How can I connect one of the TextBox controls to my new class ?

Yoav.
 
A

Alberto Poblacion

Yoavo said:
I have a dialog with 2 TextBox controls.
I want to add some functionality to one of the them.
I created a class MyTextBox which inherits from TextBox.
How can I connect one of the TextBox controls to my new class ?

You don't exactly "connect" the textbox with the new class. Instead, the
textbox has to be an instance of the new class. Search your source code for
the place where the textbox is declared and initialized. If you are using
Visual Studio 2002/2003, this is in the .cs file for your Form in the block
labelled "designer generated code". If You are using VS 2005, you have to
click on the icon "show all files" in solution explorer, and then open the
file with the extension .designer.cs. You will find declarations similar to
this ones:

TextBox texbox1;
...
textbox1 = new TextBox();

In those two lines, replace TexBox with MyTextBox, and you are done.

Another way to do it is to compile your new class into a dll, and then
add it to the toolbx in the windows forms designer (right-click on the
toolbox). Once you do that, the MyTextBox will appear in the toolbox, and
you can drag it from there into your form just like the standard textbox. If
you are using version 2005, you don't need to do any of this, since the
controls defined inside your project will appear automatically at the top of
the toolbox, under "myproject components".
 
Y

Yoavo

I am using VS 2005 and my application is a WPF program.
The wizard created a file "Window1.g.cs" and in it there is the window
class:
public partial class Window1 : System.Windows.Window,
System.Windows.Markup.IComponentConnector {
....
internal System.Windows.Controls.TextBox YoavTextBox;
}


with the method:
void System.Windows.Markup.IComponentConnector.Connect(int connectionId,
object target) {
....
this.YoavTextBox = ((System.Windows.Controls.TextBox)(target));
}

when I tried to replace "System.Windows.Controls.TextBox" with "MyTextBox",
I got compilation errors.
 
A

Alberto Poblacion

Yoavo said:
I am using VS 2005 and my application is a WPF program.

Ah, sorry. Since you didn't specify, I thought you were doing a plain
old WinForms application. I have played a little bit with WPF, but I have
not gone far enough to know how to subclass controls on this platform.
Someone more knowledgeable than me will have to provide the answer.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top