2014-03-31 58 views
-1

當我選擇該項目時,網格完美工作,但是...當我按左鍵選擇其他練習時,應用程序崩潰。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; 
     } 


    } 
+0

請你也表明了'ButtonClick'事件處理程序代碼? –

+1

處理異常並顯示錯誤消息。 –

+0

你可以簡化你的代碼,通過插入你的列表到一個數組列表 [] Exercises = new List [4];''Exercises [0] = new List ();' –

回答

2

如果沒有選擇項,gvSpecialPagesViewExercise.SelectedIndex將爲-1是用於ElementAt一個無效值。

開關前添加一個處理程序,以防止例外:

if(gvSpecialPagesViewExercise.SelectedIndex <= 0) 
    return; 
+0

是的,這正是我-1。 – Mikasuki

+0

完美的工作很好謝謝斯坦利! – Mikasuki

0

雖然不回答你的問題(顯然你發現同時的bug),這裏有幾個技巧:

您的代碼看起來很因爲你正在單獨處理四個列表。如果你把它們放在一個數組中,你可以在全局處理它們,從而簡化你的代碼。

List<BookExercises>[] exercises = new List<BookExercises>[4]; 

private void UpdateLeftButtonContent() 
{ 
    ResetSpecialPagesButtonColors(); 
    if (currentPageSettings.PageStyle.Contains("Especiales")) { 
     for (int i = 0; i < exercises.Length; i++) { 
      exercises[i] = new List<BookExercises>(); 
     } 
     foreach (BookExercises x in lstCurrentPageBookExercises) { 
      if (x.AssignmentCode == 0 && x.Id >= 0 && x.Id < exercises.Length) { 
       exercises[x.Id].Add(x); 
      } 
     } 
     ... 
    } 
} 

int specialbtnId = 0; 
private void gvSpecialPagesViewExercise_SelectionChanged(object sender, 
    SelectionChangedEventArgs e) 
{ 
    // If you are sure that this condition is always true, you can drop it. 
    if (specialbtnId >= 0 && specialbtnId < exercises.Length) { 
     BookExercises lst = exercises[specialbtnId]; 
     int index = gvSpecialPagesViewExercise.SelectedIndex; 
     if (index >= 0 && index < lst.Count) { 
      LoadSpecialPagesBookExcersices(lst[index]); 
     } 
    } 
}