Creating controls in C#

  • Thread starter Thread starter Jim
  • Start date Start date
J

Jim

I'm a little new to the C# way of doing thisgs... Although
I've done this sort of thing before in VS 6 / MFC...
creatnig controls, ActiveX controls, etc. Creating
controls in MFC is pretty straightforward and dare I say
intuitive? (well, as intuitive as you can get w/ MFC.)

Anyhow, I am wanting to create some controls of my own w/
C#. I'm not quite sure where to start though. I created
a Windows Control library project, and I get
a "Usercontrol1.cs" file / designer, now what? I tried
changing the class to inherit from a Textbox insted of a
UserControl, and my designer wouldnt' render the image
anymore, althuogh a text box did show up in my test
container, but now I'm a little stuck...

With MFC, I had a source listing of the base class so I
could tell what things I needed to override. I don't have
that in C#, and some of the functions (like OnDraw())
dont' seem to be the same, so I don't knwo where to go
from here.

What I want to create, for now at least, is just a simple
text box control for entering an IP address. I have
another post out abuot this subject, abuot the IP adderss
control that is already in Windows (somewhere) but in this
case, I want to create the control just for the practice
of doing it in C#. I am trying to get up to speed on this
now that I'll be working with it. Thanks!

JIM
 
Jim,

For what you are doing, you want to derive from TextBox, as you
suspected. When you have a custom control project (or rather, add a custom
control), it assumes you want a composite control with other controls on it,
so it automatically derives from UserControl.

However, if you are looking to create a base control, then you can not
do this obviously. Deriving from TextBox is the right thing, but as you
have found, you don't get designer support. Of course, you probably don't
need it either at this point.

The Control base class has a number of overrides. Check out the MSDN
documentation, and look at the protected members specifically. These are
the ones that are overridden to give your control the functionality you
want.

Specifically, you will want to look at the following properties/methods:

OnPaint
CreateParams
WndProc

Hopet his helps.
 
Thanks... I appreciate the info there. I hope that gets
me going ni the right direction.... I take it then that I
can remove the stuff from my class that the designer puts
in there? (the #region that says it's for the designer)
or should I just leave all that alone?

I was given to understand that migrating to C# from C++
wasn't going to be that big of a deal, but the more I see
of it, the moer I'm starting to wonder if MS just didn't
re-invent the wheel... It's a fine platform, and I'm too
new to pass judgement on it, it just seems that some
things that were once easy and intuitive have now become
obscure and in some cases (like the original IP control
from Windows) are outright absent from this new platform.

Perhaps though I am missing something-- It was / is my
understanding that .NET is supposed to replace the
previous VS / MFC way of diong things?

Years ago (liek 1991... LOL) I bought a great little
paperback called "Moving from C to C++" that gave me a
nice hands-on transition from procedural C language to the
object-orietned world of C++ (or at least as OO as you
could get before Windows... LOL) Are there any good,
concise resources on teh migration from C++ to C#, or C#
just a whole nwe ball of wax? I don't need to learn how
to program all over again, just need to learn to do the
things I already know how to do, but learn to do them ni
C#, and a basic understanding of the way apps and
components are put together in C# as opposed, for example
to C++. It all seems to be substantially different than
from what I expected, the more that i look at it. Thanks.

JIM
-----Original Message-----
Jim,

For what you are doing, you want to derive from TextBox, as you
suspected. When you have a custom control project (or rather, add a custom
control), it assumes you want a composite control with other controls on it,
so it automatically derives from UserControl.

However, if you are looking to create a base control, then you can not
do this obviously. Deriving from TextBox is the right thing, but as you
have found, you don't get designer support. Of course, you probably don't
need it either at this point.

The Control base class has a number of overrides. Check out the MSDN
documentation, and look at the protected members specifically. These are
the ones that are overridden to give your control the functionality you
want.

Specifically, you will want to look at the following properties/methods:

OnPaint
CreateParams
WndProc

Hopet his helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm a little new to the C# way of doing thisgs... Although
I've done this sort of thing before in VS 6 / MFC...
creatnig controls, ActiveX controls, etc. Creating
controls in MFC is pretty straightforward and dare I say
intuitive? (well, as intuitive as you can get w/ MFC.)

Anyhow, I am wanting to create some controls of my own w/
C#. I'm not quite sure where to start though. I created
a Windows Control library project, and I get
a "Usercontrol1.cs" file / designer, now what? I tried
changing the class to inherit from a Textbox insted of a
UserControl, and my designer wouldnt' render the image
anymore, althuogh a text box did show up in my test
container, but now I'm a little stuck...

With MFC, I had a source listing of the base class so I
could tell what things I needed to override. I don't have
that in C#, and some of the functions (like OnDraw())
dont' seem to be the same, so I don't knwo where to go
from here.

What I want to create, for now at least, is just a simple
text box control for entering an IP address. I have
another post out abuot this subject, abuot the IP adderss
control that is already in Windows (somewhere) but in this
case, I want to create the control just for the practice
of doing it in C#. I am trying to get up to speed on this
now that I'll be working with it. Thanks!

JIM


.
 
Back
Top