首页 > 解决方案 > 如何在 WinForms 中显示/隐藏对象

问题描述

我想在 C# WinForms 的条形菜单中显示/隐藏对象。

我尝试了以下方法:

private void button1_Click(object sender, EventArgs e)
        {
            int preCounter = 0;
            int check = 0;

            for (int i = 0; i < Word.Text.Length; i++) 
            {
                if (textBoxTab[i].Text == Letter.Text) 
                {
                    textBoxTab[i].Visible = true;
                    fakeBox[i].Visible = false;
                }
                else preCounter++; 

                if(textBoxTab[i].Visible == true)
                {
                    check++;
                }
            }

            if(preCounter == Word.Text.Length)
            {
                counter++; // licznik błędów
                label1.Text = counter.ToString(); // zapis błędów
            }

            Letter.Text = string.Empty;

            if(check == Word.Text.Length)
            {
                MessageBox.Show("YOU WIN!!!");
            }
        }

有条形菜单:

private void newGameToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            Word.Visible = true; 
            WordButton.Visible = true;
            Wisielec.Visible = true;
            Podaj.Visible = true; 
            Letter.Visible = false; //cant hide
            button1.Visible = false;
            label1.Visible = false;
            Counter_l.Visible = false;
            Podaj2.Visible = false;
            for (int i = 0; i < Word.Text.Length; i++) // cant hide
            {
                textBoxTab[i].Visible = false;
                textBoxTab[i].Text = string.Empty;
                fakeBox[i].Visible = false;
                fakeBox[i].Text = string.Empty;
            }
            Word.Text = string.Empty;
            Letter.Text = string.Empty;
        }

如果我再次单击菜单条,我想显示的对象实际上是可见的,但我想隐藏的一些对象仍然可见。

标签: c#winformsvisible

解决方案


private void button1_Click(object sender, EventArgs e)
        {
            //Your code
            //Your code
this.Invalidate(); // perform form re-draw
        }
private void newGameToolStripMenuItem_Click(object sender, EventArgs e) 
        {
            //Your code
            //Your code
this.Invalidate(); // perform form re-draw
        }

推荐阅读