C# 我不明白为什么这个按钮不会删除 [英] C# I don't understand why this button won't delete

查看:25
本文介绍了C# 我不明白为什么这个按钮不会删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

basically... When the user accepts or rejects a friend request it is supposed to remove the user's name, accept and reject button but it only removes the user's name and reject button. I don't understand. Code:

        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

When any of the buttons are clicked: GUI

为什么不删除接受"按钮?

Why isn't it deleting the 'Accept' button?

推荐答案

您正在循环期间更改集合.为了解决这个问题,您可以在您找到控件的条件的末尾调用 ToList 并循环遍历结果.这样,您将循环访问与要更改的集合不同的列表:

You are changing the collection during the loop. To solve the problem, you can call ToList at the end of the criteria which you find controls, and loop over the result. This way, you are looping through a different list than the collection you want to change:

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();
}

这篇关于C# 我不明白为什么这个按钮不会删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆