當我選擇該項目時,網格完美工作,但是...當我按左鍵選擇其他練習時,應用程序崩潰。C#選擇更改索引超出範圍
這是代碼:
int specialbtnId = 0;
private void gvSpecialPagesViewExercise_SelectionChanged(object sender,
SelectionChangedEventArgs e)
{
switch (specialbtnId)
{
case 0:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton1Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 1:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton2Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 2:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton3Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
case 3:
LoadSpecialPagesBookExcersices(lstCurrentPageLeftButton4Excersices
.ElementAt(gvSpecialPagesViewExercise.SelectedIndex));
break;
}
}
這是其他發泄按鈕,唯一的代碼即時通訊使用現在改變鍛鍊的鍛鍊和部位。
private void UpdateLeftButtonContent()
{
ResetSpecialPagesButtonColors();
if (currentPageSettings.PageStyle.Contains("Especiales"))
{
if (lstCurrentPageLeftButton1Excersices != null)
lstCurrentPageLeftButton1Excersices.Clear();
if (lstCurrentPageLeftButton2Excersices != null)
lstCurrentPageLeftButton2Excersices.Clear();
if (lstCurrentPageLeftButton3Excersices != null)
lstCurrentPageLeftButton3Excersices.Clear();
if (lstCurrentPageLeftButton4Excersices != null)
lstCurrentPageLeftButton4Excersices.Clear();
foreach (BookExercises x in lstCurrentPageBookExercises)
{
if (x.AssignmentCode == 0 && x.Id == 0)
{
lstCurrentPageLeftButton1Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 1)
{
lstCurrentPageLeftButton2Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 2)
{
lstCurrentPageLeftButton3Excersices.Add(x);
}
if (x.AssignmentCode == 0 && x.Id == 3)
{
lstCurrentPageLeftButton4Excersices.Add(x);
}
}
if (lstCurrentPageLeftButton1Excersices.Count > 0)
{
btn1SpecialPages.DataContext = lstCurrentPageLeftButton1Excersices.ElementAt(0);
}
if (lstCurrentPageLeftButton2Excersices.Count > 0)
{
btn2SpecialPages.DataContext = lstCurrentPageLeftButton2Excersices.ElementAt(0);
btn2SpecialPages.Visibility = Visibility.Visible;
btn2Line.Visibility = Visibility.Visible;
}
else
{
btn2SpecialPages.Visibility = Visibility.Collapsed;
btn3SpecialPages.Visibility = Visibility.Collapsed;
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn2Line.Visibility = Visibility.Collapsed;
btn3line.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
if (lstCurrentPageLeftButton3Excersices.Count > 0)
{
btn3SpecialPages.DataContext = lstCurrentPageLeftButton3Excersices.ElementAt(0);
btn3SpecialPages.Visibility = Visibility.Visible;
btn3line.Visibility = Visibility.Visible;
}
else
{
btn3SpecialPages.Visibility = Visibility.Collapsed;
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn3line.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
if (lstCurrentPageLeftButton4Excersices.Count > 0)
{
btn4SpecialPages.DataContext = lstCurrentPageLeftButton4Excersices.ElementAt(0);
btn4SpecialPages.Visibility = Visibility.Visible;
btn4line.Visibility = Visibility.Visible;
}
else
{
btn4SpecialPages.Visibility = Visibility.Collapsed;
btn4line.Visibility = Visibility.Collapsed;
}
GridSectionTitle.DataContext = null;
GridSectionTitle.DataContext = currentPageSettings;
}
}
請你也表明了'ButtonClick'事件處理程序代碼? –
處理異常並顯示錯誤消息。 –
你可以簡化你的代碼,通過插入你的列表到一個數組列表 [] Exercises = new List [4];''Exercises [0] = new List ();' –