How can I change Pannel's Border color?

M

Maxwell2006

Hi,

We are using panels and SplitContainers in my WinForms application and we
are looking for some simple way to change the border color from Black to
Blue.

I know that we can override the Paint event and have our own painting
methods, but I expect there should be much easier way to change the border
color to Blue or some other RGB color?

Any help would be appreciated,
Ali
 
J

Jeff Gaines

Hi,

We are using panels and SplitContainers in my WinForms application and we
are looking for some simple way to change the border color from Black to
Blue.

I know that we can override the Paint event and have our own painting
methods, but I expect there should be much easier way to change the border
color to Blue or some other RGB color?

Any help would be appreciated,
Ali

I wanted something similar but couldn't find a way. I put a second panel
inside the first then on layout/resize I made the inner panel 2 pixels
smaller all round and located it at 1,1. You then get a fake border and
can adjust the size to suit.
 
L

Linda Liu [MSFT]

Hi,

Could you please tell me whose's border color you'd like to change, a Panel
or a SplitContainer?

If it is a Panel, I am sorry to say I don't think there's an easy way to
change its border color. Panel class doesn't provide such a property or
method to change its border color. We have to paint the border with the
desired color by ourselves. We have two options to do this.

One option is to handle the Paint event of the Panel control. The other
option is to derive a new control from Panel class and override the OnPaint
method in the new control. Both the two options aren't difficult. The
following is two samples for the two options.

// sample for option 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Panel1.BorderStyle = BorderStyle.None;
this.panel1.Paint += new PaintEventHandler(panel1_Paint);
}

void panel1_Paint(object sender, PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
Color.Blue, ButtonBorderStyle.Solid);
}
}

// sample for option 2
class MyPanel:panel
{
public MyPanel()
{
this.BorderStyle = BorderStyle.None;
}
// when the panel is resized, redraw the panel
protected override void OnResize(EventArgs eventargs)
{
base.OnResize(eventargs);
this.Refresh();
}
protected override void OnPaint(PaintEventArgs e)
{
ControlPaint.DrawBorder(e.Graphics, this.ClientRectangle,
Color.Blue, ButtonBorderStyle.Solid);
base.OnPaint(e);
}
}

As for SplitContainer, it doesn't expose any property or method for us to
change its border color either. What's more, handle its Paint event or
override the OnPaint method don't solve the problem, because the
'PaintEventArgs' event args returns a graphics object which only represents
the area of the middle 'splitter'.

In fact, SplitContainer consists of two panels and a splitter. We could
made a custom 'SplitContainer' by ourselves. That is, add a standard Panel
control onto the form and set its BorderStyle property to None. Add a
MyPanel control into the standard Panel and dock it to its parent's left
side. Add a Splitter into the standard Panel. Add a second MyPanel control
into the standard Panel and set its Dock property to Fill. That's all. When
you run the problem, you should see a 'SplitContainer' control with a blue
border.

Hope this helps.
If you have anything unclear, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top