• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1103
  • Last Modified:

drawing a red border on or around a control in winforms

I want to highlight the controls that need to be filled in when a user does not enter information - for example a red border - how can I achieve this in a win form?

 string msg = "";
                    if (comBoxToLocations.SelectedValue.ToString() != "-1" && comBoxToLocations.SelectedValue.ToString() != "3")
                    {
                        ToID = Convert.ToInt32(comBoxToLocations.SelectedValue.ToString());
                    }
                    else
                    {
                        if (comBoxToLocations.SelectedValue.ToString() == "-1")
                        {
                            msg += "You must select a location to send to\r\n";
                        }
                        if (comBoxToLocations.SelectedValue.ToString() == "3")
                        {
                            msg += "Location cannot be Unknown\r\n";
                        }
                    }
                    if (cmboCarrier.SelectedValue.ToString() != "-1")
                    {
                        carrierID = Convert.ToInt32(cmboCarrier.SelectedValue.ToString());
                    }
                    else
                    {
                        msg += "You must select a carrier\r\n";
                    }
                    if (string.IsNullOrEmpty(trackingNumber))
                    {
                        msg += "You must enter a tracking number\r\n";
                    }
                    if (expecteddate == today)
                    {
                        msg += "Your Expected arrival date is too short\r\n";
                    }
                    if (msg != "")
                    {
                        MessageBox.Show(msg);
                        return;
                    }

Open in new window

0
r3nder
Asked:
r3nder
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Have you considered using the standard ErrorProvider control?  Very easy to use...
http://msdn.microsoft.com/en-us/library/aa983609(v=vs.71).aspx

You can set an initial error with:

    errorProvider1.SetError(comboBox1, "Please select a value!");

And clear it with:

    errorProvider1.SetError(comboBox1, "");
0
 
psdesignadminCommented:
Most of the Winform controls have a property BorderStyle that allows for a border to be shown around that control. But the look and feel of that border cannot be changed. And there are controls (button, groupbox, etc.) that do not have this property. So there is no direct way of adding a Border around these controls

The following magic lines allow us to add a border around a control:

public class MyGroupBox : GroupBox
{
   protected override void OnPaint(PaintEventArgs e)
   {
     base.OnPaint(e);
     ControlPaint.DrawBorder(e.Graphics, ClientRectangle,
                                  Color.Red, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Red, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Red, BORDER_SIZE, ButtonBorderStyle.Inset,
                                  Color.Red, BORDER_SIZE, ButtonBorderStyle.Inset);
   } 
}

Open in new window


The ControlPaint class allows to add a border around a control. The above call can be invoked in a Paint event handler of a control.

~Tim
0
 
r3nderAuthor Commented:
Thanks Tim
0
 
psdesignadminCommented:
Glad it helps!

~Tim
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now