Reputation: 2380
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
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