Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

winforms - C# I don't understand why this button won't delete

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

    }

Picture: GUI

When any of the buttons are clicked: GUI

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...