Question

I've placed label1, richTextBox1, Button1, and Button2 in Form1; Then I wrote the code below; all works fine, except Button1 and button2 are not working; Any idea why?

Public Class Form1

Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

    Select Case TabControl1.SelectedIndex
        Case 0 'first tab
            RichTextBox1.Clear() : RichTextBox1.AppendText("I'm on the first tab" & vbCrLf)
            '...
            With TabControl1.SelectedTab.Controls
                .Add(RichTextBox1)
                .Add(Label1)
                .Add(Button1)
            End With
        Case 1 'second tab
            RichTextBox1.Clear() : RichTextBox1.AppendText("Now; I'm on the Second tab" & vbCrLf)
            '...
            With TabControl1.SelectedTab.Controls
                .Add(RichTextBox1)
                .Add(Button2)
            End With
    End Select
End Sub

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

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

End Class

Was it helpful?

Solution

Your button click handlers seem to have lost their association with the buttons. Either you copy-pasted the code in without hooking up the handlers or de-selected the handlers in the object's Events pane. Just add the Handles clause back in like :

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

See : Handles Clause (MSDN)

As an aside, you should also seriously consider turning ON Option Explicit and Option Strict in your project options. These can be turned off for compatibility with legacy VB code but they encourage sloppy code that can lead to headaches and trouble. Your handler would look like this with those options :

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim s As Integer   ' Option explicit - must declare all variables!
    For s = 1 To 100
        RichTextBox1.AppendText(" Button1 - " & s.ToString() & vbCrLf)
        ' Option strict -> no implicit casting of types (ie : int to string ^^)
    Next
End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top