C#我不明白为什么这个按钮不会删除

时间:2016-06-21 20:27:25

标签: c# winforms flowlayout

基本上...当用户接受或拒绝朋友请求时,它应该删除用户的名字,接受和拒绝按钮,但它只删除用户的名字和拒绝按钮。我不明白。代码:

        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

为什么不删除“接受”按钮?

1 个答案:

答案 0 :(得分: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();
}