[英]Drawing Rectangle as TextBox Border
I'm using validation methods for my textboxes in a class named Validators
. 我在名为
Validators
的类中为文本框使用验证方法。 I'm trying also to draw a rectangle on the textbox which failed to validate. 我也在尝试在无法验证的文本框上绘制一个矩形。
Im using this code: 我正在使用此代码:
private void TextBoxStyle(TextBox textBox)
{
Graphics graphics = textBox.CreateGraphics();
Pen redPen = new Pen(Color.Red);
graphics.DrawRectangle(redPen, textBox.Location.X, textBox.Location.Y, textBox.Width, textBox.Height);
}
/// <summary>
/// Validates TextBoxes for string input.
/// </summary>
public bool ValidateTextBoxes(params TextBox[] textBoxes)
{
foreach (var textBox in textBoxes)
{
if (textBox.Text.Equals(""))
{
Graphics graphics = textBox.CreateGraphics();
Pen redPen = new Pen(Color.Red);
graphics.DrawRectangle(redPen, textBox.Location.X, textBox.Location.Y, textBox.Width, textBox.Height);
return false;
}
}
return true;
}
The problem is... the rectangles wont show. 问题是...矩形不会显示。 Am I doing something wrong with the code ?
我的代码有问题吗? If yes, help please.
如果是,请帮助。
A couple potential problems I see: 我看到一些潜在的问题:
Graphics
object for the text box but use the textbox's offset in the form to do the drawing. Graphics
对象,但是在窗体中使用文本框的偏移量进行绘制。 Net result: the rectangle is translated outside the visible area of the textbox. (0,0)
. (0,0)
。 While you're at it, check out the ErrorProvider
class. 在使用它时, 请检查
ErrorProvider
类。 It may just take care of your needs off-the-shelf. 它可能只是解决您的现成需求。
write a user control 编写用户控件
public partial class UserControl1 : UserControl
{
private string text;
private bool isvalid = true;
public string Text
{
get { return textBox.Text; }
set { textBox.Text = value; }
}
public bool isValid
{
set
{
isvalid = value;
this.Refresh();
}
}
TextBox textBox = new TextBox();
public UserControl1()
{
InitializeComponent();
this.Paint += new PaintEventHandler(UserControl1_Paint);
this.Resize += new EventHandler(UserControl1_Resize);
textBox.Multiline = true;
textBox.BorderStyle = BorderStyle.None;
this.Controls.Add(textBox);
}
private void UserControl1_Resize(object sender, EventArgs e)
{
textBox.Size = new Size(this.Width - 3, this.Height - 2);
textBox.Location = new Point(2, 1);
}
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
if (isvalid)
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Black, ButtonBorderStyle.Solid);
else
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle, Color.Red, ButtonBorderStyle.Solid);
}
}
update: just added the isvalid property 更新:刚刚添加了isvalid属性
you can put properties to show the border or not. 您可以放置属性以显示边框或不显示边框。 if the input is valid show normal border and if the control input is invalid show the red border.
如果输入有效,则显示正常边框,如果控制输入无效,则显示红色边框。
Anything drawn directly onto the TextBox will disappear as soon as the TextBox control is invalidated in some way. 一旦以某种方式使TextBox控件无效,直接绘制到TextBox上的所有内容都将消失。
A correct approach is to add a User Control to your project and add a TextBox on its canvas. 正确的方法是将用户控件添加到项目中,并在其画布上添加文本框。 Leave a little border around it.
在其周围留一点边框。
You can now simply color the background of the user control's canvas red when needed and it will look like a border drawn around the TextBox. 现在,您可以根据需要简单地将用户控件的画布背景涂成红色,看起来就像在TextBox周围绘制了一个边框。
You can add code directly to the user control to validate it whenever the text changes. 您可以将代码直接添加到用户控件中,以便在文本更改时进行验证。 That way, you only have to write code once and just add as many TextBoxes as you need to your forms or pages.
这样,您只需要编写一次代码,并向表单或页面添加所需数量的TextBox。
You shouldn't paint on a control simply from somewhere. 您不应该仅从某个地方在控件上绘画。 The build in painting will override it on the next occasion.
内置绘画将在下一次覆盖它。 The Control has a paint event where you should paint.
控件有一个绘画事件,您应该在其中绘画。 That will be used whenever painting is needed.
需要绘画时将使用该样式。
In your validate method you should just store the result of the validation somewhere so that it can be used in the paint event and call Invalidate() so that a repainting is enforced. 在您的validate方法中,您应该只将验证结果存储在某个位置,以便可以在paint事件中使用它,并调用Invalidate()以便执行重新绘制。
// You may use this
Label lblHighlight = new Label ();
Rectangle rc = new Rectangle(this.Left - 2, this.Top - 2, this.Width + 4, this.Bottom - this.Top + 4);
this.Parent.Controls.Add(lblHighlight);
lblHighlight.Bounds = rc;
lblHighlight.BackColor = "Red";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.