By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,312 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,312 IT Pros & Developers. It's quick & easy.

Changing colour of Groupbox Border

P: n/a
Hi All

Is there a way to change the colour of a Groupbox border in VB.net 2005

I want to change it to white

Can it be done in the onpaint event?

Regards
Steve

Jun 14 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hello Steve

According to your description, you need change the color of Groupbox border
from Black to White. (In VB.net 2005 ). Please don't hesitate to correct me
if I misunderstand anything.

This is a common issue. Because there is no public borderColor propery for
Groupbox, we cannot modify it by default. However, capturing the onpaint
event is the right direction. You could create customer class which
inherited GroupBox and overid its OnPaint event to draw the border the way
you like.

Zhi-Xin Ye provide a good sample for this issue. I also copy the content
as following for your convenience.
http://forums.microsoft.com/MSDN/Sho...56465&SiteID=1
[Changing border color of GroupBox]

Public Class myGroupBox
Inherits GroupBox

Private _borderColor As Color

Public Sub New()
MyBase.New()
Me._borderColor = Color.Black
End Sub

Public Property BorderColor() As Color
Get
Return Me._borderColor
End Get
Set(ByVal value As Color)
Me._borderColor = value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = e.ClipRectangle
borderRect.Y = (borderRect.Y _
+ (tSize.Height / 2))
borderRect.Height = (borderRect.Height _
- (tSize.Height / 2))
ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor,
ButtonBorderStyle.Solid)
Dim textRect As Rectangle = e.ClipRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New
SolidBrush(Me.ForeColor), textRect)
End Sub
End Class

1) Open the Solution Explorer, Add a new Class and name the class as
"myGroupBox"
2) Put the above code in this class, rebuild your project,
3) Then, you will see a "myGroupBox" control on your toolbox, it has a
"BorderColor" property on the Misc group in the property view.

Hope this helps. If you still have anything unclear, please feel free to
let me know. I'm glad to assist you.
Have a great day,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 14 '07 #2

P: n/a
WenYuan

Thank you for the prompt reply

Exactly what I needed

Regards
Steve

"WenYuan Wang [MSFT]" <v-******@online.microsoft.comwrote in message
news:y8**************@TK2MSFTNGHUB02.phx.gbl...
Hello Steve

According to your description, you need change the color of Groupbox
border
from Black to White. (In VB.net 2005 ). Please don't hesitate to correct
me
if I misunderstand anything.

This is a common issue. Because there is no public borderColor propery for
Groupbox, we cannot modify it by default. However, capturing the onpaint
event is the right direction. You could create customer class which
inherited GroupBox and overid its OnPaint event to draw the border the way
you like.

Zhi-Xin Ye provide a good sample for this issue. I also copy the content
as following for your convenience.
http://forums.microsoft.com/MSDN/Sho...56465&SiteID=1
[Changing border color of GroupBox]

Public Class myGroupBox
Inherits GroupBox

Private _borderColor As Color

Public Sub New()
MyBase.New()
Me._borderColor = Color.Black
End Sub

Public Property BorderColor() As Color
Get
Return Me._borderColor
End Get
Set(ByVal value As Color)
Me._borderColor = value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = e.ClipRectangle
borderRect.Y = (borderRect.Y _
+ (tSize.Height / 2))
borderRect.Height = (borderRect.Height _
- (tSize.Height / 2))
ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor,
ButtonBorderStyle.Solid)
Dim textRect As Rectangle = e.ClipRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New
SolidBrush(Me.ForeColor), textRect)
End Sub
End Class

1) Open the Solution Explorer, Add a new Class and name the class as
"myGroupBox"
2) Put the above code in this class, rebuild your project,
3) Then, you will see a "myGroupBox" control on your toolbox, it has a
"BorderColor" property on the Misc group in the property view.

Hope this helps. If you still have anything unclear, please feel free to
let me know. I'm glad to assist you.
Have a great day,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jun 14 '07 #3

P: n/a

You are welcome. Steve. :)

Have a nice weekend.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 15 '07 #4

P: n/a
WenYuan

I am having problems with the supplied code for mygroupbox onpaint

If mygroupbox is on a Tab control tab then when I click on another control
on the form, the groupbox ends up with numerous lines through it and the
only way to get rid of them is to do a refresh
It looks like it is redrawing in the wrong location and a different size
Refresh fixes it
How do you get a user control to behave like the originals, i.e refresh
properly?

The effect doesn't happen when the control is just on a form, only when on a
Tab control
Regards
Steve

"WenYuan Wang [MSFT]" <v-******@online.microsoft.comwrote in message
news:y8**************@TK2MSFTNGHUB02.phx.gbl...
Hello Steve

According to your description, you need change the color of Groupbox
border
from Black to White. (In VB.net 2005 ). Please don't hesitate to correct
me
if I misunderstand anything.

This is a common issue. Because there is no public borderColor propery for
Groupbox, we cannot modify it by default. However, capturing the onpaint
event is the right direction. You could create customer class which
inherited GroupBox and overid its OnPaint event to draw the border the way
you like.

Zhi-Xin Ye provide a good sample for this issue. I also copy the content
as following for your convenience.
http://forums.microsoft.com/MSDN/Sho...56465&SiteID=1
[Changing border color of GroupBox]

Public Class myGroupBox
Inherits GroupBox

Private _borderColor As Color

Public Sub New()
MyBase.New()
Me._borderColor = Color.Black
End Sub

Public Property BorderColor() As Color
Get
Return Me._borderColor
End Get
Set(ByVal value As Color)
Me._borderColor = value
End Set
End Property

Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
Dim borderRect As Rectangle = e.ClipRectangle
borderRect.Y = (borderRect.Y _
+ (tSize.Height / 2))
borderRect.Height = (borderRect.Height _
- (tSize.Height / 2))
ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor,
ButtonBorderStyle.Solid)
Dim textRect As Rectangle = e.ClipRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New
SolidBrush(Me.ForeColor), textRect)
End Sub
End Class

1) Open the Solution Explorer, Add a new Class and name the class as
"myGroupBox"
2) Put the above code in this class, rebuild your project,
3) Then, you will see a "myGroupBox" control on your toolbox, it has a
"BorderColor" property on the Misc group in the property view.

Hope this helps. If you still have anything unclear, please feel free to
let me know. I'm glad to assist you.
Have a great day,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jun 19 '07 #5

P: n/a
Hello Steve,

When does the issue occur? (On designer mode in Visual Studio 2005 IDE or
the application built by VS 2005).

I have test the code on my VS 2005 sp1 machine.
I notice mygroupbox ends up with numerous lines when I drop some control
over it in design mode of VS 2005 IDE. However, after I build the project,
this issue doesn't happen in the exe file. It seems like the issue only
occurs in VS 2005 IDE in design mode.

Did I misunderstand anything here? Please kindly let me know whether this
is the issue you are looking for? Thus, I could do further analyze. Thanks.

Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 19 '07 #6

P: n/a
Hi WenYuan

It happens to me in the IDE and in the application exe file

It only happens when the Groupbox is on a Tab page, not when it is on a form

Regards
Steve
"WenYuan Wang [MSFT]" <v-******@online.microsoft.comwrote in message
news:Ik**************@TK2MSFTNGHUB02.phx.gbl...
Hello Steve,

When does the issue occur? (On designer mode in Visual Studio 2005 IDE or
the application built by VS 2005).

I have test the code on my VS 2005 sp1 machine.
I notice mygroupbox ends up with numerous lines when I drop some control
over it in design mode of VS 2005 IDE. However, after I build the project,
this issue doesn't happen in the exe file. It seems like the issue only
occurs in VS 2005 IDE in design mode.

Did I misunderstand anything here? Please kindly let me know whether this
is the issue you are looking for? Thus, I could do further analyze.
Thanks.

Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Jun 19 '07 #7

P: n/a
Dear Steve

I'm sorry to say I still cannot reproduce the issue on my side.
The issue only occurs to me on Design mode in the IDE. In exe file, the
issue has gone away whether I put mygroupbox in Tab Page or not.

Is it possible for you to send me a simple project which I could reproduce
the issue on my side?
Thus, I could do further analyze and make sure we are talking about the
same problem.

My alias is v-******@microsoft.com. I'm waiting for your reply.
Have a great day.
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 20 '07 #8

P: n/a
Hello Steve,
Thanks for your mail.
I also paste the reply in newsgroup. Thus, more people could get benefit if
meet the same issue.

I repro the issue and figure out this is a code defect.
ColorGroupBox's onpaint event draw border and text in the wrong panel.

Please replace OnPaint event as below. This change could get rid of the
issue.
************************************************** **********
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim tSize As Size = TextRenderer.MeasureText(Me.Text, Me.Font)
'Dim borderRect As Rectangle = e.ClipRectangle
Dim borderRect As Rectangle = Me.ClientRectangle

borderRect.Y = (borderRect.Y + (tSize.Height / 2))
borderRect.Height = (borderRect.Height - (tSize.Height / 2))
ControlPaint.DrawBorder(e.Graphics, borderRect, Me._borderColor,
ButtonBorderStyle.Solid)

'Dim textRect As Rectangle = e.ClipRectangle
Dim textRect As Rectangle = Me.ClientRectangle
textRect.X = (textRect.X + 6)
textRect.Width = tSize.Width
textRect.Height = tSize.Height
e.Graphics.FillRectangle(New SolidBrush(Me.BackColor), textRect)
e.Graphics.DrawString(Me.Text, Me.Font, New SolidBrush(Color.Blue),
textRect)
End Sub
************************************************** **********

Hope this helps. Please let me know whether it works for you and I will
follow up.
Have a great day,
Sincerley,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.