user3179585
user3179585

Reputation: 123

How To Set All Text And Text Color Labels In A GroupBox?

How do you reset the Text and ForeColor of Labels in a GroupBox to a custom or default value?

enter image description here

Upvotes: 0

Views: 342

Answers (1)

user3179585
user3179585

Reputation: 123

The easiest way is to create a function that looks at each control in the groupbox and set it's value. Notice, you can modify any control in the groupbox.

        private void ResetLabelText(Control control)
        {
            List<Label> lbls = groupBox1.Controls.OfType<Label>().ToList();
            foreach (var lbl in lbls)
            {
                lbl.Text = "...";
                lbl.ForeColor = default(Color);
            }
        }

The function can then be called from a button click like so.

        private void button4_Click_1(object sender, EventArgs e)
        {
            ResetLabelText(groupBox1);
        }

enter image description here

Upvotes: 0

Related Questions