Essentially, my problem is that I have a program that dynamically creates controls. The Controls included are buttons and on click I want the buttons to remove themselves and all related data. The problem is I'm not sure how to identify the buttons from within the delete method.
The important part of the code is this :
...
public void loadEntries()
{
TextReader input;
try
{
input = new StreamReader("data.txt");
}
catch (IOException) //file does not exist, can not load previous entries.
{
return;
}
data = new Entry[Int32.Parse(input.ReadLine())];
Label[] list = new Label[data.Length];
Button[] del = new Button[data.Length];
for (int i = 0; i < data.Length; i++)
{
data[i] = new Entry();
list[i] = new Label();
del[i] = new Button();
data[i].name = input.ReadLine();
list[i].Left = 0;
list[i].Top = i * 25;
list[i].Text = data[i].name;
del[i].Top = i * 25;
del[i].Left = 100;
del[i].Text = "delete";
del[i].Click += new EventHandler(delete);
splitContainer1.Panel2.Controls.Add(list[i]);
splitContainer1.Panel2.Controls.Add(del[i]);
}
}
...
private void delete(object sender, EventArgs e)
{
/*Essentially this method needs to delete(dispose if you want to be technical) its sender; however, the programmer doesn't know which Control calls this eventhandler*/
}
I'm sorry if this is a simple problem.