Manually change the selected item in a combo box - c#

I am trying to manually change the selected item in a combo box so that it will physically change the value on the interface. I have tried changing the index value and display value on the drop down list, but this does not change the value on the interface. Has anyone ever done this?
Use case:
A user adds a new value to the database. The observable collection that feedsd the combo box is updated in memory with the new value to choose from. But, you want to save the user a click and just make the new value they added as the selected item in the combo box, rather than forcing them to add the new item, and then go over to the combo box to select it.

If you really want to do this...
You could try:
yourComboBox.SelectedItem = // the new Object that you want to set the comboBox to;
If the object is found, it will also automatically update the SelectedIndex.

Related

How to go back to previously selected entry in a combobox?

In my application there is a requirement like this:
In a form there is a combo box and 4 textboxes. The combo box always contain range of values 1...10. Based on the value selected in the combo box, I need to read 4 data items corresponding to the selected value from a database and display these values in the 4 textboxes. Thus for each value in the combo box, there are 4 values in the database.
Also, user can change these values by editing the values in 4 text boxes.
As an example assume that currently selected value in the combo box is 1. So the data corresponding to record 1 is read from the database and shown in the 4 text boxes. If the user don't edit any value in the textbox and selects entry 2 (or any other value than 1) then the data corresponding to the newly selected value (i.e. 2) is fetched from the database and displayed in the 4 textboxes. But if the user edited any value by typing into any of the four available textboxes then those new values corresponding to record 1 should be saved to the database before switching to the newly selected entry, i.e. 2.
To do this I wrote some code in the "SelectedIndexChanged event handler" of the combo box. There I check whether the user has changed any value. If yes, I will prompt the user to save the data. After saving the data, the combo box will show the newly selected value by user i.e. 2 (and the 4 text boxes will show data corresponding to record 2).
All the above mentioned functionalities work fine. But there is an additional requirement "If the save operation to the database fails, then the initial value in the combo box should be shown along with the user edited data (in the textboxes) which means if the database write fails I have to show the currently selected value 1 without switching to the newly selected value 2".
In the SelectedIndexChanged handler I tried writing the below code.
If (DatabaseWriteFails)
{
ComboBox->SelectedIndex = previous_value; (previous_value is 1 in our example)
}
But the above code did not work (I think since we are trying to change the selected value from SelectedIndexChanged handler itself). When we enter the SelectedIndexChanged handler I can see that ComboBox->SelectedValue is already changed to the newly selected value i.e. 2.
The question is "how we can go back to the previous entry once we enter the SelectedIndexChanged handler"? Any help is appreciated. Thank you.
First of all, if you change the ComboBox value from the event, it will trigger that event again, so instead of using SelectedIndexChanged use SelectionChangeCommitted which occurs only when the user changes the selected item from UI, not programmatically.
Second, you should change the dropdown by using either SelectedItem or SelectedIndex, from my experience SelectedValue binding doesn't update the ComboBox.
My advice for you as mentioned in the comments is to use bindings or even ReactiveUI.

Change in place" the selected value in ComboBox in c#

I have a Combobox, I want to "Change in place" the selected value in comboBox in c#, and I get its data from the database.
When I select an item from combobox, I want to edit in place and change it, I do that by clicking Update Button to change it. But it doesn't work!
SpmoDataBase.Instance.PhoneType.PhoneType_Edit(cmbPhoneTypeEdit.Text);
the error is:
NullReferenceException Class
Object reference not set to an instance of an object.

keep combobox text synchronized with changed item text

I have a Combobox with several items (C# Labels because I want to change individual text colors). Within callbacks, when editing some textboxes, I change the color and/or text of the items. When I click on the combobox I see that the items in the list have the correct color/text. However, the color/text changes of the selected item are not directly reflected in the shown combobox text. How can I achieve that?
I tried to set the Text property of the combobox itself: no effect. Also setting the selected item to an empty Label and then set it back to the correct Label has no effect. If I set SelectedIndex to -1 and then back to the correct selected index it works and the shown text is updated, but this triggers the SelectionChanged callback which I do not want. I could first detach the SelectionChanged callback from the combobox and then attach it again, but this is in my opinion very ugly programming.
Maybe I am missing something simple...
Edit
I tried binding, following the suggestion of SLak:
List<Label> labels;
MyComboBox.ItemsSource = labels;
The result is still the same. Suppose index 0 is selected. When I change the corresponding Label:
labels[0].Contents = "new content";
then it is not reflected in the selected text of the combobox. When I click the combobox I can see the new text in the unfolded list, but only when I change the selection and then go back to index 0 the new text is shown by the combobox as the selected item. That synchronization should be autmatically.

C# - Change index of ComboBox item?

I have a combobox that I want to be able to add items to the beginning of. For example, when you click it, you get 1,2,3 , but I want to be able to add an option, 0, so that when you click it you get 0,1,2,3.
Is this possible without rebuilding the combobox?
Just use the Insert method of the Items property of the combo box:
myComboBox.Items.Insert(0, "New item at top");
Yes, you can use the Insert() method on the Items property of your ComboBox object.
var comboBox = new ComboBox();
comboBox.Items.Add("1");
comboBox.Items.Add("2");
comboBox.Items.Add("3");
comboBox.Items.Insert(0, "0");

Retrieve value of most recently SelectedItem from multi-select listbox

How can I retrieve the value of a clicked item in a multi-select listbox?
Currently, if I click one item and use lstBox.SelectedValue, it will return the proper value; however, if I then click on another item, I am still shown the first item's value.
Basically, I want the value of the item most recently clicked on, regardless of whether or not it is the SelectedValue.
If it is a multiple selection listbox, you can get a collection of all the selected items by using SelectedItems instead of SelectedItem.
If you need to know the sequence in which the items were selected, or which was selected most recently, I think you would need to record if yourself by SelectedIndexChanged event.
The SelectedIndexChanged handler will get called when you select/unselect an item in the listbox.
However, it doesn't indicate which one was selected/unselected.
listbox1.SelectedItems
will contain the currently selected items and you could internally keep track of which index was most recently added.

Categories