Lightweight WinForms component that makes implementing handlers for the built-in form buttons a breeze!
Of course, you can use it for whatever you want to - it is compatible with anything that accepts IButtonControl
interfaces.
Visual Studio
-
Right-Click on a
.csproj
or.sln
file in the solution explorer, then click the button. -
Switch to the Browse tab and search for 'Virtual Button'
-
Click on the Install button next to the package description:
The code is open-sourced under the GPLv3 license.
You can add virtual buttons using the designer, or entirely programmatically.
VButton
appears at the bottom alongside other components, such as NotifyIcon
, Timer
, BindingSource
, etc.
- Once you have the NuGet package installed, open the designer toolbox (You may have to recompile the project for it to appear.) and add the
VButton
component somewhere. - In the properties window, create a handler for the
Click
event, and put whatever code you want to execute when the associated key is pressed in the handler. - Re-assign the button property at least once before attempting to use it.
Common places to do this are the form's constructor orForm.Load
event, for example:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Even though we set this in the designer, we have
// to set it again manually for it to work correctly:
this.CancelButton = vButton1;
}
}
You can create VButtons with ad-hoc delegates:
this.CancelButton = new VButton(delegate{
// Do something
});
Or with any event handler compatible with HandledEventHandler
:
private void HandlerOfHandledEvents(object sender, HandledEventArgs e)
{
// ...
}
private void Form1_Load(object sender, EventArgs e)
{
vButton1.Click += HandlerOfHandledEvents!;
}