On selecting the firstitem in the list box my Up button should be disabled. similarly on selecting the Lastitem in the listbox Down button should be disabled. I can able to find which item is selected by using selected index
if (lstview.SelectedIndex >= 0)
{
var selectedItems = lstview.SelectedItems;
foreach (ClassName selectedItem in selectedItems)
{
lstview.Items.Remove(selectedItem);
break;
}
}
but how to disable the up and down button based on selecting first item or last item in the list box
Compare the selected index with 0, and the number of items in the list and set the Enabled property of your buttons based on the selected index.
private void lstView_SelectedIndexChanged(object sender, System.EventArgs e)
{
upButton.Enabled = lstView.SelectedIndex > 0;
downButton.Enabled = lstView.SelectedIndex < lstView.Items.Count - 1;
}
Related
I would like to remove items from a ListBox and set the selected index to the next item.
<ListBox x:Name="lstBox" KeyDown="lstBox_KeyDown">
<ListBoxItem>A</ListBoxItem>
<ListBoxItem>B</ListBoxItem>
<ListBoxItem>C</ListBoxItem>
<ListBoxItem>D</ListBoxItem>
<ListBoxItem>E</ListBoxItem>
</ListBox>
This code works as intended unless i use the arrow keys. If i delete "B" for instance the next selected item is "C". However using cursor down will select the first item "A" instead of "D".
private void lstBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
if (lstBox.SelectedIndex == -1)
return;
int currentIndex = lstBox.SelectedIndex;
int newIndex = lstBox.SelectedIndex;
//in case the last item was deleted
if (newIndex == lstBox.Items.Count - 1)
newIndex--;
lstBox.Items.RemoveAt(currentIndex);
lstBox.SelectedIndex = newIndex;
}
}
I have already tried to set the focus to the ListBox after setting the new index. But it doesn't help.
lstBox.SelectedIndex = newIndex;
lstBox.Focus();
How can i fix that?
This issue is due to the fact that when you are removing one item, ListBox loses the focus.
So in order to make your arrow keys work, you will also have to set the focus on Selected Item of the Listbox
<ListBox x:Name="lstBox" KeyDown="lstBox_KeyDown" SelectionChanged="LstBox_OnSelectionChanged">
<ListBoxItem>A</ListBoxItem>
<ListBoxItem>B</ListBoxItem>
<ListBoxItem>C</ListBoxItem>
<ListBoxItem>D</ListBoxItem>
<ListBoxItem>E</ListBoxItem>
</ListBox>
private void LstBox_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = (ListBoxItem)lstBox.ItemContainerGenerator.ContainerFromItem(lstBox.SelectedItem);
if (item != null)
item.Focus();
}
I have a list view with the multiselect property set to false. When the user clicks on it, I take the NAME property of the list view item and convert it to a decimal then feed that to a method that loads the correct record.
The code below works perfectly when I select one item regardless of how many items are in the list and regardless of which item I select.
private void ListInstruments_SelectedIndexChanged(object sender, EventArgs e)
{
ListViewItem selection = listInstruments.SelectedItems[0];
if (selection != null)
{
string strSelection = selection.Name;
SelectedInstrumentID = Convert.ToDecimal(strSelection);
LoadSelectedInstrument();
}
}
When I make a second selection (not multi-select, but a different selection from the listbox) I get an error referencing listInstruments.SelectedItems[0].
System.ArgumentOutOfRangeException Message=InvalidArgument=Value of
'0' is not valid for 'index'. Parameter name: index
Source=System.Windows.Forms
Any help would be appreciated.
It's possible, that no items are selected, and thus list.SelectedItems is empty; you are tring to get 0th item from the empty collection and thus have the exception thrown. The quick patch is
// instead of original
// ListViewItem selection = listInstruments.SelectedItems[0];
ListViewItem selection = list.SelectedItems.Count > 0
? listInstruments.SelectedItems[0] // the collection has at least one item
: null; // if the collection is empty
Or we can check if we have a selection and return when there's none
private void ListInstruments_SelectedIndexChanged(object sender, EventArgs e)
{
if (list.SelectedItems.Count <= 0)
return;
listViewItem selection = listInstruments.SelectedItems[0];
string strSelection = selection.Name;
SelectedInstrumentID = Convert.ToDecimal(strSelection);
LoadSelectedInstrument();
}
I have a Listview that contains Comboboxes, a textbox, and a check box. I can add multiple rows, and each time a row is added you get a new row in the list view that contains all 5 of those items. I have a default prepended to the beginning of each of the Comboboxes to prompt the user to select a value from the dropdownlist that it is bound to. The problem is when I try to reorder the rows the move event resets all the values to their default values, clears out the textbox and unchecks the check box.
public ObservableCollection<RemovePunctuationRules> PunctuationRules
{
get
{
return _punctuationRules ?? (_punctuationRules = new ObservableCollection<RemovePunctuationRules>());
}
set
{
_punctuationRules = value;
}
}
private void MenuItemMoveUp_OnClick(object sender, RoutedEventArgs e)
{
var selectedIndex = ListView.SelectedIndex;
if (ListView.SelectedIndex > 0)
{
var itemToMoveUp = PunctuationRules[selectedIndex];
PunctuationRules.RemoveAt(selectedIndex);
PunctuationRules.Insert(selectedIndex - 1, itemToMoveUp);
ListView.SelectedIndex = selectedIndex - 1;
}
}
i have 2 listBoxes in a window, one next to the other, with buttons to copy items from one listBox to the other.
when an item from the first listBox is selected the copy button gets enabled, and remove button gets disabled. when i choose an item for the second listBox the copy button gets disabled, and remove button gets enabled.
when you select an item in one of the listBoxes the buttons change with no problem, after the listBox lost focus and you choose the same item that was selected the buttons dont change back.
i understand the problem is that the event of selected item changed will not fire, beacuse the selected item did not change.
setting the selected item to null when the listBox loses focus was not usefull beacuse i need the selcted item. i need to find a way to reselect the selected item when the listBox gains focus, or just fire the even of selected item changed. any suggestions?
You can try the ListBox.LostFocus Event and set the SelectedItem Property to null.
private void ListBox_LostFocus(object sender, RoutedEventArgs e)
{
((ListBox)sender).SelectedItem = null;
}
Use the ListBox.GotFocus event check if there is a SelectedItem, store the index, remove the SelectedItem and use the stored index to reset the SelectedItem. Something like this
private void ListBox_GotFocus(object sender, RoutedEventArgs e)
{
ListBox lb = (ListBox)sender;
if(lb.SelectedItem != null )
{
int index = lb.SelectedIndex;
lb.SelectedItem = null;
lb.SelectedIndex = index;
}
}
I have a command that delete the selected item in listBox. How I will change the selection on the other item of listbox after executing the delete command?
under button command, write this, it will delete the selected item in listview.
listBox1.Items.Remove(listBox1.SelectedItem);
Actually what you are trying to say is not complete,but what i am getting is that you want to delete currently selected item in ListBox;you can do it this way;
int index=0; //Set its value corresponding to item you want to delete.
listBox1.Items.RemoveAt(index); //This will remove selected item.
Now if you want the next item to be selected as soon as this one's removed,add this;
if(index!=-1)
{
int nextindex=index+1; //Calculates the index of next item.
listBox1.SelectedIndex=nextindex; //Selects next item.
}
private void button1_Click(object sender, EventArgs e) {
int i = listBox1.SelectedIndex;
if(i > -1) {
listBox1.Items.RemoveAt(i);
if(listBox1.Items.Count > 0)
listBox1.SelectedIndex = i < listBox1.Items.Count ? i : listBox1.Items.Count - 1;
}
}