I have a listbox that will update upon each button click as well as select the next item in the list. At the moment, it places the selected index into a textbox and gathers that number from the textbox once the list has repopulated (files within the listbox come from a directory). My issue is that when a file towards the bottom is selected and a file is deleted from the directory, it cannot go to the next index. Example : If there are 8 indexes, and one is deleted, while the button is being clicked to go to that 8th index, I will get a message saying: "'InvalidArgument=Value of '8' is not valid for 'SelectedIndex'. Parameter name: SelectedIndex'.
private void NextBtn_Click(object sender, EventArgs e)
{
Playlist.Update();
if (Playlist.Items.Count == 0)
{
MessageBox.Show("There is no content available for display.", "Error");
Application.Exit();
}
else
{
NextBtn.Enabled = false;
Playlist.SelectedIndex = (Playlist.SelectedIndex + 1) % Playlist.Items.Count;
IndexTextBox.Text = Playlist.SelectedIndex.ToString();
int indexval = int.Parse(IndexTextBox.Text);
Repopulate();
Playlist.SelectedIndex = indexval;
}
Playlist.EndUpdate();
}