c# - 如何在 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;
}
如果我再次单击菜单条,我想显示的对象实际上是可见的,但我想隐藏的一些对象仍然可见。
解决方案
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
}
推荐阅读
- python - python 正则表达式如何捕获所有可能是可选的组?
- wpf - 如何有 2 个子按钮,每个子按钮都有不同的 VerticalAlignment?
- vb.net - VB.NET 在不同的显示器上显示所有表单
- python - PyCharm 没有使用正确的 Python 版本/解释器
- javascript - 使用 react 和 react-bootstrap,当窗口调整为更小时,阻止组件移动到下一行
- database - 快递公司的ERD概念模型
- javascript - 未定义不是使用 .js 解析 html 表的对象(评估 'table.tBodies[0].rows')
- python-3.x - 如何按找到的顺序返回匹配的值?
- javascript - 基于嵌套对象的过滤器
- html - Visual Studio 代码中的错误或设置不正确?