發現學生們特別喜歡用TabControl控件,問過之後才知道,原來他們覺得這個控件看上去比較酷,難怪現在標簽式浏覽器這麼受歡迎。既然喜歡,咱就投其所好,趕緊做了一點教學示例。
這裡演示了TabControl的常用事件:SelectedIndexChanged
Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged lbMessage.Text = "當前顯示的是第" & (TabControl1.SelectedIndex + 1) & "頁" End Sub
用編程的方式添加新頁
Sub btAdd_Click() Sub btAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btAdd.Click TabControl1.TabPages.Add(New TabPage("新頁")) End Sub
用編程的方式刪除選中的頁
Sub btDel_Click() Sub btDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btDel.Click TabControl1.TabPages.RemoveAt(TabControl1.SelectedIndex) End Sub
向前移動某一頁
Sub btLeft_Click() Sub btLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btLeft.Click Dim selectedIndex As Integer = TabControl1.SelectedIndex If (selectedIndex = 0) Then Return '如果是第一頁,就不需要向左移動了 Else Dim selectedTab As TabPage = TabControl1.SelectedTab '暫存選中的TabPage TabControl1.TabPages.Remove(selectedTab) '從現在的位置移除 TabControl1.TabPages.Insert(selectedIndex - 1, selectedTab) '添加到前一個位置(前面的那個TabPage會自動向後移動) End If End Sub