我创建了一个自定义控件,该控件扩展了GroupBox。此控件支持折叠和展开,我使用GroupBoxRenderer和ButtonRenderer使它看起来像一个典型的GroupBox,在角落有一个Button。我已经处理了所有适当的鼠标事件,这些事件有效地使“按钮”表现得像常规的Button。现在,我遇到了一个问题,其中GroupBox无法使用TabStop获得焦点。无论如何,我可以让我的可折叠GroupBox从TabStop接收焦点吗?
我希望可以使用How to set focus to a control after validation in .NET中的技巧在Enter事件中设置焦点,但是我没有想出一种确定何时应该真正获得焦点的好方法。我可能会想出一种方法来查找具有下一个最高和最低TabIndex(如果相同的TabIndex,则为ChildIndex)的兄弟姐妹,然后确定他们是否失去了焦点,但是如果我不明白的话,这似乎有点棘手,而且很有可能被打破完全正确。
注意:我最初确实创建了用户控件,但是由于各种原因,这不是我想要的,包括:
它不是包含按钮和组框的控件(恰好是这样的样子),它是组框
灵活性
后端代码和UI之间的耦合
动态布局
在需要工具箱支持并自定义整个控件的UI和布局的许多项目中共享
展开后的样子如下:
现在,当它折叠(并具有焦点)时:
最佳答案
CheckGroup =>这是自定义控件的一种方法,该控件继承自Control,并在OnPaint方法中使用GroupBoxRenderer和CheckBoxRenderer。它是通过更改CheckBoxRenderer绘制方式来模仿具有焦点的容器。此代码折叠CheckGroup并禁用任何子控件,但是您可以根据需要删除其中一个或两个。
using System;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
namespace CoolControls
{
[Designer("System.Windows.Forms.Design.ParentControlDesigner, System.Design", typeof(System.ComponentModel.Design.IDesigner))]
public partial class CheckGroup : Control
{
public event EventHandler CheckBoxGotFocus;
public event EventHandler CheckBoxLostFocus;
private int _CheckBoxSideLength;
private Rectangle _CheckBoxBorderRectangle;
private bool _Focused = false;
private bool _Checked;
private CheckBoxState _CheckedState = CheckBoxState.UncheckedNormal;
private int _ExpandedHeight;
[Category("Behavior")]
[Description("Get or set whether the checkbox is checked.")]
public bool Checked
{
get { return _Checked; }
set
{
SetCheckedState(value);
}
}
public CheckGroup()
{
InitializeComponent();
InitControl();
}
private void InitControl()
{
_CheckBoxSideLength = 15;
_Checked = true;
_Focused = false;
_CheckBoxBorderRectangle = new Rectangle(0, 0, _CheckBoxSideLength - 1, _CheckBoxSideLength - 1);
}
private void SetCheckedState(bool pToChecked)
{
_Checked = pToChecked;
if (_Checked)
{
_CheckedState = CheckBoxState.CheckedNormal;
this.Height = _ExpandedHeight;
}
else
{
_CheckedState = CheckBoxState.UncheckedNormal;
this.Height = _CheckBoxSideLength;
}
foreach (Control c in this.Controls)
{
c.Enabled = _Checked;
}
this.Invalidate();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = pe.Graphics;
GroupBoxRenderer.DrawGroupBox(g, ClientRectangle, " " + this.Text, this.Font, this.ForeColor, TextFormatFlags.Left, GroupBoxState.Normal);
CheckBoxRenderer.DrawCheckBox(g, ClientRectangle.Location, _CheckBoxBorderRectangle, "", null, TextFormatFlags.Left, _Focused, _CheckedState);
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged(e);
if (_Checked)
{
_ExpandedHeight = this.Size.Height;
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Location.Y <= _CheckBoxSideLength)
{
SetCheckedState(!_Checked);
}
}
protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
_Focused = true;
Invalidate();
CheckBoxGotFocus.Invoke(this, new EventArgs());
}
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
_Focused = false;
Invalidate();
CheckBoxLostFocus.Invoke(this, new EventArgs());
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Space)
{
SetCheckedState(!_Checked);
}
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
Invalidate();
}
}
}
关于c# - TabStop并专注于GroupBox,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17583155/