c# - Combobox.Visible 属性不起作用

标签 c# winforms c#-4.0 combobox textbox

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

if (cb.Visible == true)

&

if (tb.Visible == true)

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

有什么想法吗?

谢谢

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

编辑1:

用户界面代码

    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 类中

Edit2:根据汤姆的建议,我做了以下更改

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 可见性属性设置的内容,结果为 True。当我尝试通过原始方法检查时,我仍然得到 false。

我很难过!!

编辑3:

似乎当我点击第二个选项卡时,comboBox 的可见属性 = false。

有人知道为什么会这样吗?

最佳答案

来 self 的评论:

The TableLayoutPanel control has to be visible on the screen when you run your verification code. If it's behind an inactive TabPage, then it isn't on the screen and the control will report the Visible property as false, regardless if the property is set to true in the designer.

关于c# - Combobox.Visible 属性不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11639583/

相关文章:

c# - Visual Studio 2010 : How to enforce build order of projects in a solution?

c# - WPF CheckBox 多重绑定(bind)

c# - 数据导出到 MS ACCESS 时, 'Microsoft.ACE.OLEDB.12.0' 提供程序未在本地计算机上注册

c# - 以前从未使用过 LocalDB

c# - Npgsql C# throwing Operation is not valid 由于对象的当前状态突然

c# - 将多个控件数据绑定(bind)到单个 LINQ 查询

c# - 我将如何等待多个线程停止?

c# - 联播 .NET 流

c# - WPF Prism + 现有 WPF 应用程序

c# - 在调用 CompleteAdding 后添加到 BlockingCollection