【发布时间】:2010-02-10 19:58:46
【问题描述】:
我在一个类 HUD.cs 中有以下方法,它具有辅助方法。下面的方法应该检查所有控件标签是否为“必需”并突出显示它找到的那些。
如果我从 UserControl 调用它并且要突出显示的控件不包含在 GroupBox 中,但是当它们是 TAG 时,它似乎没有遇到。想法?
方法如下-->
public static void HighlightRequiredFields(Control container, Graphics graphics, Boolean isVisible)
{
var borderColor = Color.FromArgb(173, 216, 230);
const ButtonBorderStyle borderStyle = ButtonBorderStyle.Solid;
const int borderWidth = 3;
Rectangle rect = default(Rectangle);
foreach (Control control in container.Controls)
{
if (control.Tag is string && control.Tag.ToString() == "required")
{
rect = control.Bounds;
rect.Inflate(3, 3);
if (isVisible && control.Text.Equals(string.Empty))
{
ControlPaint.DrawBorder(graphics, rect,
borderColor,
borderWidth,
borderStyle,
borderColor,
borderWidth,
borderStyle,
borderColor,
borderWidth,
borderStyle,
borderColor,
borderWidth,
borderStyle);
}
else
{
ControlPaint.DrawBorder(graphics, rect, container.BackColor, ButtonBorderStyle.None);
}
}
if (control.HasChildren)
{
foreach (Control ctrl in control.Controls)
{
HighlightRequiredFields(ctrl, graphics, isVisible);
}
}
}
}
【问题讨论】: