473,395 Members | 1,987 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Changing colour of Groupbox Border

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
8 28496
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
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

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dj Frenzy | last post by:
Hi, I know how to use javascript to change a background image to another background image, and how to change a background colour to another background colour. Is there a way to change an image to a...
4
by: Vincent Finn | last post by:
Can anyone tell me what the difference is? Where I might need one over the other? I know in VB there was a panel and no groupBox in MFC there was a groupBox and no panel But in C# they are...
2
by: Dan Neely | last post by:
My dialog has groupboxes with slow to redraw controls, to improve the appearance I want to doublebuffer it. While I can use SetStyle() in the Dailogs constructor the setting change doesn't get...
4
by: Peteroid | last post by:
In a standard GroupBox, how do I make the visible 'border line' not show (it looks like an etched in line about 5 pixels shy of each border of the group box)? Thanks in advance!
0
by: H-S | last post by:
Can anyone help. I have a C# app developped in C# Express. I have a form which contains several groupboxs, with text labels. I also have a standard "label" on the form. I want to set the label...
6
by: Dan Soendergaard | last post by:
Hi, I'm writing my own control and I would like it to integrate nicely into the rest of my application, which uses the standard controls primarely. I therefore wanted my control to use the same...
4
by: Johny | last post by:
I use PIL to write some text to a picture.The text must be seen wery clearly. I write the text to different pictures but to the same position. As pictures maybe different, colour, in the...
23
by: cmcdermo | last post by:
I want to change the colour of a border around an image when a user clicks on the image. Not sure how to go about this, i have tried a few things but it doens't seem to work. here is my image....
4
by: Arun | last post by:
I came across a message sometime back in 2002. Here's a link:...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.