Reputation: 395
I want to use TabControl1 in Form1; which contain 2 tabs; In tab 1; I inserted RichTextBox1, Label1, and Button1; I want to use these 3 controls in both tabs; I tried the following code, but they are not showing in Tab 2; How to do this?
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
End Class
Upvotes: 2
Views: 168
Reputation: 1579
Do this:
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
This take the controls and move them to whatever tab you are.
Upvotes: 1
Reputation: 395
Finally, I figured it out with the help of Gjaa code above; I've got the final code as below; with this code you can control the contents of the richTextBox1 from Button1 or any tab of the tabControl:
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
End Class
Upvotes: 1