Answered by:
Newbie Questions

Question
-
Hi
I'm just starting with Visual basic. I found a funny MS guide called "Visual basic for Bright kids"
There I'm at the point of creating Click Event. Code bellow.
It's written as the guide tells to, but still I get two errors from MS Visual Basic 2005 Express.
1. Error 1 'Public Event Click(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
2. Error 2 'System.EventHandler' is a delegate type and requires a single 'addressof' expression as the only argument to the constructor.
Can anyone explain what these erros are about and how to deal with them.
Kind Regards
JF
Imports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
' Constructor method
Public Sub MyButton()
mrButton = New Button()
mrButton.Text = "Click me"
mrButton.Click += New System.EventHandler(MyButtonClickEventHandler)
Me.Controls.Add(mrButton)
End Sub
' Main method
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 ClassSaturday, April 14, 2007 2:07 PM
Answers
-
There are two ways to go about what you are trying to accomplish....first is using the addhandler method:
Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
' Constructor method
Public Sub New()
mrButton = New Button()
mrButton.Text = "Click me"
AddHandler mrButton.Click, AddressOf MyButtonClickEventHandlerMe.Controls.Add(mrButton)
End Sub
' Main method
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 ClassSecond is to just declare the control with "WithEvents" and use the "Handles" statement:
Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private WithEvents mrButton As Button
' Constructor method
Public Sub New()
mrButton = New Button()
mrButton.Text = "Click me"
Me.Controls.Add(mrButton)
End Sub
' Main method
Shared Sub Main()
Application.Run(New MyButtonClass())
End Sub
Private Sub MyButtonClickEventHandler( _
ByVal sender As Object, ByVal e As EventArgs)Handles mrButton.Click
mrButton.Text = "You clicked me!"
End Sub
End ClassEdit: after taking a look I've notice that you are getting C# and VB intertwined...the contructor for a VB class should be "New"
Saturday, April 14, 2007 8:04 PMModerator
All replies
-
There are two ways to go about what you are trying to accomplish....first is using the addhandler method:
Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
' Constructor method
Public Sub New()
mrButton = New Button()
mrButton.Text = "Click me"
AddHandler mrButton.Click, AddressOf MyButtonClickEventHandlerMe.Controls.Add(mrButton)
End Sub
' Main method
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 ClassSecond is to just declare the control with "WithEvents" and use the "Handles" statement:
Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private WithEvents mrButton As Button
' Constructor method
Public Sub New()
mrButton = New Button()
mrButton.Text = "Click me"
Me.Controls.Add(mrButton)
End Sub
' Main method
Shared Sub Main()
Application.Run(New MyButtonClass())
End Sub
Private Sub MyButtonClickEventHandler( _
ByVal sender As Object, ByVal e As EventArgs)Handles mrButton.Click
mrButton.Text = "You clicked me!"
End Sub
End ClassEdit: after taking a look I've notice that you are getting C# and VB intertwined...the contructor for a VB class should be "New"
Saturday, April 14, 2007 8:04 PMModerator -
Thank you, it worked
Though it didn't please me as now I can't trust this guide at all
and I'm starting to get mixed which is VB6, VB.net and finally but not least C# . As this Guide (http://msdn2.microsoft.com/en-us/library/bb330926(VS.80).aspx) tells its for Visual Basic and still it contains that code example:
Well, perhaps that's the reason they call it Guide for "Bright Kids"Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
' Constructor method
Public Sub MyButtonClass()
mrButton = New Button()
mrButton.Text = "Click me"
mrButton.Click += New System.EventHandler(MyButtonClickEventHandler)
Me.Controls.Add(mrButton)
End Sub
' Main method
Shared Sub Main()
Application.Run(New MyButtonClass())
End Sub
' Event handler method
Private Sub MyButtonClickEventHandler(ByVal sender As Object, _
ByVal e As EventArgs)
mrButton.Text = "You clicked me!"
End Sub
End Class
as you have to notice errors they have written to it.
Sunday, April 15, 2007 11:13 AM -
Thank you, it worked
Though it didn't please me as now I can't trust this guide at all
and I'm starting to get mixed which is VB6, VB.net and finally but not least C# . As this Guide (http://msdn2.microsoft.com/en-us/library/bb330926(VS.80).aspx) tells its for Visual Basic and still it contains that code example:
Well, perhaps that's the reason they call it Guide for "Bright Kids"Code SnippetImports System
Imports System.Windows.Forms
Class MyButtonClass
Inherits Form
Private mrButton As Button
' Constructor method
Public Sub MyButtonClass()
mrButton = New Button()
mrButton.Text = "Click me"
mrButton.Click += New System.EventHandler(MyButtonClickEventHandler)
Me.Controls.Add(mrButton)
End Sub
' Main method
Shared Sub Main()
Application.Run(New MyButtonClass())
End Sub
' Event handler method
Private Sub MyButtonClickEventHandler(ByVal sender As Object, _
ByVal e As EventArgs)
mrButton.Text = "You clicked me!"
End Sub
End Class
as you have to notice errors they have written to it.
Sunday, April 15, 2007 11:13 AM