Ref*_*din 5 .net c# controls .net-3.5 winforms
当用户单击我的验证按钮(在我的C#,WinForm,.net 3.5应用程序中)时,如果它是空的,我想在某个控件周围绘制一个边框.说一个名为tbxLastName的文本框我以为我需要做这样的事情 - >
ControlPaint.DrawBorder(Graphics.FromHwnd(this.Handle),
tbxLastName.ClientRectangle, Color.Firebrick, ButtonBorderStyle.Solid);
Run Code Online (Sandbox Code Playgroud)
不幸的是,我不知道要为图形对象放什么,因为我没有做什么.
我遇到的所有示例,MSDN - HERE,都在Paint事件中包含此代码.像这样 - >
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle,
Color.DarkBlue, ButtonBorderStyle.Solid);
}
Run Code Online (Sandbox Code Playgroud)
但是,我只希望在某些条件满足时出现边框,这是由Button_Click启动的
许多建议建议使用容器对象来保存文本框并将其命名为Paint_Event.我做了这个,出现了一个方框但不在控件周围.它出现在Container Control的左上角.这是我在做什么 - >
private void grpImmunizationCntrl_Paint(object sender, PaintEventArgs e)
{
if (lkuNOImmunizationReason.Text.Equals(string.Empty)
{
ControlPaint.DrawBorder(
e.Graphics, lkuNOImmunizationReason.ClientRectangle,
Color.Firebrick, ButtonBorderStyle.Solid);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑
这就是我提出的将这些建议与对我有用的建议相结合的方法.
public static void HighlightRequiredFields(Control container, Graphics graphics, Boolean isVisible)
{
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, Color.FromArgb(173,216,230), ButtonBorderStyle.Solid);
}
else
{
ControlPaint.DrawBorder(graphics, rect, container.BackColor, ButtonBorderStyle.None);
}
}
if (control.HasChildren)
{
foreach (Control ctrl in control.Controls)
{
HighlightRequiredFields(ctrl, graphics, isVisible);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我从Paint_Event我需要的任何容器中调用它.
您可以使用表单中的操作列表字段并添加或删除自定义绘图:
// field
List<Action<Graphics>> drawings = new List<Action<Graphics>>();
// on click event:
drawings.Add(delegate(Graphics g) {
var rect = tbxLastName.Bounds;
rect.Inflate(1, 1); // make rectange a bit larger than textbox
ControlPaint.DrawBorder(g, rect,
Color.DarkBlue, ButtonBorderStyle.Solid);
});
// make sure you added only once or clear before
panel1.Refresh(); // refresh panel to force painting
// Paint method:
foreach (var draw in drawings) {
draw(e.Graphics);
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以添加多个边框
我刚刚在这个帖子的帮助下用 VB.Net 做了类似的事情。Panel我的每组控件周围都有一个容器,在OnPaint的处理程序中Panel,我循环遍历 中的所有子控件Panel,并在它们周围绘制边框(如果它们具有tag="required". 我只在“编辑”或“新建”上显示边框,因此我创建了fVisible参数来打开和关闭这些边框。当我想关闭它时,我会调用Panel.Refresh()它来触发OnPaint事件。这样,我所要做的就是在设计时在所需的控件上设置标签,并为容器面板添加一个处理程序,这一切都是动态工作的。
这是我从面板的所有事件处理程序调用的共享函数OnPaint。(抱歉,我知道您使用的是 C#,这是 VB,但它非常基础。)
Friend Sub HighlightRequiredFields(ByVal pnlContainer As Panel, ByVal gr As Graphics, ByVal fVisible As Boolean)
Dim rect As Rectangle
For Each oControl As Control In pnlContainer.Controls
If TypeOf oControl.Tag Is String AndAlso oControl.Tag.ToString = "required" Then
rect = oControl.Bounds
rect.Inflate(1, 1)
If fVisible Then
ControlPaint.DrawBorder(gr, rect, Color.Red, ButtonBorderStyle.Solid)
Else
ControlPaint.DrawBorder(gr, rect, pnlContainer.BackColor, ButtonBorderStyle.None)
End If
End If
If TypeOf oControl Is Panel Then HighlightRequiredFields(DirectCast(oControl, Panel), gr, fVisible)
Next
End Sub
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11694 次 |
| 最近记录: |