Windows Develop Bookmark and Share   
 index > Windows Forms Designer > Creating custom controls
 

Creating custom controls

Hello,

I have got one question about creatingmy own controls in Visual Studio. For egxample, I want to have a button which shape is a circle. HowI coulddo that?

Thanks in advance.

Kestutis  Wednesday, April 25, 2007 11:15 AM
Imho, you're best off inherting from Button and override the Paint method...
timvw  Wednesday, April 25, 2007 11:36 AM

I'm assuming that you probably want to create more controls than a button.

You would need to use the "UserControl" class. Override theOnPaint, then in theon-paint method, you would paint a circle that looks like you want. Then on MouseDown you would paint it different to make it looked like it is pressed (for your button example).

To make it even better, I would make the Region of the control a circle, that way the click events would only work in the actual circle.

You can make a user control look however you want or contain any controls you want. For example, you could makea sincle control that contains a label, textbox and a button. If you are wanting to create your own controls, the UserControl class is the way to go.

Ken

Ken_Bussell  Wednesday, April 25, 2007 2:06 PM
There are a couple of possible approaches:

- When you want to modify the behaviour of an existing control you'll probably want to inherit from the control
- When you want to compose a new control that exists out of a set of existing controls, then you'll probably want to inherit from UserControl
- When you simply want to add a Property to a control you could look into an IExtenderProvider

(And i've got the feeling i forgot to mention an option...)
timvw  Wednesday, April 25, 2007 2:56 PM
A websearch for 'winforms custom controls' will probably provide you with more than enough valuable links... (eg: http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx)
timvw  Wednesday, April 25, 2007 6:21 PM
Imho, you're best off inherting from Button and override the Paint method...
timvw  Wednesday, April 25, 2007 11:36 AM

I'm assuming that you probably want to create more controls than a button.

You would need to use the "UserControl" class. Override theOnPaint, then in theon-paint method, you would paint a circle that looks like you want. Then on MouseDown you would paint it different to make it looked like it is pressed (for your button example).

To make it even better, I would make the Region of the control a circle, that way the click events would only work in the actual circle.

You can make a user control look however you want or contain any controls you want. For example, you could makea sincle control that contains a label, textbox and a button. If you are wanting to create your own controls, the UserControl class is the way to go.

Ken

Ken_Bussell  Wednesday, April 25, 2007 2:06 PM
There are a couple of possible approaches:

- When you want to modify the behaviour of an existing control you'll probably want to inherit from the control
- When you want to compose a new control that exists out of a set of existing controls, then you'll probably want to inherit from UserControl
- When you simply want to add a Property to a control you could look into an IExtenderProvider

(And i've got the feeling i forgot to mention an option...)
timvw  Wednesday, April 25, 2007 2:56 PM

Thanks for answers.

Could anyone give a link on MSDN or something other resource about creating custom controls?

Kestutis  Wednesday, April 25, 2007 4:41 PM
A websearch for 'winforms custom controls' will probably provide you with more than enough valuable links... (eg: http://samples.gotdotnet.com/quickstart/winforms/doc/WinFormsCreatingControls.aspx)
timvw  Wednesday, April 25, 2007 6:21 PM

You can use google to search for other answers

Custom Search

More Threads

• Subcribing to a parent event from a child....form / usercontrol...
• Displaying small Icon on top of a Control (in Design Mode)
• Apply DesignTimeVisible Attribute Dynamically By Condition
• Custom design-time control dragging.
• CollectionEditor only calls DestroyInstance for items added during current session
• DataGeidView Problem
• Customize controls in VB.NET
• Error in Windows Forms Designer VS 2005
• combo box like textbox
• Can you get to the open project or solution files while in a Designer in Design mode