c#如何创建按钮并稍后通过ID将其删除 [英] c# How to create buttons and delete them later by an ID

查看:160
本文介绍了c#如何创建按钮并稍后通过ID将其删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力创建此脚本,以在该脚本中生成好友请求.我需要为每个按钮和标签专门添加一个ID,以便当用户单击接受"或拒绝"按钮时可以将其删除.

I'm struggling to create this script where it generates friend requests. I need to specifically add an id to each button and label so I can remove it when the user clicks either the accept or reject button.

Button reqAccept;
Button reqReject;
Label reqUserName;

private void loadFriendRequests()
{
    using (SqlConnection connection = new SqlConnection(con))
    {
        using (SqlCommand cmd = new SqlCommand("Select UserFirstName, UserLastName, FriendEmail From PendingRequests Where FriendEmail = @fe", connection))
        {
            connection.Open();
            cmd.Parameters.AddWithValue("@fe", Properties.Settings.Default.Email);
            using (SqlDataReader dr = cmd.ExecuteReader())
            {
                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.Click += Accept_Click;
                        reject.Click += Reject_Click;

                        userName.Text = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(dr["UserFirstName"].ToString() + " " + dr["UserLastName"].ToString());

                        accept.Text = "Accept";
                        reject.Text = "Reject";

                        friendRequestPanel.Controls.Add(userName);
                        friendRequestPanel.Controls.Add(accept);
                        friendRequestPanel.Controls.Add(reject);

                        reqAccept = accept;
                        reqReject = reject;
                        reqUserName = userName;
                    }
                }
            }
        }
    }
    Requests.Start();
}
private void Reject_Click(object sender, EventArgs e)
{
    friendRequestPanel.Controls.Remove(reqUserName);
    friendRequestPanel.Controls.Remove(reqAccept);
    friendRequestPanel.Controls.Remove(reqReject);

    updateFriendRequestDatabase(2);
}
private void Accept_Click(object sender, EventArgs e)
{
    friendRequestPanel.Controls.Remove(reqUserName);
    friendRequestPanel.Controls.Remove(reqAccept);
    friendRequestPanel.Controls.Remove(reqReject);

    updateFriendRequestDatabase(1);
}

代码在做什么: 上面的代码选择的请求与用户的电子邮件相同,对于每个好友请求,它将使用标签和2个按钮接受或拒绝,将其添加到"FlowLayourPanel"中.

What the code is doing: The code above is selecting requests that are the same as the user's email and for every friend request there is, it will add it to 'FlowLayourPanel' by using a label and 2 buttons to either accept or reject.

这是GUI的外观: GUI

当用户单击按钮时,它显然会转到事件处理程序,但是如何识别按下了哪个按钮?

When the user clicks a button it will obviously go to the event handler but how do I identify which button was pressed?

它必须是这样的:

friendRequestPanel.Controls.Remove(reqUserName##ID##);

推荐答案

要做的第一件事是从查询中检索唯一标识数据的值.假设您的PendingRequest表具有一个IDRequest,则您的查询可以更改为

The first thing to do is to retrieve from your query the value that uniquely identifies your data. Suppose that your PendingRequest table has an IDRequest then your query could be changed to

  using (SqlCommand cmd = new SqlCommand(@"Select IDRequest, UserFirstName, 
                      UserLastName, FriendEmail 
                      From PendingRequests 
                      Where FriendEmail = @fe", connection))

现在,当您动态创建控件时,您还要将该ID添加到该记录创建的每个控件的Tag属性中

Now when you create your controls dynamically you add also that ID to the Tag property of every control created by that record

foreach (object request in i.ToString())
{
    Label userName = new Label();
    Button accept = new Button();
    Button reject = new Button();

    int idRequest = Convert.ToInt32(dr["IDRequest"]);
    userName.Tag = idRequest;
    accept.Tag = idRequest;
    reject.Tag = idRequest;
    ....

最后,在单击事件中,您可以使用如下代码获取按钮和标签的确切实例

Finally in your click event you could retrieve the exact instance of the buttons and label using a code like this

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 => x.Tag != null &&
                                         Convert.ToInt32(x.Tag) == idRequest)
                                  .ToList();
    foreach(Control ct in ctrls)
    {
          friendRequestPanel.Controls.Remove(ct);
          ct.Dispose();
    }
    updateFriendRequestDatabase(2);
}

请注意,如果您从Controls集合中删除控件,则不要忘记处理它.

Notice that if you remove the control from the Controls collection you should not forget to Dispose it.

这篇关于c#如何创建按钮并稍后通过ID将其删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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