如何在C#中增加或减少标签值?

时间:2015-12-19 16:34:50

标签: c#

我已经动态地向我的flowLayoutPanel添加了控件,并且我正在为用户提供他想要删除哪一对label和richTextBox(标签文本只是1.,2.,3 ......,...)和标签只是数字(1,2,3,...)。这就是我删除控件的方式:

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();
    }
}

现在,我想要的是更改下一对控件的标签。例如,如果用户想要删除第二对标签和RTBox,那么我想将label3和RTBox3的标签从3更改为2,将label4和RTBox4的标签从4更改为3等。我该怎么做?

3 个答案:

答案 0 :(得分:2)

我修改了一点机制来找到要删除的控件。之后,我以删除它的方式删除控件。之后,我降低了标签高于删除控件的任何控件的标签号。假设标签是数字。我请你去做适当的检查。

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;
            }
    }
}

答案 1 :(得分:2)

在这些不同控件相关的情况下,我更喜欢依赖额外的存储(在Controls集合之上)来简化控件之间的所有操作。例如,类似下面的类:

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

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

可以在表单加载时填充:

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

this.Tag = allLabelRTB;

请求的删除现在很简单,索引会自动更新。例如:

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);
}

只要控制的数量非常有限并且相关操作的复杂性可能会显着降低,这种先验效率低的方法(它将相同的信息存储两次)甚至可能使应用程序的效率高于如果完全依赖Controls

答案 2 :(得分:0)

这也是解决方案之一:

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

        if (Convert.ToInt32(ctrl.Tag) > pairToDelete)
        {
               int decrease = Convert.ToInt32(ctrl.Tag) - 1;
               ctrl.Tag = decrease;

               ctrl.Text = decrease + ".";
        }
 }

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

        if (Convert.ToInt32(ctrl.Tag) > pairToDelete)
        {
               int decrease = Convert.ToInt32(ctrl.Tag) - 1;
               ctrl.Tag = decrease;
        }
 }