How to disable editing or hide text edit field, when DropDownStyle = Simple for Combo Box control?
MSDN on ComboBox.DropDownStyle:
The DropDownStyle property specifies whether the list is always displayed or whether the list is displayed in a drop-down. The DropDownStyle property also specifies whether the text portion can be edited.
Docs on ComboBoxStyle.Simple:
Specifies that the list is always visible and that the text portion is editable. This means that the user can enter a new value and is not limited to selecting an existing value in the list.
So, ComboBoxStyle.Simple suggests that list can be edited by user and it's confusing to disable edit with this DropDownStyle selected. Alternatives:
If you are ok with drop-down list use ComboBoxStyle.DropDownList
If you want to display a non-editable list with a view similar to ComboBoxStyle.Simple consider using ListBox
If you really need to achieve this effect on Combox you can just catch the events like "TextChanged" and then setting it back to "" and asking if (!comboBox1.DropDownStyle == ComboBoxStyle.Simple) before adding items to Items collection. Although it seems there are better ways to achive similar functionality using listbox as suggested before.
Related
On a form, I have a default ComboBox, I have another button that allows user to add more ComboBoxes, however, when user adds new combobox dynamically, existing selected value in the default combo box changes, where as idea is however many ComboBoxes are added, every previously added ComboBox would not lose a selection if user has selected anything.
Can some one point in the right direction?
Thanks
You are not very clear on your problem, however I would suggest setting a new BindingContext to your Control, before adding it to the form.
Good luck.
Is there a way to add an item which has no text and even if that item selected it would be shown that nothing selected at all or SelectedIndex would be smaller than zero?
No, the combo box does not support this. There is no built-in logic for treating an item as non-selectable.
Sounds like you will need to either implement an owner draw combo box, or perhaps even a new control from scratch to make this work. I'm sure there are third-party controls that would do what you need as well.
Depending on what you are trying to accomplish (you didn't provide much detail), the ListView control might also provide some options for you.
You can add an item that is represented by an empty string, sure.
But SelectedIndex will return the selected index of the item.
Excerpt from MSDN: A value of negative one (-1) is returned if no item is selected.
You should consider working with the value of the item instead of its index.
Specifically, in my original design, I had Text comments in my ComboBox at load-up time, but then realised that users could type into the box, so I changed the DropDownStyle to DropDownList. Unfortunately, whilst this prevents the user from typing into the box, it also removes the Text from my VS2010 design and also ignores my C# updates to the Text in the program.
Is this normal behaviour or do I have a problem, or do I need to do this via another parameter ?
Keep your DropDownStyle on DropDown.
now on KeyPress event add the following:
e.Handled = true;
This is probably something simple. The winforms combobox items by default can be edited by the user, how to disable this?
Set DropDownStyle = DropDownList.
Set the ComboBox.DropDownStyle to DropDownList.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
Specifies that the list is displayed by clicking the down arrow and
that the text portion is not editable. This means that the user cannot
enter a new value. Only values already in the list can be selected.
Set the ComboBox style to ComboBoxStyle.DropDownList
Try setting the DropDownStyle property to DropDownList. Style of Simple makes it like a listbox, Combobox is what you are seeing allowing editing, and DropDownList only allows user to select.
two Method that help you Stop User to not Edit DropDownList:
A. using Programming code:
DropDownListName.DropDownStyle = ComboBoxStyle.DropDownList;\
B. using Design properties of Visual Studio
Set DropDownStyle = DropDownList.
I hope this well help you.
I have a combobox that displays a list of 'active' objects the user can select from. However, if the user goes into some older data screens, the selected values of some of the comboboxes might be old and 'inactive' objects that aren't in the current list.
I want the combobox to just display the old value even though it is not in the list. And I don't want the user to be able to manually edit it either.
Any suggestions?
Thanks!
Isaac
I would assume that it's your code that is removing the objects from the ComboBox in the first place, so you just... don't do that. To disallow typing into the ComboBox set its DropDownStyle property to ComboBoxStyle.DropDownList.