2

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!

2
  • Use a data structure like a list or queue. Commented Jul 3, 2015 at 1:00
  • You can use the Stack data structure (first in first out) to get the last added label. Commented Jul 3, 2015 at 1:16

2 Answers 2

2

Just add the Labels to a FlowLayoutPanel and let it take care of positioning them automatically for you. You can remove the last one by using Controls.RemoveAt():

    private void btnAdd_Click(object sender, EventArgs e)
    {
        string input1 = Interaction.InputBox("Type contents of label:");
        Label lbl = new Label();
        lbl.Text = input1;
        lbl.AutoSize = true;
        flowLayoutPanel1.Controls.Add(lbl);
    }

    private void btnRemove_Click(object sender, EventArgs e)
    {
        if (flowLayoutPanel1.Controls.Count > 0)
        {
            flowLayoutPanel1.Controls.RemoveAt(flowLayoutPanel1.Controls.Count - 1);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

That's what I was looking for and should fit nicely into my program, thank you!
1

You need some kind of collection to store your labels.

You could use a List<Label>, but since you're especially interested in the label that you last added to the list, it sounds like you want a Stack<Label>.

A stack makes it easy (and fast) to retrieve/remove the most recently added item.

So you'd have something like this:

Stack<Label> _labels = new Stack<Label>();

private void btnAddNew_Click(object sender, EventArgs e)
{
    var label = new Label();
    label.Text = "....";    // etc
    if (_labels.Any())
    {
        // _labels.Peek() gets the most recently added label,
        // without removing it from the stack.
        var lastLabel = _labels.Peek();
        // set position based on previous
        label.Left = lastLabel.Left + 50;            
        label.Top = lastLabel.Top + 50; 
    }
    else {
        // this is the first label - set start position manually
        label.Left = 50;
        label.Top = 50;
    }
    // add to stack and form
    _labels.Push(label);
    this.Controls.Add(label);
}

private void btnRemove_Click(object sender, EventArgs e)
{
    if (!labels.Any()) {
        // there are no labels!
        return;
    }
    // _labels.Pop() returns the most recently added label,
    // and removes it from the stack.
    var label = _labels.Pop();
    // remove it from the form too
    this.Controls.Remove(label);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.