Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase or decrease the tag value in C#?

Tags:

c#

I have dynamically added controls to my flowLayoutPanel and I'm offering the user to choose which pair of label and richTextBox does he wants to delete (label text is simply 1.,2.,3.,...) and tags are just numbers(1,2,3,...). This is how I did deleting the controls:

pairToDelete = Convert.ToInt32(textBox.Text);
foreach (Control ctrl in flowLayoutPanel1.Controls.OfType<Label>())
{
    if (ctrl.Tag.ToString() == pairToDelete.ToString())
    {
         Controls.Remove(ctrl);
        ctrl.Dispose();
    }
}

foreach (Control ctrl in flowLayoutPanel1.Controls.OfType<RichTextBox>())
{
    if (ctrl.Tag.ToString() == pairToDelete.ToString())
    {
        Controls.Remove(ctrl);
        ctrl.Dispose();
    }
}

Now, what I want is to change the tags of next pairs of controls. For example, if the user wants to delete 2nd pair of label and RTBox then I want to change tags of label3 and RTBox3 from 3 to 2, tags of label4 and RTBox4 from 4 to 3 etc. How can I do this?

like image 232
NutCracker Avatar asked Jun 28 '26 00:06

NutCracker


2 Answers

I modified a little bit the mechanism to find a control to remove. After that I removing the control the way you remove it. After that I'm lowering the tag number of any control that has the tag higher then the removed control. Assumption is that the tags are numbers. I leave for you to do appropriate checks.

public void Delete()
{
    var pairToDelete = Convert.ToInt32(textBox1.Text);

    // Find what to remove.
    var lblToDelete = this.Controls.OfType<Label>()
            .FirstOrDefault(l => l.Tag.ToString() == pairToDelete.ToString());
    var txtToDelete = this.Controls.OfType<RichTextBox>()
            .FirstOrDefault(c => c.Tag.ToString() == pairToDelete.ToString());

    // Can be removed?
    if (lblToDelete != null)
    {
        // Remove.
        this.Controls.Remove(lblToDelete);
        lblToDelete.Dispose();

        // Lower tag number for labels with tag higher then the removed one.
        foreach (var c in this.Controls.OfType<Label>()
            .Where(l => Convert.ToInt32(l.Tag) > pairToDelete))
        {
            var newTag = Convert.ToInt32(c.Tag) - 1;
            c.Tag = newTag;
        }
    }

    // Can be removed?
    if (txtToDelete != null)
    {
        // Remove.
        this.Controls.Remove(txtToDelete);
        txtToDelete.Dispose();

        // Lower tag number for rich textvbox with tag higher then the removed one.
        foreach (var c in this.Controls.OfType<RichTextBox>()
                .Where(r => Convert.ToInt32(r.Tag) > pairToDelete))
            {
                var newTag = Convert.ToInt32(c.Tag) - 1;
                c.Tag = newTag;
            }
    }
}
like image 118
PiotrWolkowski Avatar answered Jun 29 '26 14:06

PiotrWolkowski


In these situations where different controls are related, I prefer to rely on an additional storage (on top of the Controls collection) to ease all the actions among controls. For example, a class like the following one:

public class LabelRTB
{
    public Label label;
    public RichTextBox rtb;

    public LabelRTB(Label label_arg, RichTextBox rtb_arg)
    {
        label = label_arg;
        rtb = rtb_arg;
    }
}

It can be populated on form load:

List<LabelRTB> allLabelRTB = new List<LabelRTB>();
allLabelRTB.Add(new LabelRTB(label1, rtb1));
allLabelRTB.Add(new LabelRTB(label2, rtb2));

this.Tag = allLabelRTB;

The requested deletion is now straightforward and the indices are updated automatically. For example:

private void removeItem(int index)
{
    LabelRTB curItem = ((List<LabelRTB>)this.Tag)[index];

    Controls.Remove(curItem.label);
    Controls.Remove(curItem.rtb);

    ((List<LabelRTB>)this.Tag).Remove(curItem);
}

As far as the number of controls is quite limited and the complexity of the associated actions might be reduced notably, this a-priori-less-efficient approach (it stores the same information twice) might even make the application more efficient than in case of exclusively relying on Controls.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!