C# 将矩形绘制为文本框边框

C# 将矩形绘制为文本框边框,c#,winforms,C#,Winforms,我在名为Validators的类中为文本框使用验证方法。我还试图在文本框上绘制一个无法验证的矩形 我正在使用这个代码: private void TextBoxStyle(TextBox textBox) { Graphics graphics = textBox.CreateGraphics(); Pen redPen = new Pen(Color.Red); graphics.DrawRectangle(redPen, te

我在名为
Validators
的类中为文本框使用验证方法。我还试图在文本框上绘制一个无法验证的矩形

我正在使用这个代码:

    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;
    }
private void TextBoxStyle(TextBox TextBox)
{
Graphics Graphics=textBox.CreateGraphics();
钢笔红笔=新钢笔(颜色为红色);
graphics.DrawRectangle(红色笔,textBox.Location.X,textBox.Location.Y,textBox.Width,textBox.Height);
}
/// 
///验证字符串输入的文本框。
/// 
公共bool validatetextboxs(params TextBox[]textboxs)
{
foreach(文本框中的var textBox)
{
if(textBox.Text.Equals(“”)
{
Graphics Graphics=textBox.CreateGraphics();
钢笔红笔=新钢笔(颜色为红色);
graphics.DrawRectangle(红色笔,textBox.Location.X,textBox.Location.Y,textBox.Width,textBox.Height);
返回false;
}
}
返回true;
}

问题是。。。矩形不会显示。我的代码有问题吗?如果是,请提供帮助。

我发现了几个潜在问题:

  • 获取文本框的
    图形
    对象,但使用表单中文本框的偏移量进行绘制。最终结果:矩形在文本框的可见区域外平移。尝试使用位置
    (0,0)
  • 您可以绘制与文本框一样宽的矩形。最终结果:右边缘和下边缘将不可见。您应该从这些值中减去笔的宽度

  • 当您使用时,
    ErrorProvider
    类。它可能只会满足您现成的需求。

    我发现了几个潜在问题:

  • 获取文本框的
    图形
    对象,但使用表单中文本框的偏移量进行绘制。最终结果:矩形在文本框的可见区域外平移。尝试使用位置
    (0,0)
  • 您可以绘制与文本框一样宽的矩形。最终结果:右边缘和下边缘将不可见。您应该从这些值中减去笔的宽度
  • 当您使用时,
    ErrorProvider
    类。它可以满足您的需求。

    编写一个用户控件

      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);
    
            }
        }
    
    更新: 刚刚添加了isvalid属性

    您可以放置属性以显示边框或不显示边框。如果输入有效,则显示正常边框;如果控制输入无效,则显示红色边框

    编写用户控件

      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);
    
            }
        }
    
    更新: 刚刚添加了isvalid属性


    您可以放置属性以显示边框或不显示边框。如果输入有效,则显示正常边框;如果控制输入无效,则显示红色边框

    您不应该仅仅从某个地方在控件上绘制。内置绘画将在下次使用时覆盖它。控件有一个绘制事件,您应该在其中绘制。无论何时需要绘画,都会用到它


    在validate方法中,您应该将验证结果存储在某个位置,以便在绘制事件中使用,并调用Invalidate(),以便强制重新绘制。

    您不应该仅从某个位置在控件上绘制。内置绘画将在下次使用时覆盖它。控件有一个绘制事件,您应该在其中绘制。无论何时需要绘画,都会用到它


    在validate方法中,您应该将验证结果存储在某个地方,以便在绘制事件中使用,并调用Invalidate(),以便强制重新绘制。

    一旦TextBox控件以某种方式失效,直接绘制到TextBox上的任何内容都将消失

    正确的方法是向项目中添加用户控件,并在其画布上添加文本框。在它周围留一点边界

    现在,您只需在需要时将用户控件画布的背景涂成红色,它看起来就像在文本框周围绘制的边框


    您可以直接向用户控件添加代码,以便在文本更改时对其进行验证。这样,您只需编写一次代码,只需在表单或页面中添加所需数量的文本框。

    一旦文本框控件以某种方式失效,直接绘制到文本框上的任何内容都将消失

        // 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";
    
    正确的方法是向项目中添加用户控件,并在其画布上添加文本框。在它周围留一点边界

    现在,您只需在需要时将用户控件画布的背景涂成红色,它看起来就像在文本框周围绘制的边框


    您可以直接向用户控件添加代码,以便在文本更改时对其进行验证。这样,您只需编写一次代码,只需在表单或页面中添加所需数量的文本框。

    Oops,忘了提及。抱歉,问题已被编辑。请提及或标记.net VersionOps,忘记提及。抱歉,问题现在已编辑。请提及或标记.net版本,请放弃此操作,改用
    ErrorProvider
    。除非在
    Paint
    事件处理程序中进行绘制,否则绘制矩形将无法正常工作。这需要做很多工作。@CodyGray:我不认为textbox paint或OnPaint有效。将需要推翻wndproc,并根据边界油漆信息采取行动,并以此方式划定边界。明确地说,我同意普遍共识。不要浪费时间使用ErrorProvider。是的,请放弃使用它,改用
    ErrorProvider
    。除非在
    Paint
    事件处理程序中进行绘制,否则绘制矩形将无法正常工作。这需要做很多工作。@CodyGray:我不认为textbox paint或OnPaint有效。将需要推翻wndproc,并根据边界油漆信息采取行动,并以此方式划定边界。明确地说,我同意普遍共识。不要浪费时间使用ErrorProvider。控件订阅自己的事件有些不寻常。消息
        // 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";