侧面导航栏以及如何添加子窗体

Posted VB.Net

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了侧面导航栏以及如何添加子窗体相关的知识,希望对你有一定的参考价值。

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。


今天有网友咨询如何设计主窗体,使它左侧显示按钮导航,右侧显示子窗体。

后来想了一下还是挺简单的:
窗体设计:
form1(主窗体)

其它子窗体(这里主要演示,只设计了2个子窗体)
form2:

form3:

form2和form3的formborderstyle属性设置为none。

在Form1中的代码如下:

    Dim loc As Point

    Dim currentForm As Form

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        loc = New Point(12, 14)

        Me.Size = New Size(707, 407)
        Call shownNav(gbMain)
    End Sub

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        Call shownNav(gb1)
    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        Call shownNav(gb2)
    End Sub

    Private Sub shownNav(ByVal shownGroup As GroupBox)
        gb1.Visible = False
        gb2.Visible = False
        gbMain.Visible = False

        shownGroup.Visible = True
        shownGroup.Location = loc
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button10.Click
        Call shownNav(gbMain)
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If currentForm IsNot Nothing Then
            Me.Panel1.Controls.Remove(currentForm)
            currentForm = Nothing
        End If

        Dim f2 As New Form2
        f2.TopLevel = False
        f2.Dock = DockStyle.Fill
        Me.Panel1.Controls.Add(f2)
        f2.Visible = True

        currentForm = f2
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If currentForm IsNot Nothing Then
            Me.Panel1.Controls.Remove(currentForm)
            currentForm = Nothing
        End If
        Dim f3 As New Form3
        f3.TopLevel = False
        f3.Dock = DockStyle.Fill
        Me.Panel1.Controls.Add(f3)
        f3.Visible = True

        currentForm = f3
    End Sub

代码简单,不做多的解释。
运行时显示如下:

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供的参考。

学习更多vb.net知识,请参看 vb.net 教程 目录

以上是关于侧面导航栏以及如何添加子窗体的主要内容,如果未能解决你的问题,请参考以下文章

如何制作侧面导航栏并让div填充其余空间[重复]

delphi左侧导航栏收缩

如何实现winform 可隐藏导航栏

wordpress上面如何在导航栏上面添加子分类

如何使导航栏全角?

如何使用 swift 在子视图中添加标签栏和导航栏?