Skip to content

Instantly share code, notes, and snippets.

@riyadparvez
Last active December 10, 2015 10:08
Show Gist options
  • Save riyadparvez/4419551 to your computer and use it in GitHub Desktop.
Save riyadparvez/4419551 to your computer and use it in GitHub Desktop.
This panel class gets focus. On the other hand default panel control tries to get away from focus. This code is taken from Hans Passant from stackoverlfow
//This panel will get focus as the original .NET panel always tries to get away from focus
public class FocusablePanel : Panel
{
public FocusablePanel()
{
this.SetStyle(ControlStyles.Selectable, true);
this.TabStop = true;
}
protected override void OnMouseDown(MouseEventArgs e)
{
this.Focus();
base.OnMouseDown(e);
}
protected override bool IsInputKey(Keys keyData)
{
if (keyData == Keys.Up || keyData == Keys.Down) return true;
if (keyData == Keys.Left || keyData == Keys.Right) return true;
return base.IsInputKey(keyData);
}
protected override void OnEnter(EventArgs e)
{
this.Invalidate();
base.OnEnter(e);
}
protected override void OnLeave(EventArgs e)
{
this.Invalidate();
base.OnLeave(e);
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
if (this.Focused)
{
var rc = this.ClientRectangle;
rc.Inflate(-2, -2);
ControlPaint.DrawFocusRectangle(pe.Graphics, rc);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment