C# Adding Combobox dynamically losing selected items in others Combobox - c#

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.

Related

Selected ListViewItem is not highlighted on TabPage, after selected TabPage changed forth and back

I am currently facing a strange behavior of the C#.Net ListView when used on a TabControl's TabPage.
Background:
I have said TabControl with three TabPages, the ListView is on the first one.
Scenario:
I select one Item in the ListView and its "Selected" property is set to "true"
I change to another TabPage
I change back to the first TabPage with the ListView containing the selected Item
Result:
The former selected Item in the ListView is not highlighted any longer. Iterating over all Items shows that its "Selected" property is still set to "true".
As soon as I click into the ListView control, I can see the selected Item being highlighted for a short moment, before loosing its highlighting again due to me having clicked somwhere else and therefore deselecting it.
But neither one of Refresh, Invalidate, RedrawItems, Focus or Select on the ListView leads to the selected Item being highlighted again.
Has anyone of you experienced the same issue and found a proper way to solve this?
My current solution is to clear the List of SelectedItems on the ListView as soon as the corresponding TabPage is re-focused, hence deselecting every Item and forcing the user to re-select what he wants to be selected.
But since I also noticed that ListBoxes can savely store their selection and display it properly, I would wish for the same feature on the ListView.
Thanks,
Thomas

combobox items not show in datagridview

I have a win form project in c#, there is a combo box in a datagridview.
it has 2 items which added in design. when I run the program, the combo box is empty.
why? please help me
I added the items in Design mode and it doesn't show anything then I added items programatically and it doesn't work either.
Here is the code:
cbcCell = (DataGridViewComboBoxCell)dgvFolder.Rows[0].Cells[1];
cbcCell.Items.Add("cat");
cbcCell.Items.Add("dog");
Without too much information here, the likely problem here is that the ComboBox has the ReadOnly property set to true. You can fix this by going into Design View, click the little arrow that appears in the top left of the DataGridView, select Edit Column, select the appropiate column/cell, and change the ReadOnly property to false.

WinRT XAML ComboBox opens in middle of list

So one particular behavior of the XAML combobox in WinRT is causing me a huge headache, because my client sees it as a defect, and doesn't care if it's the behavior of the control, he wants it changed. However, I cannot find anything that tells how to change it. The behavior I'm speaking of is that when nothing is selected the ComboBox popup opens displaying the ItemsSource in the middle of the list. I have a sorted list of countries, with the exception of US, UK, CAN being at the top. These 3 items are the most often selected items and the client wants them on top rather than having to scroll through the list to find them. That's easy enough, but because the list opens in the middle, you still have to scroll quite a bit to get to them. Is there some property I'm missing that turns this behavior off? I was able to finally convince them that the CarouselPanel wasn't a defect, but this one isn't going to fly.
Thanks in advance!
UPDATE:
So this combobox is databound through a ViewModel. in this instance, the ViewModel has no value (it is an empty string) for that particular property and so the Combobox shows empty, which is fine and desirable. When you click on the Combobox to select a value, it displays the list in the middle of the available values. this is the behavior that is undesirable. it should be showing the 1st value in the list at the top!
Well, one would think that the out of the box Combobox (there is no other built in dropdown control) would be able to work like any other combobox control in any other MS technology to date, but of course this is MS, so why should things be consistent. At any rate, I ended up having to create a "blank" entry and pre-select that item if the value in the VM is empty, and then write code in the setter of that property to ignore if "blank" item if it is selected. It's kludgy and wreaks of code smell, but it works
When you set the SelectedItem property to an object, the ComboBox attempts to make that object the currently selected one in the list. If the object is found in the list, it is displayed in the edit portion of the ComboBox and the SelectedIndex property is set to the corresponding index. If the object does not exist in the list, the SelectedIndex property is left at its current value.

.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, can I configure the combobox to allow values not in the drop down list?

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.

Categories

Resources