0

I was just wondering if its was easily possible to set the margins of a textbox a particular color? Im using winforms and in my validating event handlers i have a series of error providers, which if return false I want to set just the margins red and if successful green. is this possible with out having a any controls hidden behind ? I know how to set the foreground and panel colour but it just seems so sloppy to have to have this all hidden behind it. this is my validating event handler.

       private void txt_LndLine_Validating(object sender, CancelEventArgs e)
        {

            if (utility.isNum,(txt_LndLine.Text))
            {
                epLandline.SetError(txt_LndLine, "Please use unknown Entity!!!");

                return;
            }

            else 
            {
                epLandline.Clear();
                _IsValid = true;

            } 
        }

Just a query, as the event hadnler works fine just wouldn't mind a smarter way of presenting the errors rather than the icon

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
Chaos
  • 633
  • 1
  • 6
  • 14
  • 1
    There is a good answer for changing border color in this SO, slightly different, but I think nicer. https://stackoverflow.com/questions/9768938/change-the-bordercolor-of-the-textbox. See the answer by Reza Aghaei –  Aug 17 '17 at 23:05

2 Answers2

0

May be the answer in comments is a good one (@RudyTheHunter).

I have a simple way of doing.

Put a panel control right below the text box and change the border color of the panel.

I tried it and looks as shown in below image.

Code:

            if (utility.isNum,(txt_LndLine.Text))
            {
                epLandline.SetError(txt_LndLine, "Please use unknown Entity!!!");
              //PANEL COLOR
              this.panel1.BackColor = System.Drawing.Color.Green;
              this.panel1.Padding = new System.Windows.Forms.Padding(5);

                return;
            }

            else 
            {
                epLandline.Clear();

               //PANEL COLOR
                this.panel1.BackColor = System.Drawing.Color.Red;
                this.panel1.Padding = new System.Windows.Forms.Padding(5);

                _IsValid = true;    
            } 

enter image description here

Pavan Chandaka
  • 11,671
  • 5
  • 26
  • 34
0

Here is an alternative solution, which won't add any extra controls. It paints the border onto the TextBoxes' Parent.

Set it up by hooking the routine into the Paint event of your TextBox's Parent once, maybe like this:

textBox1.Parent.Paint += DrawMargins;

Now you can set the Tag to hold the Brush you want to use:

textBox1.Tag = Brushes.Red;
textBox2.Tag = Brushes.Green;

After changing the Brush you need to trigger the routine, by Invalidating the Parent:

textBox1.Parent.Invalidate();

To take one TextBox out of the painting reset the Tag to null:

textBox1.Tag = null;

You can also un-hook the whole event of course:

textBox1.Parent.Paint -= DrawMargins;

Here is the drawing method:

private void DrawMargins(object sender, PaintEventArgs e)
{
    Control parent = sender as Control;
    foreach ( Control ctl in parent.Controls)
    { 
        SolidBrush brush = ctl.Tag as SolidBrush;
        if (brush == null) continue;
        e.Graphics.FillRectangle(brush, ctl.Left - ctl.Margin.Left,
            ctl.Top - ctl.Margin.Top,
            ctl.Width + ctl.Margin.Horizontal,
            ctl.Height + ctl.Margin.Vertical);
    }
}

Note that this will work for any control which has a SolidBrush in the Tag and is a child of the same parent. If some controls as nested, say in a Panel or a GroupBox, I guees you should replace the loop over the parent.Controls collection by a List<Control> of the participating controls..

enter image description here

I have enlarged the left margin of the 1st TextBox, as you can see..

TaW
  • 53,122
  • 8
  • 69
  • 111