beny lim
beny lim

Reputation: 1304

Get the selected value of listbox and reset the selected index value in C# windows phone 7

I am trying to get the value of the selected item and store it into a variable.

And then reset the selected index of the listbox to -1 so that when i navigate back to that page the listbox will not show anything is selected previously.

Below is my code:

But when the selected index is reset to -1 it will keep have an error at sortedTimeListBox.Items[selectedIndexOfSchedule].ToString(); because the selectedIndexOfSchedule have become to -1.

What i want is to just get the value and navigate to next page. And the index of -1 is just to reset the selected value of the listbox.

How should i do it?

private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
        //Get the value of selected index in scheduleListBox
        int selectedIndexOfSchedule = scheduleListBox.SelectedIndex;

        if (sortedSelectedValue.Text == "")
        {
            string selectedValueText = sortedTimeListBox.Items[selectedIndexOfSchedule].ToString();
            MessageBox.Show("selectedValueText : " + sortedSelectedValue.Text);
        }
        else
        {
            MessageBox.Show("Empty");
        }

        NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + fullFolderName + "&passToDelete=" + selectedFolderName, UriKind.Relative));
        scheduleListBox.SelectedIndex = -1;

}

Upvotes: 1

Views: 2482

Answers (1)

keyboardP
keyboardP

Reputation: 69372

You could just add a check to see if the value is -1.

private void scheduleListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    //Get the value of selected index in scheduleListBox
    int selectedIndexOfSchedule = scheduleListBox.SelectedIndex;

    if(selectedIndexOfSchedule != -1)
    {
        if (sortedSelectedValue.Text == "")
        {
            string selectedValueText = sortedTimeListBox.Items[selectedIndexOfSchedule].ToString();
            MessageBox.Show("selectedValueText : " + sortedSelectedValue.Text);
        }
        else
        {
            MessageBox.Show("Empty");
        }

        NavigationService.Navigate(new Uri("/ViewScheduleDetails.xaml?selectedIndexOfSchedule=" + selectedIndexOfSchedule + "&selectedFolderName1=" + fullFolderName + "&passToDelete=" + selectedFolderName, UriKind.Relative));
        scheduleListBox.SelectedIndex = -1;
    }

}

Upvotes: 2

Related Questions