简体   繁体   中英

Drawing Rectangle as TextBox Border

I'm using validation methods for my textboxes in a class named 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:

  1. You get the Graphics object for the text box but use the textbox's offset in the form to do the drawing. Net result: the rectangle is translated outside the visible area of the textbox. Try using the location (0,0) .
  2. You draw the rectangle as wide as the textbox. Net result: right and bottom edges won't be visible. You should subtract the width of the pen from these values.

While you're at it, check out the ErrorProvider class. 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

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.

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.

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.

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.

    // 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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM