Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms: How to extend a button?

Tags:

c#

winforms

How can I extend a Button?

I want a Button that has an additional property IsSwitchOffable.

Do I have to write an extra custom control?

EDIT: I want that the button is usable as a standard WindowsFormsButton.

This includes that I can add the button at design time!

like image 247
Rookian Avatar asked Jul 29 '10 16:07

Rookian


People also ask

How do I use buttons in Windows Forms?

A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if the button has focus. Set the AcceptButton or CancelButton property of a Form to allow users to click a button by pressing the ENTER or ESC keys even if the button does not have focus.

How do I maximize a Windows Form?

Set the WindowState property of your form to Maximized . That will cause your form to be maximumized when it's opened. In addition, the FormBorderStyle can be set to FormBorderStyle. None to remove the border as well, for a more true maximized feeling, no borders added.


3 Answers

Extending a button control is no different then extending any class in C#. Simply do the following:

class ToggleButton : Button {
// Add your extra code
}
like image 104
Icemanind Avatar answered Oct 02 '22 14:10

Icemanind


You need to create a class that inherits the System.Windows.Forms.Button class and adds your property and associated behavior.

After compiling your project, your new class will appear in the toolbox.

like image 28
SLaks Avatar answered Oct 02 '22 12:10

SLaks


I know this is old and has been answered - however, Why make life difficult?

Each control has a Tag property which you can easily set to IsSwitchedOffable - or better English CanBeDisabled

Far easier.

like image 34
Dave Gordon Avatar answered Oct 02 '22 12:10

Dave Gordon