什么时候Control.Visible = true结果是假的?

amb*_*ber 7 c# winforms

我有一个C#WinForms项目,它的功能非常棒.各个步骤位于一个名为StepPanel的类中,该类继承自Panel控件,在表单中,这些面板以数组形式组织.

我遇到的是,当调用UpdateUI()并遍历数组时,调整当前步骤的向导步骤标题文本,它确保隐藏所有非活动步骤,并确保活动步骤可见,在正确的位置,并且是正确的尺寸.

这是代码:

    private void UpdateUI()
    {
        // If the StepIndex equals the array length, that's our cue 
        // to exit.
        if (StepIndex == Steps.Length)
        {
            Application.Exit();
            return;
        }

        for (var xx = 0; xx < Steps.Length; xx++)
        {
            if (xx == StepIndex)
            {
                if (!String.IsNullOrEmpty(Steps[xx].Title))
                {
                    LabelStepTitle.ForeColor = SystemColors.ControlText;
                    LabelStepTitle.Text = Steps[xx].Title;
                }
                else
                {
                    LabelStepTitle.ForeColor = Color.Red;
                    LabelStepTitle.Text =
                        Resources.UiWarning_StepTitleNotSet;
                }
            }
            else
            {
                Steps[xx].Visible = false;
            }
        }

        Steps[StepIndex].Top = 50;
        Steps[StepIndex].Left = 168;
        Steps[StepIndex].Width = 414;
        Steps[StepIndex].Height = 281;
        Steps[StepIndex].Visible = true;

        SetNavigationButtonState(true);
    }
Run Code Online (Sandbox Code Playgroud)

当一切都说完了,Step [StepIndex] .Visible == false.

我仍然对这种行为感到困惑,因为我的工作时间不到30分钟.

San*_*nen 17

如果您将父/容器控件Visible = false设置为然后将任何子控件设置为Visible = true将无效.Visible子控件的属性仍然是false.

我不知道在这种情况下是否发生了什么,因为我不知道控件的结构,但似乎是一种可能的情况.

要解决此问题,您需要先将父/ contianer控件设置为Visible = true,然后将子控件设置为.

  • 很好,你解决了它.我的最后一句话实际上并非如此.您可以在父级之前将子控件visible属性设置为true.该属性将保持为false但是当您将parent属性设置为true时.Net将"神奇地"记住子控件的状态并相应地设置它们. (4认同)