Tabcontrol控制其他控件

时间:2014-03-16 07:28:40

标签: vb.net tabcontrol

我想在Form1中使用TabControl1;其中包含2个标签;在标签1;我插入了RichTextBox1,Label1和Button1;我想在两个标签中使用这3个控件;我尝试了以下代码,但它们没有显示在Tab 2中;怎么做?

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    For sss = 1 To 100
        RichTextBox1.AppendText(Label1.Text & " - No. - " & sss & vbCrLf)
    Next
End Sub

Private Sub MyTabControl_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
    Dim indexOfSelectedTab As Integer = TabControl1.SelectedIndex
    Dim selectedTab As System.Windows.Forms.TabPage = TabControl1.SelectedTab
    If indexOfSelectedTab = 0 Then
        MessageBox.Show("Tab 0 Selected")
    ElseIf indexOfSelectedTab = 1 Then
        MessageBox.Show("Tab 1 Selected")
        '************************************
        RichTextBox1.Show()
        Label1.Show()
        Button1.Show()
        '************************************
    End If
End Sub

结束班

2 个答案:

答案 0 :(得分:1)

这样做:

Private Sub MyTabControl_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
    With MyTabControl.SelectedTab.Controls
       .Add(RichTextBox1)
       .Add(Label1)
       .Add(Button1)
    End With
    Select Case MyTabControl.SelectedIndex
       Case 0 'first tab
           RichTextBox1.Text = "I'm on the first tab"
           '...
       Case 1 'second tab
           RichTextBox1.Text = "Now on second tab"
    End Select
End Sub

这会获取控件并将它们移动到您的任何标签。

答案 1 :(得分:1)

最后,我在上面的Gjaa代码的帮助下想出来了; 我已经得到了如下的最终代码;使用此代码,您可以从Button1或tabControl的任何选项卡控制richTextBox1的内容:

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    RichTextBox1.Clear()
    For s = 1 To 100
        RichTextBox1.AppendText(Label1.Text & " - No. - " & s & vbCrLf)
    Next
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    RichTextBox1.Clear()
    For s = 1 To 100
        RichTextBox1.AppendText(" Button2 - " & s & vbCrLf)
    Next
End Sub

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
    Dim indexOfSelectedTab As Integer = TabControl1.SelectedIndex
    Dim selectedTab As System.Windows.Forms.TabPage = TabControl1.SelectedTab
    With TabControl1.SelectedTab.Controls
        .Add(RichTextBox1)
        .Add(Label1)
        .Add(Button1)
    End With
    If indexOfSelectedTab = 0 Then
        RichTextBox1.Clear()
        For sss = 1 To 100
            RichTextBox1.AppendText(" Tab 0 - " & sss & vbCrLf)
        Next
    ElseIf indexOfSelectedTab = 1 Then
        RichTextBox1.Clear()
        For sss = 1 To 100
            RichTextBox1.AppendText(" Tab 1 - " & sss & vbCrLf)
        Next
    End If
End Sub

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click

End Sub

结束班