绘制边框上Button_Click一个控制 [英] Draw border around a Control on Button_Click
问题描述
当用户点击我的验证按钮(的在我的C#,WinForm的,.NET 3.5的应用程序的)我想提请围绕一定的控制边界,如果它是空的。假设一个名为tbxLastName我想我需要做的是这样的文本 - >
When a user clicks my Validate Button (in my C#, WinForm, .net 3.5 app) I would like to draw a border around a certain control if it is empty. Say a textbox that is named tbxLastName I thought I needed to do something like this -->
ControlPaint.DrawBorder(Graphics.FromHwnd(this.Handle),
tbxLastName.ClientRectangle, Color.Firebrick, ButtonBorderStyle.Solid);
不幸的是,我不知道该怎么放的图形对象为我有什么什么都不做。
Unfortunately, I have no idea what to put for the Graphics Object as what I have does NOTHING.
所有我接触过的例子, MSDN - 这里 ,有这样的code在Paint事件。像这样 - >
All of the examples I have come across, MSDN - HERE, have this code in a Paint Event. Like so -->
private void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.panel1.ClientRectangle,
Color.DarkBlue, ButtonBorderStyle.Solid);
}
但是,我只希望有边界出现,而某些条件得到满足它是由一个Button_Click
I, however, only want to have the border appear while certain conditions are meet which is kicked off by a Button_Click
这么多的建议,建议使用一个容器对象来保存文本框,并调用它的Paint_Event。我这样做,并会出现一个框,但不是控制各地。它出现在容器控件的左上角。下面是我在做什么 - >
So many of the suggestions suggest using a container object to hold the textbox and call it's Paint_Event. I did this and a box appears but NOT around the control. It appears in the Top Left corner of the Container Control. Here is what I am doing -->
private void grpImmunizationCntrl_Paint(object sender, PaintEventArgs e)
{
if (lkuNOImmunizationReason.Text.Equals(string.Empty)
{
ControlPaint.DrawBorder(
e.Graphics, lkuNOImmunizationReason.ClientRectangle,
Color.Firebrick, ButtonBorderStyle.Solid);
}
}
修改
EDIT
这就是我来到了这里合并的建议与什么为我工作。
This is what I came up with combining suggestions here with what worked for me.
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);
}
}
}
}
我把这个从 Paint_Event
任何Container我需要。
I call this from the Paint_Event
of any Container I need to.
推荐答案
我只是做了一些与VB.Net相似,从这个线程的帮助。我有一个面板
围绕每一套我的控制容器,在的OnPaint
处理程序面板
,我经历了所有的孩子回路控制在面板
和绘制边框周围,如果他们有标记= 必需的
。我只显示在编辑或新的边界,所以我创建了 fVisible
参数来打开和关闭它们。当我想火这一关,我称之为 Panel.Refresh()
所以它使用了的OnPaint
事件。这样,所有我需要做的是设置在设计时所需要的控制标签,并添加一个处理程序容器面板,这一切工作动态。
I just did something similar with VB.Net, with help from this thread. I have a Panel
container around each set of my controls, and in the OnPaint
handler for the Panel
, I loop through all the children controls in the Panel
and draw borders around them if they have the tag="required"
. I only display the borders on Edit or New so I created the fVisible
parameter to toggle these on and off. When I want to fire this off, I call Panel.Refresh()
so it fires the OnPaint
event. This way all I have to do is set the tags on the required controls at design time, and add a handler for the container Panels, and it all works dynamically.
下面是共享的功能,我从我所有的小组的OnPaint
事件处理程序调用。 (对不起,我知道你正在使用C#,这是VB,但它是pretty的基础。)
Here's the shared function I'm calling from all of my Panel's OnPaint
Event Handlers. (Sorry I know you're using C# and this is VB but it's pretty basic.)
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
这篇关于绘制边框上Button_Click一个控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!