更新标签

时间:2013-11-22 21:07:03

标签: .net visual-studio-2008 textbox label updates

我需要的是我的标签显示了5个文本框上写入的内容。

我尝试在更改文本框值时执行此操作,但由于其中有5个,因此需要大量重复代码。

有没有办法自行更新标签?如何?

1 个答案:

答案 0 :(得分:1)

您可以在文本框的TextChanged事件中注册相同的方法:

public FooControl()
        {
            InitializeComponent();

            textBox1.TextChanged += textBoxes_TextChanged;
            textBox2.TextChanged += textBoxes_TextChanged;
            textBox3.TextChanged += textBoxes_TextChanged;
            textBox4.TextChanged += textBoxes_TextChanged;
            textBox5.TextChanged += textBoxes_TextChanged;
        }

        void textBoxes_TextChanged(object sender, TextChangedEventArgs e)
        {
            label1.Content = ...
        }

或:

 var sumBoxes = new List<TextBox> { textBox1, textBox2, textBox3, textbox4, textbox5 };
           sumBoxes.ForEach(i => i.TextChanged += sumBoxes_TextChanged);
相关问题