c# UI Automation label
See the question and my original answer on StackOverflowIt's possible to change the behavior of Winforms controls with regard to UI Automation. If you want a PictureBox to be seen as a button by UI Automation, and therefore be clickable using UI Automation tools, you can derive from PictureBox and override Automation/Accessible methods, something like this:
Before:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var pb = new PictureBox();
pb.Dock = DockStyle.Fill;
pb.Click += (s, e) => MessageBox.Show("hello world");
Controls.Add(pb);
}
}
This is how Inspect (from the SDK tools) sees it, as a "pane", with no specific action:
After:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var pb = new ButtonPictureBox();
pb.Dock = DockStyle.Fill;
pb.Click += (s, e) => MessageBox.Show("hello world");
Controls.Add(pb);
}
}
public class ButtonPictureBox : PictureBox
{
protected override AccessibleObject CreateAccessibilityInstance() => new Accessible(this);
private class Accessible : ButtonBase.ButtonBaseAccessibleObject
{
public Accessible(ButtonPictureBox control)
: base(control)
{
}
public new ButtonPictureBox Owner => (ButtonPictureBox)base.Owner;
public override AccessibleRole Role => AccessibleRole.PushButton;
public override AccessibleStates State => AccessibleStates.Default;
public override void DoDefaultAction() => Owner.OnClick(EventArgs.Empty);
}
}
Now, Inspect sees it as a "button" and the Invoke Pattern is available, so if you Invoke it, the Click method will be raised: