Retrieve value of most recently SelectedItem from multi-select listbox - c#

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.

Related

Windows 8 ListView (multiple selection) unselect all items expect latest selected?

I am using ListView with SelectionMod = Multiple, According to the client's requirement I am in a situation where I want to Deselect All items in listview except lastest selected,
if I change selection Mod programmatically to single, it deselects all items but the selected item is then the first one(that I selected first time) not the last.
SelectionChanged event is fired every time you select or deselect one item in ListView. So you can keep this one in separate property, then deselect everything and set this one as selected.
Don't forget to check if item was selected and not deselected.

Setting the GridView Selected item to none

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.

How do I check if a selected item has changed from the previously selected item?

I have a listbox in my winform, when an item in the listbox is selected, a value is placed into a textbox in the same Form. There my many items in my listbox which when selected, i want the text box to be empty so i can pass in the new value. How do i check is if the user has clicked on something other their initial selected item? i get the currently selected item like this below:
var selectedItem = (ReportItems)listbox.selectedItem
You can use the SelectedIndexChanged event on your ListBox . You can create an event handler for this event to determine when the selected index in the ListBox has been changed. This can be useful when you need to display information in other controls based on the current selection in the ListBox. You can use the event handler for this event to load the information in the other controls.
See MSDN documentation: link
You can add a global variable for your ReportItems and call it 'selItem'.
After the user changed the selected Item you check the "new" selectedItem with the 'selItem'-variable.. i don't think that a listbox has a method that can check if the selection has changed from the previous one..
I'm not sure if there is a reason you're not leveraging the SelectionChanged event of the ListBox or not but you should be if you're not. Further, determining if it's different than the initially selected item should be pretty straight forward because you can save the initially selected item in a private variable in your form and compare every time the method handler for SelectionChanged fires.
Other than this, there's not much more I can suggest because your questions isn't terribly clear and there is no code to look at.
My solution was to always clear the textbox first. So as soon as a user selects an item in the listview, rather than populating the textbox straight away, clear the textbox before populating it.
clearText(); is called soon as a listbox item is clicked.
public void clearText()
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
textBox7.Clear();
textBox8.Clear();
textBox9.Clear();
textBox10.Clear();
textBox11.Clear();
textBox12.Clear();
}

ListBox.SelectedItem is null notification

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?

C# Databinding SelectedIndexChanged call

I am using a SelectedIndexChanged function for a combobox to update the content of my DataGridview item. I have the combobox data bound to keep track of it's currently selected record. However, when I changed the combobox index it updates the datagridview as if the selected value was the same. This means once I have selected a different index value in the combobox, I have to select it again to run the function with the proper value.
Am I missing something here? Do I need to call the check for the current datarow selected in the combobox before calling the SelectedIndexChanged function?
Sounds like the page life cycle issue, as the datagrid will be fired before the slected index change if its in the page load event. at which point the index wont have changed so the data will be the same.

Categories