In a form, I have both textbox and comboBox. Initially, I use DropDown for comboBox, now I would like to change some ComboBox from DropDown to DropDownList (to prevent user input).
Now the issue is: When comboBox is changed to DropDownList, the backcolor (inside comboBox) turns to form background color. While I prefer to have white color (as it was when I use DropDown). In other words, I want to have both DropDownList and white background color.
How to do that?
Thanks.
In Winforms the means of styling your controls properties are somewhat limited.
Have you checked out the all the properties the control offers?
e.g. Backcolor? https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox.backcolor?view=net-5.0
If this does not help achieving what you want might not be easily done.
In most situations where you end up wanting to actually define the look of your app you will either have to basically learn a lot of arcane ways to create your own controls, or, as it bears repeating no matter how condescending it sounds: you should’ve picked WPF. The point of WPF is precisely to give you the tools to customize ui elements without having to build custom controls.
The color SystemColors.Window
is reserved (generally) for areas considered to be editable or in some way selectable.
Appearance -> DropDownStyle.Simple
This is a combination of EDIT and SELECT windows in a large rectangle with both controls visible at all times. Both controls are colored with a background of SystemColors.Window
(white) because both are editable or selectable.
Appearance -> DropDownStyle.DropDown
This is an EDIT window with a hidden SELECT window. The hidden SELECT window is toggled by the dropdown button. Both controls are still white because they’re both still editable or selectable.
Appearance -> DropDownStyle.DropDownList
This is a LABEL window with a hidden SELECT window. The label is NOT editable, so it gets SystemColors.Control
for the background. The hidden SELECT is the same as DropDownStyle.DropDown
with a white background from SystemColors.Window
.
Now you’re going to say, but I set the background of the control to “white” (or whatever) and it doesn’t look right. That is correct. You don’t have direct access to the label (just the label’s container), so when you change the background, the label doesn’t inherit the color like one would assume it would. Blame Microsoft.
You’re going to have to learn how to create your own control and/or “owner draw” the control by hooking into how Windows draws the control. None of this is trivial. It’s all extremely difficult to “get right”, so when it comes to the appearance of a Windows control in an Windows Forms application I always recommend that you save yourself the headache of potentially weeks worth of work for almost no real gain and simply allow Windows to do the heavy lifting. Sorry. Them’s the breaks.
Thanks. Sounds complicated for people who is new to Visual Studio, don’t even follow the message. I will just use Dropdown, rather than Dropdownlist