基本上......當用戶接受或拒絕好友請求時,它應該刪除用戶的名稱,接受和拒絕按鈕,但它只會刪除用戶的名稱和拒絕按鈕。我不明白。代碼:C#我不明白爲什麼這個按鈕不會刪除
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
當任一按鈕被點擊:GUI
爲什麼沒有刪除 '接受' 按鈕?
你其實放置斷點在代碼和臺階的貫通,看看那裏的預期失敗..請做到這一點首先你可以不要期望我們根據您發佈的所有代碼對您進行代碼審查。謝謝 – MethodMan
「這是我的代碼,修復它」 – LaneL
同意。我問自己的第一個問題是,「OP是否調試了他自己的代碼?」您在ctrls枚舉中接受按鈕? – Amy