473,883 Members | 1,591 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Label control border color

Is there a quick and easy way to change the color of a label controls border
from the default black to white?

Thank you,
John
Nov 20 '05 #1
31 7270
Hi,

If you want to change the border color you have to override the
WM_NCPAINT message.
Public Class BorderLabel
Inherits Label

Declare Function GetWindowDC Lib "user32" Alias "GetWindowD C" (ByVal
hwnd As IntPtr) _
As IntPtr
Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal
hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer
Protected Overrides Sub WndProc(ByRef m As
System.Windows. Forms.Message)
Const WM_NCPAINT = &H85

If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White , 3)
Dim hdc As IntPtr = GetWindowDC(m.H Wnd)
Dim g As Graphics = Graphics.FromHd c(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle (penBorder, rDraw)

ReleaseDC(Me.Ha ndle, hdc)
Else
MyBase.WndProc( m)

End If

End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub

Public Sub New()
Me.BorderStyle = BorderStyle.Fix edSingle
End Sub
End Class

Ken
---------------------------

"jcrouse" <me> wrote in message
news:eC******** *****@TK2MSFTNG P10.phx.gbl:
Is there a quick and easy way to change the color of a label controls
border
from the default black to white?

Thank you,
John


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004
Nov 20 '05 #2
* Ken Tucker [MVP] <vb***@bellsout h.net> scripsit:
Dim penBorder As New Pen(Color.White , 3)
Dim hdc As IntPtr = GetWindowDC(m.H Wnd)
Dim g As Graphics = Graphics.FromHd c(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle (penBorder, rDraw)

ReleaseDC(Me.Ha ndle, hdc)


Don't forget to dispose the pen 'penBorder' there.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3
Ken,
What do I do with this code or where does it need to go. I created a new
project and added one label to the form. Here is the entire code:

Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows. Forms.Label

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

Me.Label1 = New System.Windows. Forms.Label

Me.SuspendLayou t()

'

'Label1

'

Me.Label1.Borde rStyle = System.Windows. Forms.BorderSty le.FixedSingle

Me.Label1.Locat ion = New System.Drawing. Point(48, 56)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing. Size(136, 48)

Me.Label1.TabIn dex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)

Me.ClientSize = New System.Drawing. Size(292, 273)

Me.Controls.Add (Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout (False)

End Sub

#End Region
I added it on the end but got nothing. Do I need to call something somehow
or mix pieces of the code in with the above code?

Thanks,
John

"Ken Tucker [MVP]" <vb***@bellsout h.net> wrote in message
news:uv******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

If you want to change the border color you have to override the
WM_NCPAINT message.
Public Class BorderLabel
Inherits Label

Declare Function GetWindowDC Lib "user32" Alias "GetWindowD C" (ByVal
hwnd As IntPtr) _
As IntPtr
Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal
hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer
Protected Overrides Sub WndProc(ByRef m As
System.Windows. Forms.Message)
Const WM_NCPAINT = &H85

If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White , 3)
Dim hdc As IntPtr = GetWindowDC(m.H Wnd)
Dim g As Graphics = Graphics.FromHd c(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle (penBorder, rDraw)

ReleaseDC(Me.Ha ndle, hdc)
Else
MyBase.WndProc( m)

End If

End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub

Public Sub New()
Me.BorderStyle = BorderStyle.Fix edSingle
End Sub
End Class

Ken
---------------------------

"jcrouse" <me> wrote in message
news:eC******** *****@TK2MSFTNG P10.phx.gbl:
Is there a quick and easy way to change the color of a label controls
border
from the default black to white?

Thank you,
John


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004

Nov 20 '05 #4
Hi Ken,

Can you change this in your sample?
Const WM_NCPAINT = &H85

Const WM_NCPAINT As Integer = &H85

:-)

Cor
Nov 20 '05 #5
Hi,

Sure.

Ken
--------------------

"Cor Ligthert" <no**********@p lanet.nl> wrote in message
news:#u******** ******@TK2MSFTN GP10.phx.gbl:
Hi Ken,

Can you change this in your sample?
Const WM_NCPAINT = &H85


Const WM_NCPAINT As Integer = &H85

:-)

Cor


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004
Nov 20 '05 #6
Hi John,

Using Kens code I made this class, you can use it by creating a new item
(usercontrol), name it UserLabel, delete all the code, build, and now you
can drag it from your toolbox usercontrols on your form.

Public Class UserLabel
Inherits System.Windows. Forms.Label
Public Sub New()
MyBase.New()
InitializeCompo nent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Disp ose()
End If
End If
MyBase.Dispose( disposing)
End Sub
Private components As System.Componen tModel.IContain er
Private Sub InitializeCompo nent()
components = New System.Componen tModel.Containe r
Me.BorderStyle = BorderStyle.Fix edSingle
End Sub
Declare Function GetWindowDC Lib "user32" Alias _
"GetWindowD C" (ByVal hwnd As IntPtr) _
As IntPtr
Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" _
(ByVal hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer
Protected Overrides Sub WndProc(ByRef m _
As System.Windows. Forms.Message)
Const WM_NCPAINT As Integer = &H85
If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White , 3)
Dim hdc As IntPtr = GetWindowDC(m.H Wnd)
Dim g As Graphics = Graphics.FromHd c(hdc)
Dim rDraw As Rectangle = _
New Rectangle(0, 0, Me.Width, Me.Height)
g.DrawRectangle (penBorder, rDraw)
ReleaseDC(Me.Ha ndle, hdc)
g.Dispose()
Else
MyBase.WndProc( m)
End If
End Sub
End Class

I hope this helps?
(The code is changed from Ken not original from me).

Cor
Nov 20 '05 #7
Hi,

Compile it into a control and add it to your form.
http://www.onteorasoftware.com/downl...hiteborder.zip

Ken
-------------

"jcrouse" <me> wrote in message
news:ei******** ******@TK2MSFTN GP10.phx.gbl:
Ken,
What do I do with this code or where does it need to go. I created a new

project and added one label to the form. Here is the entire code:

Public Class Form1
Inherits System.Windows. Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()

MyBase.New()

'This call is required by the Windows Form Designer.

InitializeCompo nent()

'Add any initialization after the InitializeCompo nent() call

End Sub

'Form overrides dispose to clean up the component list.

Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)

If disposing Then

If Not (components Is Nothing) Then

components.Disp ose()

End If

End If

MyBase.Dispose( disposing)

End Sub

'Required by the Windows Form Designer

Private components As System.Componen tModel.IContain er

'NOTE: The following procedure is required by the Windows Form Designer

'It can be modified using the Windows Form Designer.

'Do not modify it using the code editor.

Friend WithEvents Label1 As System.Windows. Forms.Label

<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()

Me.Label1 = New System.Windows. Forms.Label

Me.SuspendLayou t()

'

'Label1

'

Me.Label1.Borde rStyle = System.Windows. Forms.BorderSty le.FixedSingle

Me.Label1.Locat ion = New System.Drawing. Point(48, 56)

Me.Label1.Name = "Label1"

Me.Label1.Size = New System.Drawing. Size(136, 48)

Me.Label1.TabIn dex = 0

Me.Label1.Text = "Label1"

'

'Form1

'

Me.AutoScaleBas eSize = New System.Drawing. Size(5, 13)

Me.ClientSize = New System.Drawing. Size(292, 273)

Me.Controls.Add (Me.Label1)

Me.Name = "Form1"

Me.Text = "Form1"

Me.ResumeLayout (False)

End Sub

#End Region
I added it on the end but got nothing. Do I need to call something somehow

or mix pieces of the code in with the above code?

Thanks,
John

"Ken Tucker [MVP]" <HYPERLINK
"mailto:vb***@b ellsouth.net"vb ***@bellsouth.n et> wrote in message
news:uv******** *****@TK2MSFTNG P11.phx.gbl...
Hi,

If you want to change the border color you have to override the
WM_NCPAINT message.
Public Class BorderLabel
Inherits Label

Declare Function GetWindowDC Lib "user32" Alias "GetWindowD C"
(ByVal
hwnd As IntPtr) _
As IntPtr
Declare Function ReleaseDC Lib "user32" Alias "ReleaseDC" (ByVal
hwnd As IntPtr, _
ByVal hdc As IntPtr) As Integer
Protected Overrides Sub WndProc(ByRef m As
System.Windows. Forms.Message)
Const WM_NCPAINT = &H85

If m.Msg = WM_NCPAINT Then
Dim penBorder As New Pen(Color.White , 3)
Dim hdc As IntPtr = GetWindowDC(m.H Wnd)
Dim g As Graphics = Graphics.FromHd c(hdc)
Dim rDraw As Rectangle = New Rectangle(0, 0, Me.Width,
Me.Height)

g.DrawRectangle (penBorder, rDraw)

ReleaseDC(Me.Ha ndle, hdc)
Else
MyBase.WndProc( m)

End If

End Sub

Protected Overrides Sub Finalize()
MyBase.Finalize ()
End Sub

Public Sub New()
Me.BorderStyle = BorderStyle.Fix edSingle
End Sub
End Class

Ken
---------------------------

"jcrouse" <me> wrote in message
news:eC******** *****@TK2MSFTNG P10.phx.gbl:

Is there a quick and easy way to change the color of a label
controls
border
from the default black to white?

Thank you,
John

--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004


--
Outgoing mail is certified Virus Free.
Checked by AVG Anti-Virus (http://www.grisoft.com).
Version: 7.0.230 / Virus Database: 263.3.6 - Release Date: 6/25/2004
Nov 20 '05 #8
* Ken Tucker [MVP] <vb***@bellsout h.net> scripsit:
Sure.


.... but in this case not necessary. The value will be treated as
'Int32' automatically.
Const WM_NCPAINT = &H85


Const WM_NCPAINT As Integer = &H85


--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #9
Hi Herfried,

You can be glad that Armin is not active some days I think.

... but in this case not necessary. The value will be treated as
'Int32' automatically.
Const WM_NCPAINT = &H85

Const WM_NCPAINT As Integer = &H85


Do you think I saw this, I was just making a sample for John, while I
thought Ken was not active, when I saw that Ken was I had already made it
and found it a pity not to send it.

Cor
Nov 20 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
12352
by: Stuart Norris | last post by:
Dear Readers, I am attempting to draw box around some text using unicode on multiline label. The label is forty characters wide and 12 lines deep. I have been trying to draw a box around text (centered in the label) on this label. My font on this label is Courier new - hence fixed width character cells.
5
4313
by: malcolm | last post by:
I'm trying to make a combo box custom control (Windows Forms .NET 1.1 c#) that can behave like a label programatically at run time. This is actually a strong feature request by our customers. I really don't want to have to make a UserControl that has both a label and ComboBox in it. I would rather have my control derive from a ComboBox for databinding (and other) reasons. I can get close, I've tried setting cbo.DropDownStyle =...
3
5365
by: Dude | last post by:
Below is the code - it is finding the control, and there is no error, just not assigning the text to the label <asp:DataList id="dlGoals" runat="server" onEditCommand="myListEditHandler" onUpdateCommand="myListUpdateHandler" onCancelCommand="myListCancelHandler" onDeleteCommand="myListDeleteHandler" DataKeyField="ID"> <ItemTemplate>
2
2436
by: John Holmes | last post by:
I would like to turn on a label control in javascript with the onfocus event of another control by setting the Visible property to true. When I try and reference this label control it says "Microsoft JScript runtime error: 'document.Form1.lblSigGrantor' is null or not an object". I wrote a loop and iterated through the elements for the form and this control doesn't show up. Code listed below, please help. Thanks, John Holmes
7
8993
by: Ed West | last post by:
Hello, I have a simple form with some input boxes. After validation if one fails, then I would like to at the top of the page say something like "The following fields in red are required" and then change the label in front of the textbox or dropdown list to red... is this possible with asp.net? It seems you can only put a RequiredFieldValidator on the page, and if it fails validation then that text is displayed... ? Thanks
1
4295
by: Jameel | last post by:
Hi Coders, How do i Add a Lable control to DataList to show the Count Files which belong the current Category. i have a DataList which displays categoryName,Description and File counter (coutn the images belong to category), i able to display first 2 values from database, but how to dispalyg the image counted data in DataList ? Here is my code which doesnt working:(i am using code behind to pull the data from DB)
4
2974
by: Ben | last post by:
Hi, i have some problem to access from code-behind a label nested into a CreateWizard control. I use a html-table for align purpose only. I try to change the text property of the label with ID="UserNameLabel". <asp:CreateUserWizard ID="CreateUserWizard1" runat="server"> <WizardSteps> <asp:CreateUserWizardStep runat="server"> <ContentTemplate>
1
403
by: Sue | last post by:
Hello Is there an equivalent of a OnTextChangedEvent for a asp: label control in asp.Net 2.0. Every time the text of a label changes, I want the items in a listbox control cleared. Tried doing as textbox and gave the ReadOnly="True" and border=0 to mimic a label control, but since the Readonly is set to true, the "OnTextChanged" event does not get fired.
1
2902
by: cnixuser | last post by:
Hello, I am having a problem that I believe is related to the way a stream reader object looks for a text file by default. What I am doing is using a StreamReader object to read the text of a text file which includes some html code to populate html formatted content as the text of an asp:label (<asp:label>). The reading of the text file itself goes just fine ;however, this only occurs when I use an absolute file path which will not work of...
0
9793
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11151
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10419
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9577
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3237
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.