2016-06-21 156 views
-3

基本上......當用戶接受或拒絕好友請求時,它應該刪除用戶的名稱,接受和拒絕按鈕,但它只會刪除用戶的名稱和拒絕按鈕。我不明白。代碼:C#我不明白爲什麼這個按鈕不會刪除

 private void loadFriendRequests() 
    { 
     using (SqlConnection connection = new SqlConnection(con)) 
     { 
      using (SqlCommand cmd = new SqlCommand(@"Select IDRequest, UserFirstName, UserLastName, FriendEmail From PendingRequests Where FriendEmail = @fe", connection)) 
      { 
       connection.Open(); 
       cmd.Parameters.AddWithValue("@fe", Properties.Settings.Default.Email); 
       using (SqlDataReader dr = cmd.ExecuteReader()) 
       { 
        int i = 0; 
        while (dr.Read()) 
        { 
         i++; 
         foreach (object request in i.ToString()) 
         { 
          Label userName = new Label(); 
          Button accept = new Button(); 
          Button reject = new Button(); 
          accept.Text = "Accept"; 
          reject.Text = "Reject"; 
          int idRequest = Convert.ToInt32(dr["IDRequest"]); 
          userName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dr["UserFirstName"].ToString() + " " + dr["UserLastName"].ToString()); 
          userName.Tag = idRequest; 
          accept.Tag = idRequest; 
          reject.Tag = idRequest; 

          accept.Click += Accept_Click; 
          reject.Click += Reject_Click; 

          friendRequestPanel.Controls.Add(userName); 
          friendRequestPanel.Controls.Add(accept); 
          friendRequestPanel.Controls.Add(reject); 
         } 
        } 
       } 
      } 
     } 
     Requests.Start(); 
    } 
    private void Reject_Click(object sender, EventArgs e) 
    { 
     Button c = sender as Button; 
     int idRequest = Convert.ToInt32(c.Tag); 
     var ctrls = friendRequestPanel.Controls 
             .Cast<Control>() 
             .Where(x => 
              Convert.ToInt32(x.Tag) == idRequest); 
     foreach (Control ct in ctrls) 
     { 
      friendRequestPanel.Controls.Remove(ct); 
      ct.Dispose(); 
     } 
     updateFriendRequestDatabase(2); 
    } 
    private void Accept_Click(object sender, EventArgs e) 
    { 
     Button c = sender as Button; 
     int idRequest = Convert.ToInt32(c.Tag); 
     var ctrls = friendRequestPanel.Controls 
             .Cast<Control>() 
             .Where(x => x.Tag != null && 
              Convert.ToInt32(x.Tag) == idRequest); 
     foreach (Control ct in ctrls) 
     { 
      friendRequestPanel.Controls.Remove(ct); 
      ct.Dispose(); 
     } 
     updateFriendRequestDatabase(1); 

    } 

圖片:GUI

當任一按鈕被點擊:GUI

爲什麼沒有刪除 '接受' 按鈕?

+2

你其實放置斷點在代碼和臺階的貫通,看看那裏的預期失敗..請做到這一點首先你可以不要期望我們根據您發佈的所有代碼對您進行代碼審查。謝謝 – MethodMan

+2

「這是我的代碼,修復它」 – LaneL

+1

同意。我問自己的第一個問題是,「OP是否調試了他自己的代碼?」您在ctrls枚舉中接受按鈕? – Amy

回答

1

您在循環中更改集合。爲了解決這個問題,你可以在你找到的控制條件結尾調用ToList,並循環結果。這樣一來,你是通過不同的列表比收集循環要更改:

var ctrls = friendRequestPanel.Controls.Cast<Control>() 
           .Where(Convert.ToInt32(x.Tag) == idRequest) 
           .ToList(); //<--- Creates a new List<Control> 
foreach (Control ct in ctrls) 
{ 
    friendRequestPanel.Controls.Remove(ct); 
    ct.Dispose(); 
} 
+0

**關於OP的注意事項:**當你發佈很多與數據庫相關的代碼時,很難找到問題,因爲問題可能是因爲任何部分而沒有人可以簡單地重現問題,如果你發佈[mcve](http://stackoverflow.com/help/mcve),問題會得到更多的關注和更有效的幫助。格式良好的問題對未來的讀者會更有用:) –

+1

修復細微問題的好處 – Steve

+0

謝謝Reze Aghaei,我花了幾個小時試圖修復它,我嘗試了'ToList()',但它沒有工作,但它現在工作:)謝謝很多人。 – richardj97

相關問題