Solved

Changing VB.Net control backcolor property to Default Control color?

Posted on 2013-11-08
9
1,048 Views
Last Modified: 2013-11-11
This might be a relatively easy question but I'm not sure how to change a Vb.Net control's backcolor property to the default "Control" color? I know how to do in design mode (see screenshot), but the Color Object does not have "Control" as a color. How can I change this in code?

Thanks!
Screenshot.jpg
0
Comment
Question by:BlakeMcKenna
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39634016
not sure of exactly what you want but have a look at http://emoreau.com/Entries/Articles/2005/07/Colors-of-disabled-controls.aspx
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39634023
Me.Button1.BackColor = Color.FromKnownColor(KnownColor.Control)

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 39634049
kaufmed,

That helps but I guess it was my fault for not being more clear. How do I get the integer value for that color? The integer value is what I need to store in the DB.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39634064
I think it's 5
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 400 total points
ID: 39634106
KnownColor is an enum, so just cast it.

Dim controlColor As Integer = DirectCast(KnownColor.Control, Integer)

Open in new window


To revert, cast again:

Dim controlColor As KnownColor = DirectCast(someIntegerValue, KnownColor)

Open in new window

0
 

Author Comment

by:BlakeMcKenna
ID: 39634199
Well I tried that and found that I didn't even need to Cast it. Just a direct assignment:

    ctlBackColor = KnownColor.Control
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39634337
Possibly. VB is a bit more forgiving on implicit casts, so it may be fine. You might encounter issues if you're ever asked to port the code over to C#.
0
 
LVL 40
ID: 39635229
Wrong solution. This might work with some controls, but not all. By default, the Control color is the color you get on a button, a light shade of gray. Many controls use another color. For instance, the default color of a TextBox is WindowText, which is White. But the result can be changed, at least under Windows XP, through the Configuration Panel or if the user applies a theme to its Windows environment.

If you want to set the default color according to the current Windows theme, then you would need to set the color differently depending on whether you are dealing with a Button or a TextBox.

If you really want the default color for a given control, or simply want a single way of specifyint the color, use the DefaultBackColor property for the specific type of control, for instance:

txtIntRate.BackColor = TextBox.DefaultBackColor
btnQuit.BackColor = Button.DefaultBackColor
0
 

Author Comment

by:BlakeMcKenna
ID: 39638856
Hi James,

I see your point here and it makes sense. Here is what I am trying to do. I have an application that allows the user to customize the look and feel of the various controls. There is a Form that has GroupBoxes that contain 4 different properties for controls (each GroupBox represents a specific Control...see Screenshot). These are the common controls used, i.e. TextBox, Button, Label, DataGridView, GroupBox, StatusStrip, etc.

The 4 different properties are the Bold, Italics, BackColor, ForeColor. The BackColor/ForeColor are Checkboxes and when checked, the Color Dialog pops up allowing the user to select the color they want.  Here is a snippet of code that is used for all the controls and modified accordingly.

                Dim pal As New ColorDialog
                Dim iColor As Integer
                Dim ctlBackColor As Integer
           
                If cboButtonBackColor.Checked = True Then
                    pal.ShowDialog()
                    iColor = pal.Color.ToArgb
                    ctlBackColor = iColor
                    pbButtonsBackColor.BackColor = pal.Color
                Else
                    ctlBackColor = KnownColor.Control
                    pbButtonsBackColor.BackColor = Me.BackColor
                End If

The ctlBackColor gets stored as an integer to the Database. Whenever a Form is activated, all the Buttons will reflect that stored value using the "Color.FromArgb(Database Value)".

My point being is that in the form that allows the users to customize controls, I can't really assign Button.DefaultBackColor because I'm not assigning it to an actual button control rather an integer variable. If you know of a way, I'm all ears!!! Thanks
Screenshot.jpg
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

29 Experts available now in Live!

Get 1:1 Help Now