简体   繁体   中英

Show/Hide textboxes- Visible

I am currently working with visible attributes on textboxes. Below I copy/pasted a snippet of my code. I have a total of 8 textboxes set to visible false as the form is loaded. Then I have two radio buttons that display the textboxes accordingly. One radioButton will display the first 4 textboxes and the other will display all 8 textboxes. The problem is when switching back to radioButton1 to only show 4 textboxes it will still display all 8 textboxes?

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
    {

        int count = 0;
        int txtBoxVisible = 3;

        foreach (Control c in Controls)
        {
            if (count <= txtBoxVisible)
            {
                TextBox textBox = c as TextBox;
                if (textBox != null) textBox.Visible = true; 
                count++;
            }
        }
    }

private void radioButton2_CheckedChanged(object sender, EventArgs e)
    {

        int count = 0;
        int txtBoxVisible = 7;

        foreach (Control c in Controls)
        {
            if (count <= txtBoxVisible)
            {
                TextBox textBox = c as TextBox;
                if (textBox != null) textBox.Visible = true; 
                count++;
            }
        }
    }

Try changing this:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;
    if (rb != null && rb.Checked)
    {
        int count = 0;
        int txtBoxVisible = 3;
        HideAllTextBox();
        foreach (Control c in Controls)
        {

            if(count > txtBoxVisible) break;

            TextBox textBox = c as TextBox;

            if (count <= txtBoxVisible && textBox != null)
            {
                textBox.Visible = true; 
                count++;
            }
        }
    }
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb = sender as RadioButton;
    if (rb != null && rb.Checked)
    {

        foreach (Control c in Controls)
        {
            TextBox textBox = c as TextBox;
            if (textBox != null) textBox.Visible = true; 
        }
    }
}

private void HideAllTextBox()
{
    foreach (Control c in Controls)
    {
        TextBox textBox = c as TextBox;
        if (textBox != null) textBox.Visible = false; 
    }
}

In any case, it would be better to iterate through the controls or similar name, for greater accuracy of the affected controls

The CheckedChanged event occures when the Checked property of the RadioButton control has changed. This means both when the RadioButton is checked or unchecked.

Try writing something similar to:

private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton1.Checked)
    {
        // Display the first 4 TextBox controls code.
    }
}

private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
    if (radioButton2.Checked)
    {
        // Display all TextBox controls code.
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM