roncansan
roncansan

Reputation: 2380

How to select all text boxes with a specific tag

Using c# 6 windows forms, I want to choose all textBoxes with the tag "txt". I have confirm that there are several of them with the tag "txt". Then, I want to clear the text. But the following code is not choosing any.

this.Controls.OfType<TextBox>()
             .Where(textBox => textBox.Tag.ToString() == "txt").ToList()
             .ForEach(textBox => textBox.Clear());

Upvotes: 2

Views: 380

Answers (1)

Joseph Wu
Joseph Wu

Reputation: 5018

your code almost work, use this:

        this.Controls.OfType<TextBox>()
            .Where(text =>!(text.Tag == null) && text.Tag.ToString() == "txt").ToList()
            .ForEach(text => text.Clear());

Upvotes: 3

Related Questions