ComboBox with Simple DropDownStyle doesn't show the drop down - c#

I want to use a Combobox, with DropDownStyle=Simple, which changes the list of items when typing some content in the box.
The loading of the items is OK, and I can see them in debugging mode on the Items property, but the dropdown is not shown and seems to be empty.
I also tried to force the display of the dropdown putting
MyComboBox.DroppedDown = True;
Any clue on this behavior?

According to MSDN:
ComboBoxStyle.Simple is the style that
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 if the list is always visible then where is it. It is not visible because of the Size that is set by default. Change the height like so:
MyComboBox.Size = new System.Drawing.Size(256, 150);
The 150 denotes the height in this case. By default the height was something like 21 which was very less. Increase the height to some appropriate figure and the list should be visible.
Also a very important note: Do set the ComboBoxStyle before you set the size. I do not know why but it seems some invalidation or something is amiss here.
So the following would work:
//Will work
MyComboBox.DropDownStyle = ComboBoxStyle.Simple;
MyComboBox.Size = new System.Drawing.Size(256, 150);
but this will not:
//Will not work
MyComboBox.Size = new System.Drawing.Size(256, 150);
MyComboBox.DropDownStyle = ComboBoxStyle.Simple;
The latter might work with some explicit invalidation calls but i did not verify that.
If you want a drop down to open up when the user clicks on the drop down arrow then you would have to use the other combo box styles. In the Simple style the drop down arrow would not appear and the list would always be visible as the MSDN definition suggests.

I would post this as a comment but my reputaion isn't high enough.
How are you populating the ComboBox?
Your problem seems very familiar to:
ComboBox will not update its display list unless you change selections first

Related

Why doesn't the dropdown of the combobox not update when I change the ItemsSource?

While trying to alter the contents of a combobox on runtime, I stumbled upon a confusing feature.
(Please excuse the many images. I felt that they were severely needed to understand what's going on)
Above you can see my WPF form, my xaml and the codebehind for the Button_Click_1 event.
Incase you were wondering what strings were exactly being saved into this "txt" variable, it was "jon", "ted", "tod" and "adam". As you might expect "jon" would have the index of 0, "ted" of 1, "tod" of 2 and "jon" of 3.
What I want to do is change the "List" assigned to the "ItemsSource" directly without having to reassign a new "List".
What I noticed while testing was that this is definitely possible.
Below you will see the results of me clicking on the button once.
What essentially is happening is that the content of the "test.txt" file is getting saved into the the "txt" variable as a "List".
This variable is then assigned to the "ItemsSource" property of the combobox.
I then proceed to alter the "ItemsSource" directly by removing the item with the index of 1 ("ted").
Lastly, I set the "SelectedIndex" to the item with the new index of 1 ("tod").
When running the program and clicking the combobox to see what items are contained inside the dropdown, I am presented with exactly those three items contained inside of the altered "ItemsSource". ("jon", "tod" and "adam")
Now it starts to get a little more confusing when I decide to click on the button again.
As you can see, after clicking the button a second time, all four items of the "List" get displayed instead of the previous three.
My current theory of how the dropdown of a combobox works is that the content of the "ItemsSource" property gets saved into a sort of cashe which gets used by wpf internally to display the items of the dropdown. This saving of the content seems to also only happen on the first click of the choicebox (textbox). It is then no longer possible to change this cashe by altering the "ItemsSource" directly. Through further testing I also discovered that the only way to change/update this cashe is by assigning a whole new "List" to the "ItemsSource".
Is my theory in some way, shape, or form correct?

What windows forms control best matches a Selectable list

I want a box that shows 5 items (of the 20 that are available). The other fifteen are scroll-able to.
When I click on one of the items, it acts like a combo box in that it is the one selected/highlighted.
Is there a control for doing this already?
I have tried using the combobox and setting the height on it, but the height is still only one row high.
ComboBox cboResults;
cboResults = new ComboBox();
cboResults.Height = 500;
cboResults.DropDownStyle = ComboBoxStyle.DropDownList;
cboResults.IntegralHeight = true;
frmLookup.Controls.Add(cboResults);
Am I using the wrong control?
Try to use ListBox, setting a default height you can achieve the scrollable behaviour you want.
A ComboBox is a combination of an editable field with a list. The list either drops down or is shown and scrollable. For this set the DropDownStyle=Simple!
However a simple ListBox is probably more suitable, unless you do want to allow the user to enter data.
Of, if you want to display more complex data, maybe with columns a ListView is best; I found I promote most ListBoxes sooner or later to ListViews..

.Net Combo Box simple style without edit

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.

In WinForms, how can I create a delete button in a DevExpress GridControl?

I'm trying to create a delete button at the right of every row in a DevExpress GridControl, like this:
What I've done is added another column and set its ColumnEdit property to an instance of RepositoryItemButtonEdit. I handle the ButtonClick event, to delete a row.
I can determine which row I'm on from this code:
myGridView.GetRow(myGridView.FocusedRowHandle);
Because I don't want a text editor on my button, I set the TextEditStyle to HideTextEditor.
By default, the button shows an ellipsis.
To remove the ellipsis, I adjusted the Buttons property on the RepositoryItemButtonEdit. I set the Kind to Glyph and set the image to my X icon.
Unfortunately that seems to simply remove the button altogether.
Does anyone know a better way to do this, or a way to show a button with an image on it, in each grid row?
I discovered that there is actually a delete button kind. So, I do everything as in the question, but instead of choosing the kind Glyph, I choose Delete, and I don't need to select an image.
I have summarized what I found in the DevExpress forum:
Use the ButtonEdit control and set the TextEditStyle property to HideTextEditor. The Repository Item has a Buttons collection through which you can add a caption, image etc.
In the Buttons collection, change the "Kind" property to "Glyph".
You can use the CustomRowCellEdit event to conditionally apply editors on a cell-by-cell basis.
Make sure you set the Button's Kind property to "Glyph" and set the Caption property to whatever text you'd like:
DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit buttonEdit =
new DevExpress.XtraEditors.Repository.RepositoryItemButtonEdit();
buttonEdit.Buttons[0].Kind = DevExpress.XtraEditors.Controls.ButtonPredefines.Glyph;
buttonEdit.Buttons[0].Caption = "X";
buttonEdit.TextEditStyle =
DevExpress.XtraEditors.Controls.TextEditStyles.HideTextEditor;
e.RepositoryItem = buttonEdit;
You should handle the GridView's CustomRowCellEdit event, construct a new RepositoryItemButtonEdit and assign it to the e.RepositoryItem property.
Let me know if that works.

Preventing a control changing its Text property while it is not enabled

I have some DomainUpDown controls in my application. The behaviour is set like this:
Normal: Each control can individually goes Up/Down (Change selected item)
Special: If a check box is checked, Up/Downing the first control will cause the other controls to change as well
Now there are some cases that I want some of the controls to be disabled by setting Disabled = true. I did it, but in this case changing the selected item of the first control will cause the other disabled controls to change the selected item as well.
Is there a way to completly disable a control so it does not accepts something like control.Text = "bla bla" ?
P.S: I need not to hide the control!
One way:
You can hold a list of Textbox.Text values, which will hold entries for all of your textboxes.
Upon OnTextChanged:
if your textbox is enabled - update it's text property and store it up in the list.
if your textbox is disabled - set it's text property to the last value you had in your list.
You can use a Dictionary or anything similar that contain KeyValuePair objects.
I'm afraid there's not. At least not without additional work.

Categories

Resources