【问题标题】:Show/Hide textboxes- Visible显示/隐藏文本框 - 可见
【发布时间】:2013-02-17 14:13:32
【问题描述】:

我目前正在处理文本框上的可见属性。下面我复制/粘贴了我的代码的 sn-p。加载表单时,我总共有 8 个文本框设置为可见 false。然后我有两个相应地显示文本框的单选按钮。一个 radioButton 将显示前 4 个文本框,另一个将显示所有 8 个文本框。问题是当切换回radioButton1 只显示 4 个文本框时,它仍然会显示所有 8 个文本框吗?

    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++;
            }
        }
    }

【问题讨论】:

    标签: c#


    【解决方案1】:

    尝试改变这个:

    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; 
        }
    }
    

    无论如何,最好遍历控件或类似名称,以提高受影响控件的准确性

    【讨论】:

    • 当我返回只显示 4 个文本框时,它仍然显示 8 个?
    • 更新了!添加 HideAllTextBox 方法
    【解决方案2】:

    CheckedChanged 事件发生在RadioButton 控件的Checked 属性发生更改时。这意味着无论是选中还是取消选中 RadioButton

    试着写一些类似的东西:

    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.
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-04
      • 2015-05-25
      • 1970-01-01
      • 1970-01-01
      • 2018-05-01
      • 2011-04-04
      • 1970-01-01
      • 2016-07-13
      • 2020-06-08
      相关资源
      最近更新 更多