Instead of the standard Button
, use the following when you need that behavior
public class NonSelectableButton : Button
{
public NonSelectableButton()
{
SetStyle(ControlStyles.Selectable, false);
}
}
EDIT:
Here is a little test proving that it's working
using System;
using System.Linq;
using System.Windows.Forms;
namespace Samples
{
public class NonSelectableButton : Button
{
public NonSelectableButton()
{
SetStyle(ControlStyles.Selectable, false);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form();
Control[] controls = { new TextBox(), new TextBox(), };
Button[] buttons = { new NonSelectableButton { Text = "Prev" }, new NonSelectableButton { Text = "Next" }, };
foreach (var button in buttons)
button.Click += (sender, e) => MessageBox.Show("Button " + ((Button)sender).Text + " clicked!");
int y = 0;
foreach (var item in controls.Concat(buttons))
{
item.Left = 8;
item.Top = y += 8;
form.Controls.Add(item);
y = item.Bottom;
}
Application.Run(form);
}
}
}
EDIT2:: To apply the solution, you need to do the following:
(1) Add a new code file to your project, call it NonSelectableButton.cs with the following content
using System;
using System.Linq;
using System.Windows.Forms;
namespace YourNamespace
{
public class NonSelectableButton : Button
{
public NonSelectableButton()
{
SetStyle(ControlStyles.Selectable, false);
}
}
}
(2) Compile the project
(3) Now the new button will appear in the control toolbox (at the top) and you can drag it on a form instead of a standard button.