Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

drawing a red border on or around a control in winforms

Posted on 2014-12-15
4
857 Views
Last Modified: 2014-12-15
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
Comment
Question by:r3nder
  • 2
4 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 40501125
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
 
LVL 1

Accepted Solution

by:
psdesignadmin earned 500 total points
ID: 40501132
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
 
LVL 6

Author Closing Comment

by:r3nder
ID: 40501155
Thanks Tim
0
 
LVL 1

Expert Comment

by:psdesignadmin
ID: 40501162
Glad it helps!

~Tim
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question