Xin chào mọi người, trong cái form của em có insert, mỗi lần nhấn button insert nó sẽ tạo một cái label mới, em muốn lưu tất cả các label đó trong List cho nó dễ xóa; có button Clear, mỗi lần nhấn nó, nó sẽ xóa các label có trong List đó. Code của em như thế này ạ:
List<Label> wholeLabelOnScreen;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) // add event
{
try
{
if (textBox1.Text != "" && textBox1.Text.Length <= 4)
{
Label lb = new System.Windows.Forms.Label();
lb = editLabel(lb, value); // Hàm này chỉ chỉnh sửa style của label
this.Controls.Add(lb);
textBox1.Text = "";
wholeLabelOnScreen.Add(lb);
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
private void button4_Click(object sender, EventArgs e) // clear event
{
foreach(Label item in wholeLabelOnScreen)
{
Controls.Remove(item);
}
wholeLabelOnScreen.Clear();
}
}
Idea của em là như vậy, nó bị lỗi và không chạy được, mọi người xem và chỉ giúp em với, em xin cảm ơn ạ!