1

I am very new to programming, so hopefully this is not a stupid question. I have created a WinForm app that will create a csv file based on information entered in several textboxes. I want the last set of textboxes and their corresponding labels to appear in the GUI only when a corresponding number has been entered into a specific text box. I have the appropriate number of textboxes appearing when a number is entered in the controlling textbox, but I am not sure how to get the labels to appear as well. When the number in the controlling textbox is deleted or changed, I would like the other textboxes and labels to disappear, then the correct number to reappear.

WinForm GUI

Here is my code for getting the textboxes to appear. I'm not sure how to get the labels to appear, or how to reset the boxes when the number changes in the SitesTextBox.

//Number entered into Sites Textbox enables Site Repeater Textboxes 
private void SitesTextBox_TextChanged(object sender, EventArgs e)
    {
        int repeaterSites;
        string SitesInput = SitesTextBox.Text;
        int.TryParse(SitesInput, out repeaterSites);

        for (int i = 1; i <= repeaterSites; i++)
        {
            foreach (Control SiteTextBox in this.Controls)
            {
                if (SiteTextBox.Name == "SiteTextBox"i.ToString())
                {
                    SiteTextBox.Visible = true;
                }
                                    
            }
            

        }
    }
Jman1787
  • 13
  • 5
  • Give your labels meaningful names just like you did with your textboxes, you can use the same loop for both types of controls. – Zohar Peled Apr 10 '23 at 06:41
  • You can loop through the form controls and check to see if they are textboxes and labels and then take appropriate action. For example, see this link: https://stackoverflow.com/questions/15186828/loop-through-all-controls-of-a-form-even-those-in-groupboxes – SoftwareDveloper Apr 10 '23 at 14:53

1 Answers1

1

You are almost there, the way requiring the least changes to your current code is probably to make sure each label has a meangingfulname, like you do with your textboxes, and then do this:

foreach (Control control in this.Controls)
{
    if (control.Name.StartsWith("SiteTextBox"))
    {
        control.Visible = true;
     }
          
    if (control.Name.StartsWith("NameOfYourLabel"))
    {
        control.Visible = true;
     }                      
 }

Or the name of the controls could both start the same, then you only need 1 If statement.

Maybe a little more elegant is to first check the type of control and cast it

        foreach (Control control in this.Controls)
        {
            //If the control is a textbox, then we cast it and then place the reference in "tb"
            if (control is TextBox tb)
            {
                if (tb.Name.StartsWith("SiteTextBox"))
                {
                    tb.Visible = true;
                }
            }

            //similar for a label
            if (control is Label lbl)
            {
                if (lbl.Name.StartsWith("NameOfYourLabel"))
                {
                    lbl.Visible = true;
                }
            }
        }

FYI: If you just wanted one type of control, then you can also use the OfType option like this:

foreach(var textbox in this.Controls.OfType<TextBox>())
{

}

And then even expand this with Linq to also check the name at the same time.

    foreach(var control in this.Controls.OfType<TextBox>().Where(c => c.Name.StartsWith("SiteTextBox")))
    {

    }
jason.kaisersmith
  • 8,712
  • 3
  • 29
  • 51
  • Thanks! I went with checking the type of control and casting it. The textboxes and labels now appear when a corresponding number is entered in the controlling textbox. How do I get them to 'reset' if the number in the controlling textbox changes though? – Jman1787 Apr 10 '23 at 14:01