'Find Textboxes != "" Transfer them to a label with a "," between each [closed]
I am looking for a solution I have several TexBoxes I would like to get the information when there is some and insert them in a label with a "," between each. I had thought of a solution with Foreeach :
List<TextBox> test = new List<TextBox>() { Test1, Test2, Test3, Test4, Test5, Test6 };
if (test != null)
{
test .ForEach(("," + Result.Content.ToString());
}
But he not work. any body for help me please ?
Solution 1:[1]
Use Join to join the strings and some linq
var ls = new List<TextBox>()
{
new TextBox() { Text = "test1" },
new TextBox() { Text = "test2" },
new TextBox() { Text = "test3" }
};
var concat = string.Join(",", ls.Select(x=>x.Text));
Console.WriteLine(concat);
Solution 2:[2]
You can select the contents from the textboxes where the value is not empty and then join these together with string.join. Then you will have to update the content of the label to this value. It would look something like this.
var textBoxes = new List<TextBox>() { Test1, Test2, Test3, Test4, Test5, Test6 };
var values = textBoxes.Select(x => x.Text).Where(x => !string.IsNullOrEmpty(x));
ResultLabel.Content = string.Join(',', values);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Isaac |
| Solution 2 | GuyVdN |
