0

I'm having a little trouble with my program. I'm currently programming a friend request script and basically when the user receives a friend request, it will automatically add it to a FlowLayoutPanel and in each friend request, the user's name will show along with a accept and reject button. Here's the code:

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

What it looks like: GUI

Now the problem is, when the user either accepts or rejects a request, it seems to delete all of the labels and buttons. Code:

friendRequestPanel.Controls.Remove(UserName); // Label
friendRequestPanel.Controls.Remove(accept); // Button
friendRequestPanel.Controls.Remove(rejects); // Button

How would I assign an id to each button and label so I can remove it later on? Something like: friendRequestPanel.Controls.Remove(username[ID No]);

Draken
  • 3,134
  • 13
  • 34
  • 54
richardj97
  • 79
  • 1
  • 10
  • You can store redundantly your references in a Dictionary, and when you want to remove it, obtain the reference from the dictionary to request for the appropriate instance. Obviously, don't forget to remove it from the dictionary too. – wOOdy... Jun 21 '16 at 16:27
  • If you just want to have an ID, you can use the Tag property, and look up for the appropriate in friendRequestPanel.Controls to obtain the reference you want to remove. – wOOdy... Jun 21 '16 at 16:40
  • [`Name`](https://msdn.microsoft.com/en-us/library/system.windows.forms.control.name(v=vs.110).aspx) property is for such usage. You can assign a name to control and then find it in `Controls` collection of its parent using that name. – Reza Aghaei Jun 21 '16 at 16:51
  • Hi guys could you show me an example? Nothing seems to work... Thanks – richardj97 Jun 21 '16 at 17:21

2 Answers2

0

You should create a control for those buttons and create them dynamically. Store them into a List<MyControl> and remove the controls when needed. Because you'd like to 'group' them.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0

I've changed your code a bit in order to simplify the code. Hope you find it useful.

        string[] tags = new string[] { "A", "B", "C" };

        foreach (string request in tags)
        {
            Label userName = new Label();
            userName.Name = request;
            Button accept = new Button();
            accept.Name = request;

            Button reject = new Button();
            reject.Name = request;

            userName.Text = request;
            accept.Text = "Accept";
            reject.Text = "Reject";
            friendRequestPanel.Controls.Add(userName);
            friendRequestPanel.Controls.Add(accept);
            friendRequestPanel.Controls.Add(reject);
        }

        // Just remove the "B"s
        friendRequestPanel.Controls.RemoveByKey("B");

Obviously, this is not the best approach for what you want to do. But solves your problem.

wOOdy...
  • 659
  • 13
  • 25
  • How would I use int instead of string (Tags)? – richardj97 Jun 21 '16 at 18:00
  • You can't. Name is a String property. but you always can call integer.ToString() method to convert your integer in a string. Obviously, don't forget to convert the int you are looking for to string before try to remove it. – wOOdy... Jun 21 '16 at 18:20