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);
}
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.
This is what the GUI looks like: GUI
When the user clicks a button it will obviously go to the event handler but how do I identify which button was pressed?
It needs to be something like:
friendRequestPanel.Controls.Remove(reqUserName##ID##);
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
Now when you create your controls dynamically you add also that ID to the Tag property of every control created by that record
Finally in your click event you could retrieve the exact instance of the buttons and label using a code like this
Notice that if you remove the control from the Controls collection you should not forget to Dispose it.
@Steve has answered it all well. He has already shown you how to fix your current code.
I want to suggest you a better way to structure your current code, so that it looks cleaner and you have more control over the friend request functionality. If you are interested, read further. If you are on tight deadlines then ignore :).
If I was in your place, I would create a user control to represent individual friend request and use events to notify main form in case of accept/reject so that it can remove the request from panel/list. This way main form code (designer + code behind) will become cleaner to read and understand.
FriendRequestControl
This would be standard
UserControl
which has contains 1Label
(for displaying friend's name and 2Buttons
accept and reject.Main Form