【问题标题】:Combobox.Visible property not workingCombobox.Visible 属性不起作用
【发布时间】:2012-07-23 06:51:21
【问题描述】:

晚上,我在一个静态类中有以下代码,这有助于我的 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;
    }

编辑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:根据 Toms 的建议,我做了以下更改

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 可见性属性设置为什么,结果为真。当我尝试通过原始方法检查时,我仍然得到错误。

我被难住了!!

编辑3:

似乎当我单击第二个选项卡时,组合框的可见属性 = false。

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

【问题讨论】:

  • 表单中是否有combobox的子类?
  • 代码与 ComboBox 上的异​​常一起工作,我必须为 SelectedIndex &gt; -1 检查添加检查。 什么时候你调用这个例程?
  • 我点击一个按钮来执行一些文件验证。此代码在我创建任务之前运行。
  • 你能不能尝试捕捉事件 [visibleChanged] : msdn.microsoft.com/en-us/library/…
  • 运行验证码时,TableLayoutPanel 控件必须在屏幕上可见。如果它位于非活动 TabPage 后面,则它不在屏幕上。

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


【解决方案1】:

来自我的评论:

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

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 2011-03-31
    • 2018-11-19
    • 2016-07-16
    • 2020-01-24
    • 2015-09-05
    • 2011-06-19
    • 1970-01-01
    相关资源
    最近更新 更多