I want to create two buttons in a Windows Form Application using C# which adds a label every time it is pressed and another which removes the most recently added label from the form. I know of two methods to do this however, the buttons won't keep track of the most recently added or deleted labels.
The two methods I know of are:
//I used a input box here just for an example,
//however I will most likely use this is my final code.
//it can be swapped for a textbox input
label1.Text = "";
label2.Text = "";
private void btnAddNew_Click(object sender, EventArgs e)
string input1 = Interaction.InputBox = "Type contents of label:"
label1.Text = input1;
This example has two faults I can see, the first being that it will take a lot of space and inefficient code to keep making the label text null so it doesn't show also, as the problem previously stated, every time the button is clicked it will just change the text to the same label though, this example may be the simpler of the two.
Example two includes the method of just manually adding and removing a label including size, position, name and contents, and therefore there won't be any potentially unused/ unwanted labels although I can't add a label based on how many there currently are or remove the most previous label.
If anyone knows how to add labels based on the last added label, e.g.
label2.Position = label1.Position + 50,50;
Then remove a label depending on the most previously added one please share!