我创建了一个扩展GroupBox的自定义控件。这个控件支持折叠和扩展,我使用GroupBoxRenderer和ButtonRenderer使它看起来像一个典型的GroupBox,在角落里有一个Button。我已经处理了所有适当的鼠标事件,这些事件有效地使“按钮”表现得像一个普通的按钮。现在我遇到了GroupBox没有使用TabStop获得焦点的问题。无论如何,我可以让我的Collapsable GroupBox从TabStop获得焦点吗?
我希望我可以使用来自How to set focus to a control after validation in .NET的技巧来设置Enter事件中的焦点,但我还没有找到一个很好的方法来确定它何时应该实现焦点。我可能会设法找到具有下一个最高和最低TabIndex(或ChildIndex,如果相同的TabIndex)的兄弟姐妹,然后确定他们是否失去焦点但这似乎有点hacky和很高的机会,如果我没有得到破解它完全正确。
注意:我最初创建了用户控件,但由于各种原因,这不是我想要的,包括:
以下是展开时的样子:
现在它已经崩溃(并且有焦点):
答案 0 :(得分:0)
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();
}
}
}