我在Form1中放置了label1,richTextBox1,Button1和Button2; 然后我写了下面的代码;一切正常,但Button1和button2不起作用; 知道为什么吗?
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
结束班
答案 0 :(得分:1)
您的按钮点击处理程序似乎已失去与按钮的关联。您可以在不挂接处理程序的情况下复制粘贴代码,也可以在对象的“事件”窗格中取消选择处理程序。只需添加Handles
子句,如:
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
顺便说一句,您还应该认真考虑在项目选项中启用 ON Option Explicit
和Option Strict
。可以关闭它们以与传统的VB代码兼容,但它们会鼓励可能导致令人头疼和麻烦的代码。您的处理程序看起来像这样:
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