如何通过单击C#中的按钮来隐藏和显示面板

时间:2018-09-26 10:42:13

标签: c# .net winforms

我有三个面板和三个按钮。单击button1后,我要显示Panel1并隐藏Panel2和Panel3。

与其他按钮和面板相同的过程。我该怎么办?

我正在使用用C#/。NET编写的Window Forms应用程序。该代码无法很好地运行,因为仅显示了两个面板。

private void frmMain_Load(object sender, EventArgs e)
{
    pnlItems.Visible = true;
    pnlCustomer.Visible = false;
    pnlPOS.Visible = false;
}

private void btnItems_Click(object sender, EventArgs e)
{
    pnlItems.Visible = true;
    pnlCustomer.Visible = false;
    pnlPOS.Visible = false;
}

private void btnCustomers_Click(object sender, EventArgs e)
{
    pnlCustomer.Visible = true;
    pnlItems.Visible = false;
    pnlPOS.Visible = false;
}

private void btnPOS_Click(object sender, EventArgs e)
{
    pnlPOS.Visible = true;
    pnlCustomer.Visible = false;
    pnlItems.Visible = false;
}

1 个答案:

答案 0 :(得分:1)

private void frmMain_Load(object sender, EventArgs e)
{
    pnlItems.Visible = true;
    pnlCustomer.Visible = false;
    pnlPOS.Visible = false;
}

private void btnItems_Click(object sender, EventArgs e)
{
    if(pnlItems.Visible != true)
    {
        pnlItems.Visible = true;
        pnlCustomer.Visible = false;
        pnlPOS.Visible = false;
    }
}

private void btnCustomers_Click(object sender, EventArgs e)
{
    if(pnlCustomer.Visible != true)
    {
        pnlCustomer.Visible = true;
        pnlItems.Visible = false;
        pnlPOS.Visible = false;
    }
}

private void btnPOS_Click(object sender, EventArgs e)
{
    if(pnlPOS.Visible != true)
    {
        pnlPOS.Visible = true;
        pnlCustomer.Visible = false;
        pnlItems.Visible = false;
    }
}

如果您的面板不可见,请在这里更新条件语句。我希望这会起作用。