一键更改面板

Change panels with one button of a click

我目前正在制作一个程序,我正在添加一个实用程序表单,并且我有按钮和面板。 1 个面板的 1 个按钮,所以如果他们单击一个按钮而另一个面板可见以隐藏和显示另一个面板,我有代码。出于某种原因,我必须点击按钮两次才能切换到不同的面板,不知道为什么。

代码:

Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
    If pnlAbout.Visible = False Then
        pnlAbout.Visible = True
    Else
        If pnlProfile.Visible = True Then
            pnlProfile.Visible = False
            pnlAbout.Visible = True

            If pnlUpdates.Visible = True Then
                pnlUpdates.Visible = False
                pnlAbout.Visible = True
            End If
        End If
    End If
End Sub
Private Sub btnProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProfile.Click
    If pnlProfile.Visible = False Then
        pnlProfile.Visible = True
    Else
        If pnlAbout.Visible = True Then
            pnlAbout.Visible = False
            pnlProfile.Visible = True

            If pnlUpdates.Visible = True Then
                pnlUpdates.Visible = False
                pnlProfile.Visible = True
            End If
        End If
        End If
End Sub
Private Sub btnUpdates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdates.Click
    If pnlUpdates.Visible = False Then
        pnlUpdates.Visible = True
    Else
        If pnlAbout.Visible = True Then
            pnlAbout.Visible = False
            pnlUpdates.Visible = True

            If pnlProfile.Visible = True Then
                pnlProfile.Visible = False
                pnlUpdates.Visible = True
            End If
        End If
    End If
End Sub

您的代码可以概括为:

Private Sub btnAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAbout.Click
    pnlAbout.Visible = True
    pnlProfile.Visible = False
    pnlUpdates.Visible = False
End Sub
Private Sub btnProfile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnProfile.Click
    pnlAbout.Visible = False
    pnlProfile.Visible = True
    pnlUpdates.Visible = False
End Sub
Private Sub btnUpdates_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdates.Click
    pnlAbout.Visible = False
    pnlProfile.Visible = False
    pnlUpdates.Visible = True
End Sub

您需要两次,因为第一次点击使正确的面板可见,而第二次(如果幸运的话)可见的面板不可见。

你的代码可以总结成这样:

private sub button1_click(Byval sender as System.Object, ByVAl e AS System.EventArgs)Handles buttonUpdates.click

    panel1.Visible = true
    panel2.Visible = false

End Sub