-1

i used text boxes name as text_box_1,text_box_2,text_box_3,text_box_4 i need to check these text boxes one by one and if they are null the text box should hide, this is my code ,i tried to declare a variable text box name to do this but it doesn't work ,so can you help me with this

int i;
private void check()
{
    for(i = 0; i < 4; i++)
    {
        if((text_box_+i).Text == "")
        {
            (Text_box_+i.Hide();
        }
    }
}

thanks to @Oztaco i solved it, this is the way that i wanted it thank you very much @Oztaco...

    private void check()
    {
        TextBox[] textBoxes = new TextBox[10];
        textBoxes[0] = a;
        textBoxes[1] = b;
        textBoxes[2] = c;
        textBoxes[3] = d;

        int i;
            for (i = 0; i < 4; i++)
            {
                if(textBoxes[i].Text == "")
                {
                textBoxes[i].Hide();
                }
            }
    }
  • You should use `your_form.Controls.OfType` and cycle on controls returned! – Marco Oct 02 '18 at 14:20
  • You cannot use this syntax to dynamically talk to different classinstances. You could use reflection, but this is going down the wrong path. You could create a List and add your textboxes in there and add them to the view dynamically. From here on you can just iterate through the list. – Csharpest Oct 02 '18 at 14:20
  • 2
    This was asked thousand times. Search: winforms find control by name and upvote the question and answer – Tim Schmelter Oct 02 '18 at 14:21
  • @Tim Schemelter i mean if the text box text is null textbox should hide that's it – kavindu tissera Oct 02 '18 at 14:23
  • @KãvîñdūTîssêrã: you might want to use `String.IsNullOrEmpty` or `String.IsNullOrWhiteSpace`(multiple spaces still empty). – Tim Schmelter Oct 02 '18 at 14:26

3 Answers3

0

If you plan to place Check within your form, then you could define it like this:

private void Check()
{
    foreach (var tb in this.Controls.OfType<TextBox>())
    {
        if (String.IsNullOrEmpty(tb.Text)) tb.Hide();
        else tb.Show();
    }
}

this.Controls contains all the controls in your form; calling OfType<TextBox>() you get only the controls of type TextBox, so all the textboxes you have in your form.

Marco
  • 56,740
  • 14
  • 129
  • 152
0

This is currently one sulution, to use OfType<T> to get all textboxes with in you winform

You don't need to have the Name of your TextBox in a specified format, just.. whatever you want.

//This Gets all textboxes, in your window actually named: form;

//List<TextBox> should also work!
IEnumerable<TextBox> textBoxes = this.Controls.OfType<TextBox>(); 

foreach (TextBox textBox in textBoxes)
{
    if(string.IsNullOrEmpty(textBox.Text)
    {
         //DO SOMETHING IF IT's NULL (or just "")
    }
    else
    {
         //DO SOMETHING ELSE...
    }
}
  • A better method [here](https://stackoverflow.com/questions/3898588/find-control-by-name-from-windows-forms-controls) –  Oct 02 '18 at 14:29
0

There probably is a way to do something similar to text_box_ + i, but you don't want to do that, it is not clean code (explained below).

What you should do instead if you want to do something like that is declare an array of textboxes and loop through the array.

Something like:

TextBox[] textBoxes = new TextBox[10];

and then when you create a new textbox you would do:

textBoxes[0] = new TextBox();

and to access a specific textbox you would do:

textBoxes[i]; // You can loop this

This is a clean way of doing it because if you have an array, the compiler knows what you're trying to do (loop through an array). If C# had a feature to do what you're describing, it would make the program harder to predict because the compiler would not know what you will try to access by concatenating those two statements.

Oztaco
  • 3,399
  • 11
  • 45
  • 84