I have a comboBox which correctly loads the List of data that I've queried from the DB, my only issue is that I can't seem to make it load blank and force the user to select from the list (which then pushes the actions under the SelectedIndexChanged() method). Through my searches, I've seen where I can simply change the SelectedItem value to -1, and that should load the comboBox on a null choice, but when I use this code I see no difference. The comboBox still loads the list correctly, but the first entry in the list is still displayed.
private void loadPatientList()
{
comboBox_PatientSelect.DataSource = patientList.Distinct().ToList();
comboBox_PatientSelect.DisplayMember = "displayFullName";
comboBox_PatientSelect.ValueMember = "patientID";
comboBox_PatientSelect.SelectedItem = -1;
}
I appreciate any help - and will be happy to provide more information if needed. Thank you in advance!
The property you're looking for is SelectedIndex. So make it comboBox_PatientSelect.SelectedIndex = -1;
From MSDN:
This property indicates the zero-based index of the currently selected item in the combo box list. Setting new index raises the SelectedIndexChanged event.
SelectedIndex, SelectedValue, and FormattingEnabled are related as follows:
If FormattingEnabled is false, SelectedIndex will not be set to -1 when SelectedValue is blank.
If FormattingEnabled is true, SelectedIndex will be set to -1 when SelectedValue is blank.
NOTE:
To deselect the currently selected item, set the SelectedIndex to -1. You cannot set the SelectedIndex of a ComboBox item to -1 if the item is a data-bound item.
Related
For a Combobox, I am getting a list of values from the System.Collections.ObjectModel.Collection.
I am assigning the values like this:
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
I don't want to see the first item in the list in the Combobox, unless I selected it.
How do I display a blank field in the Combobox when nothing is selected?
If I understand correctly you just need to reset the SelectedItem. Just set SelectedIndex to -1.
this.cmbSqlServer.DataSource = this.SqlInstancesCollection;
this.cmbSqlServer.SelectedIndex = -1;
I am developing windows 8.1 app using Xaml and c#, Windows 8.1 selectes the first list item when the list loads. How can i Change this so that the list has no selectedItem when it loads.
From MSDN documentation:
Use the SelectedIndex property to determine the index of the currently selected row in a >GridView control. You can also use this property to programmatically select a row in the >control. To clear the selection of a row, set this property to -1.
Source: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.selectedindex(v=vs.110).aspx
In my case, setting the SelectedIndex to -1 did not work. If you are populating your GridView from a CollectionViewSource, you have to insert this line in the GridView :
IsSynchronizedWithCurrentItem="False"
From MSDN
Selection behavior and CollectionViewSource
List controls that derive from Selector have a default selection behavior that depends on what the items source is (the type that's used for ItemsSource). If the items source is a CollectionViewSource instance, then the behavior in the selection control is that the selection will default to the current item. When the list is first displayed, the selection defaults to the first item as current item. If you don't want the first item to be selected in this case, set IsSynchronizedWithCurrentItem to false in the GridView.
I need to track current selected item in ListBox to turn off some other controls on the form when selected item become null. I try to use SelectedIndexChanged event, but it not raise when selected item is null.
Can you please advise something?
UPDATE: Selected item becomes null because i set new DataSource value with empty collection. May be it's a reason of my problem?
I need to explain. ListBox represent collection of items from database. When user add/edit/delete some item, I refresh listbox by calling this method:
private void RefreshList()
{
lbParts.DataSource = this.database.Fetch<part>(string.Empty);
}
It fetched all items from database, convert it to List<part> collection and set as ListBox DataSource.
That is incorrect. SelectedIndexChanged is raised when SelectedItem becomes null. In this case, SelectedIndex will be -1.
EDIT: you are indeed correct that when you change DataSource, you don't get SelectedIndexChanged. I would recommend explicitly setting SelectedIndex=-1 immediately before anytime you change DataSource
Perhaps handle the DataSourceChanged event as well? If the choices available in the listbox change, then I assume this is reason to perform a refresh on the forms avialable controls?
I cannot seem to figure out how to dynamically change the selected item in a combo box. I am trying this:
myComboBox.SelectedItem = item.Id;
Here item.Id is a int that corresponds to valid ValueMember that is bound to the combobox. However the combobox remains unchanged. I have trying invalidating the control after changing the selected item. What's the trick?
Thanks
try SelectedValue instead..
myComboBox.SelectedValue = item.Id;
You can use either of these:
SelectedIndex Property
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.
SelectedItem Property
Get or Set A zero-based index of the for selected item.
I use VS2008 C# + Windows Forms. I can't understand why comboBox does not behave the way it should.
In Design mode, I added a comboBox to my form, and edit Items to add "A" and "B". Double-clicking brings me to SelectedIndexChanged event, which I edit to display the selected text with MessageBox.
private void comboBoxImageSet_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(comboBoxImageSet.SelectedText);
}
When I run, and select "A" or "B" in the comboBox, the MessageBox appears, but nothing is written.
Why?
Thanks.
Here the differences between the selection properties of a ComboBox control.
SelectedIndex;
SelectedItem;
SelectedText;
SelectedValue.
The SelectedIndex property :
Gets or sets the index specifying the currently selected item.
Simply indicates the index of the selected item in the selection list. (Information provided for your kind information only. =))
The SelectedItem property :
Gets or sets currently selected item in the ComboBox.
The SelectedItem represents the element that is currently selected as per the ListControl of the ComboBox. That is why this is what you want to use, to answer your question.
The SelectedText property :
Gets or sets the text that is selected in the editable portion of a ComboBox.
That is, when you edit the TextBox portion of the ComboBox, the text that might be selected when you enter for edit, or any other type of text selection. This indeed does include any selection made through the ListControl portion of the ComboBox. For instance, if your ComboBox.DropDownStyle property is set to ComboBoxStyle.DropDownList, then you will never be able to select any text in the editable portion of the ComboBox. Despite, you're able to select another item within the its list. That is why it is not the right property to use to serve your purpose.
The SelectedValue property :
Gets or sets the value of the member property specified by the ValueMember property.
Only used when using DataBinding, in conjunction with the DisplayMember property. For instance, when you want to display the name of a customer, and select him by his database Id, then the DisplayMember should display the customer's name, and the ValueMember the Id. This way, when you select one customer, the SelectedValue changes and raises the SelectedValueChanged event inherited from the ListControl. (Information provided for your kind information only. =))
The SelectedText property returns the text that is marked in the combobox, not the selected item. If the combobox is editable you can mark a part of the text and the SelectedText property will return the marked text. Look here.
What you are interested in is the SelectedItem property or the SelectedValue property.
ComboBox.SelectedText
A string that represents the currently
selected text in the combo box. If
DropDownStyle is set to DropDownList,
the return value is an empty string
("").
Use SelectedItem instead of SelectedText
SelectedText:
Gets or sets the text that is selected
in the editable portion of a ComboBox.
That is, it gets the text that is currently marked.
You want to use SelectedItem.ToString().