472,780 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 28338
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: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.