Combobox.Visible属性不起作用

时间:2012-07-24 21:28:25

标签: c# winforms c#-4.0 combobox textbox

晚上,我在静态类中有以下代码,这有助于我的userInterface分部类。每当我到达检查组合框/文本框是否可见的部分时:

if (cb.Visible == true)

&安培;

if (tb.Visible == true)

即使控件在表单上可见,它也始终为假。

有什么想法吗?

由于

public static bool VerifyTableLayoutControlsContainData(TableLayoutPanel tlp)
    {
        foreach (Control input in tlp.Controls)
        {
            ComboBox cb = input as ComboBox;
            if(cb != null)
            {
                if (cb.Visible == true)
                {
                    if (string.IsNullOrEmpty(cb.SelectedItem.ToString()))
                    {
                        return false;
                    }
                }
            }

            TextBox tb = input as TextBox;
            if (tb != null)
            {
                if (tb.Visible == true)
                {
                    if (string.IsNullOrEmpty(tb.Text))
                    {
                        return false;
                    }
                }
            }
        }
        return true;
    }

Screenshot

EDIT1:

UserInterface代码

    private void uiBtnDataVerification_Click(object sender, EventArgs e)
    {
        if (VerifyingData != true)
        {
            AllInputsContainDataCsvFileVerificationStage = true;
            //Check all inputs have something in them
            InputsContainDataCsvFileVerificationStage();

            if (AllInputsContainDataCsvFileVerificationStage == false)
            {
                UiHelper.UpdateLog("One or more inputs has not been specified.", uiTxtOperationLog);
                return;
            }

            ...
        }
        else
        {
            //Cancel permanent cancellation token.
        }
    }

    public void InputsContainDataCsvFileVerificationStage()
    {

        ....

        if (UiHelper.VerifyTableLayoutControlsContainData(uiTableLayoutPanelColumnDataTypes)) { }
        else
        {
            AllInputsContainDataCsvFileVerificationStage = false;
        }

        ....
    }

原始代码位于UiHelper类

编辑2:根据汤姆斯的建议,我做了以下更改

public userInterface()
    {
        InitializeComponent();

        uiTableLayoutPanelColumnDataTypes.VisibleChanged += new EventHandler(notifyMe);
        uiCBColumn1DataType.VisibleChanged += new EventHandler(notifyMe1);

        SortedColumnNames = new List<TextBox>();
        SortedDataTypes = new List<ComboBox>();
        AllInputsContainDataCsvFileVerificationStage = true;
    }

    public void notifyMe1(object sender, EventArgs e)
    {
        bool temp = uiCBColumn1DataType.Visible;
        MessageBox.Show("its been changed");
    }

    public void notifyMe(object sender, EventArgs e)
    {
        bool temp = uiTableLayoutPanelColumnDataTypes.Visible;
        MessageBox.Show("its been changed");
    }

在点击按钮之前,我确定了cb1可见性属性设置为什么,它是真的。当我尝试通过原始方法检查时,我仍然是假的。

我很难过!!

EDIT3:

似乎当我点击第二个标签时,comboBox的visible属性= false。

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:5)

来自我的评论:

  

运行验证码时,必须在屏幕上显示TableLayoutPanel控件。如果它位于非活动TabPage后面,则它不在屏幕上,并且控件会将Visible属性报告为false,无论设计器中的属性是否设置为true。