1

I want the following code to give me the exact number of S values from the textboxes named : Box1_1 , Box1_2 , Box1_3, Box1_4, Box1_5 ...

But when i try to see the value it's always blank. What can i do ?

for (int i = 1; i <= 7; i++){
    for (int j = 1; j <= 10; j++){
        string box = "Box" + i.ToString() + "_" + j.ToString();
        TextBox nameBox = new TextBox();
        nameBox.Name = box;
        if(string.Compare(nameBox.Text, "S")==0){
            numberS++;
        }
    }
}
ReD
  • 13
  • 4
  • 1
    If I guessed your question correctly then you can find your answer here https://stackoverflow.com/questions/4483912/find-a-control-in-windows-forms-by-name. You have to find the control by its name then compare the text. – lkdhruw Nov 24 '19 at 15:53

2 Answers2

1

This is a cheeky little one-liner using Linq (split over multiple lines for clarity):

var textBoxes = this.Controls
                    .OfType<TextBox>() // controls that are TexteBoxes
                    .Where(t => t.Name.StartsWith("Box") && t.Text.StartsWith("S"))
                    .ToList();

int numberS = textBoxes.Count();          
  • We get all TextBox controls using OfType<TextBox>()
  • This assumes the name of the TextBoxes you're interested in start with "Box". The corresponding Linq is Where(t => t.Name.StartsWith("Box")).
  • It looks like you're only interested in the TextBoxes that have a value that starts with "S". The corresponding linq is Where(t => t.Text.StartsWith("S")). I have combined these in a single Where.
  • Then we get the count: .Count()
  • If you want the count of textboxes that contain S (not just start with S) then use t.Text.Contains("S") in the Where clause instead.

If you want to get the TextBox names (Box1_1, Box1_2, etc) then you can use Select which will take the Name property from each TextBox and return a List<string>

var txtNames = this.Controls
                   .OfType<TextBox>() // controls that are TexteBoxes
                   .Where(t => t.Name.StartsWith("Box") && t.Text.StartsWith("S"))
                   .Select(t => t.Name)  // select textbox names
                   .ToList();

txtNames is a List<string> containing the textbox names which start with S.

haldo
  • 14,512
  • 5
  • 46
  • 52
  • This works, but i need the positions where the S is located. – ReD Nov 24 '19 at 17:19
  • @ReD See my update - `TextBoxes` is a list of all the textboxes that contain `S`. Iterate through them to get the names/positions. – haldo Nov 24 '19 at 17:23
0

First of all, you need a collection of your TextBox'es or scrap them from your window. Example of how to do the second is here.

Here is your code that I modified:

public int CountS()
        {
            var numberS = 0;
            for (int i = 1; i <= 7; i++)
            {
                for (int j = 1; j <= 10; j++)
                {
                    string box = "Box" + i.ToString() + "_" + j.ToString();
                    TextBox nameBox 
                        = UIHelper.FindChild<TextBox>(Application.Current.MainWindow, box); //using example linked above
                        //= textBoxes.First(tbx => tbx.Name == box); //if you got collection of your textboxes
                    numberS += nameBox.Text.Count(c => c == 'S'); //if you wont also count a small "s" add .ToUpper() before .Count
                }
            }
            return numberS;
        }
Sawair
  • 1
  • 1