在程序中获取构建错误以在单击时更改按钮名称

时间:2010-06-13 09:29:01

标签: vb.net visual-studio-2010

Imports System
Imports System.Windows.Forms
Class MyButtonClass
    Inherits Form

    Private mrButton As Button
    Public Sub MyButtonClass()

        mrButton = New Button()
        mrButton.Text = "Click me "
        mrButton.Click += New System.EventHandler(MyButtonClickEventHandler)
        Me.Controls.Add(mrButton)
    End Sub
    Shared Sub Main()
        Application.Run(New MyButtonClass())
    End Sub
    Private Sub MyButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs)
        mrButton.Text = "You clicked me!"
    End Sub
End Class

1 个答案:

答案 0 :(得分:3)

您正在混合使用C#和VB.Net代码。

mrButton.Click += New system.EventHandler(MyButtonClickEventHandler)

是C#语法。

按钮处理程序应声明为:

Private Sub MyButtonClickEventHandler(ByVal sender As Object, ByVal e As EventArgs) Handles mrButton.Click

或者您使用AddHandler作为:

AddHandler mrButton.Click, AddressOf MyButtonClickEventHandler