Read only combobox? - c#

How do I make a combobox read only? I remember back when I used VS2005 you could have a read only combobox in the default style. Reason I don't want to use DropDownList is because it looks ugly on forms that don't use a default color. Here is an example.
I want one like in the left of this picture.
Were you can not enter in your own text. But I don't want the style in the first image to the right.

Set the drop down style to DropDownList and the flatStyle to Flat:
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox1.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
You can select always the element[0] (if exists) to look like the example you provided.
The Visual Studio combo draws a border OnMouseHover, you can also do this if you want.

Change the drop down style, it's a property.
See: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle.aspx

Related

How to set the format on a WinForms ComboBox AutoComplete drop down

I'm using a Winforms ComboBox, specifying the background color and font programatically. I've also set AutoCompleteMode to "Suggest" and AutoCompleSource to "ListItems", via the designer properties.
Autocomplete seems to work great, but the drop down it displays for auto complete uses the Winforms default background color and font. I'd like it to match the background color and font of the rest of the control. Does anyone know how to accomplish this in .Net 4.0?
Thanks for your help.
AFAIK you cannot do this. Setting the background colour only affects the "TextBox" part of a ComboBox, i.e. when you are using a ComboBox in DropDown style (default) allowing the user to enter free text as well as selecting from the list.

Check Box Label Above Checkbox. How?

I am using C# visual studio 2010 to develop an ASP.NET website.
I dynamically create a checkbox at run time.
CheckBox chkbox = new CheckBox();
chkbox.ID = "chk" + checkboxID;
// Add our checkbox to the panel
dynamicPanel.Controls.Add(chkbox);
chkbox.Text = checkboxName;
By default, the label is displayed to the right of the checkbox. I can successfully move the label from side to side by adjusting chkbox.TextAlign = TextAlign.Right / Left.
What I can not figure out for the life of me is how to set the Text above the check box.
I am not looking for any kind of hack like, verticle-align:-3px as this will not work for me because I let the user pick the font and size of the text. It will not always be -3px in depth.
I suppose you should go with a new Control where you add a separate Label below the Checkbox (actually wrapping the checkbox).
Derieve that class from CheckBox, and override (or define new if not virtual) the Text property, so it will now set the upper Label's text.
This will actually be a good example of Decorator pattern.
Set the text align property to TextAlign.Left then use CSS to set the label's display to: block.

How to make a ToolStripComboBox look like a regular ComboBox

I am dynamically adding a ToolStripComboBox, and I need it to look like a regular combo box.
The tool strip version has the editable field and looks completely different. Is there a property (or multiple properties) I need to set to make it look and feel like the default combo box?
Set property FlatStyle to FlatStyle.Standard; this will give you the same dropdown arrow button.
Set property DropDownStyle to ComboBoxStyle.DropDownList; this will make it so that you can select but not edit.
In Visual Studio designer it would be setting the property to DropDownList:

Make the text of a disabled textbox easier to see

I have a text box that when it is disabled the text in it is gray and kind of dithered. (This is the standard functionality.)
Is there a way to make this easier to see?
I have tried this:
txtBoxNumber.Enabled = false;
txtBoxNumber.ForeColor = Color.Black;
and that has no effect.
NOTE: This is a .net Compact Framework app, but I am not tagging the question with CF because I think it is the same for normal .net.
txtBoxNumber.ReadOnly = true;
// Then set your styles here...
HTH.
Why don't you make the TextBox.ReadOnly instead? That would allow the user to see & copy the textbox value, but not change it. A read-only textbox is usually rendered the same way as a normal textbox.
From MSDN:
You can use this feature instead of disabling the control with the Enabled property to allow the contents to be copied and ToolTips to be shown.
I often set it to read only or if you have to use disabled set the text box color to white and the font color to black.
Just make the text box Read-Only. And then if you need to set style, set it.

Change forecolor of disabled combobox

I need black forecolor in a disabled combobox. Is it possible?
I have searched around for information in the past about this, and as far as I can tell, the best solution is to change the DrawMode of the combo box to OwnerDrawFixed or OwnerDrawVariable and then write your own drawing code in the DrawItem event of the combo box.
I found this article that goes into much more detail about it. Hope it helps.
A "hack" I've used in the past for textboxes is to leave the control enabled, but capture the "OnFocus" event and immediately set the focus to some other object on the form, preferably a label since it doesn't show as being selected. I think this should work for comboboxes, too.
Not sure if your app is Winforms or WPF. The code given below works in a WPF app.
combo1.Items.Add("Item 1");
combo1.Items.Add("Item 2");
combo1.SelectedIndex = 0;
combo1.Foreground = Brushes.Black;
In my XAML I added a combo box and set its IsEnabled property to "false" then in the code behind I used the code given above and it does work.
HTH
All you need to do is say
combobox1.ForeColor = Color.FromName("Black");
It doesn't matter if the control is disabled or not, it should change the foreground color.
comboBox1.BackColor=Color.Black;

Categories