-
Sep 25th, 2020, 02:06 PM
#1
Thread Starter
New Member
Handles Clause on WithEvent
Hi all,
I'm a long time programmer/developer but new to Visual Basic and the Visual Studio. I'm mainly an AS/400 RPG programmer, I just started working where I'll need to add Visual Basic to my toolset.
I'm using Visual Studio 2013. I started my VB learning trying to walk through the examples at www.vbtutor.net. I got stuck on www.vbtutor.net/vb2013/vb2013_lesson7.htm.
The instructions were to create a VB project, add a Picture Box, add an Image, add the OpenFileDialog, filter for JPEG Files| *.JPG|GIF Files|*.GIF|Windows Bitmaps|*.BMP.
So far so good. Then it says, quote,
>>Next, double-click on the View button and enter the following code:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OFGSelectImage.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OFGSelectImage.FileName)
End If
End Sub
<<
I did that. Below is my code.
>>
Code:
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, OFGSelectImage.HelpRequest
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OFGSelectImage.FileOk
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OFGSelectImage.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OFGSelectImage.FileName)
End If
End Sub
End Class
<<
I get the error
BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.
I googled the error... according to what I googled I should add a WithEvents.
If so that should have been in the tutorial, but in any case, when I do that I get another error.
BC30242: 'WithEvents' is not valid on a method declaration.
>>
Code:
Public Class Form1
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, OFGSelectImage.HelpRequest
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OFGSelectImage.FileOk
End Sub
Private WithEvents Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If OFGSelectImage.ShowDialog = Windows.Forms.DialogResult.OK Then
PictureBox1.Image = Image.FromFile(OFGSelectImage.FileName)
End If
End Sub
End Class
<<<<<<
I'm sure I'm missing something simple, but I'm stuck. I tried many variations of where to put the WithEvents.
I'm going to move on to the next lesson. Hopefully someone can clue me in what's happening with this one.
Regards,
Charlie
Last edited by Shaggy Hiker; Sep 26th, 2020 at 04:35 PM.
Reason: Added CODE tags and fixed up the appearance.
-
Sep 25th, 2020, 03:52 PM
#2
Re: Handles Clause on WithEvent
Get rid of this code completely:
Code:
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click, OFGSelectImage.HelpRequest
End Sub
Private Sub OpenFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles OFGSelectImage.FileOk
End Sub
Those are empty subs, and almost certainly one of the handles you have in them is the culprit.
-
Sep 26th, 2020, 11:49 AM
#3
Re: Handles Clause on WithEvent
You did rename the OpenFileDialog1 object to OFGSelectImage, correct? You were suppose to do that before doubleclicking and generating any events.
Before that, rename OpenFileDialog as OFGSelectImage.
Of course I love tutorials that tell you what you are going to change, and then say "Before that, do this". Why tell me what I'm going to do out of order.
"Turn left here."
<crash>
"But before that, turn on your left turn signal..."
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Sep 26th, 2020, 12:19 PM
#4
Re: Handles Clause on WithEvent
Code:
Private WithEvents Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
???
You don't declare a sub withevents. You declare a variable withevents. For example...
Code:
Public Class Form1
Private WithEvents tmr as new Timer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
tmr.Interval = 1000
tmr.Start
End Sub
Private Sub tmr_Tick(sender As Object, e As EventArgs) Handles tmr.Tick
msgbox("Another tick")
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2020, 04:38 PM
#5
Re: Handles Clause on WithEvent
I edited the original post to add CODE tags (the # button) and improve the indenting and separation between blocks in the code. All the replies show that each person found something wrong, and each found a different point.
My usual boring signature: Nothing
-
Sep 28th, 2020, 07:32 AM
#6
Thread Starter
New Member
Re: Handles Clause on WithEvent
Thanks. I think what I may need is a better VB course/tutorial.
-
Sep 28th, 2020, 07:58 AM
#7
Re: Handles Clause on WithEvent
Try the tutorial section of this site, as well as www.homeandlearn.co.uk --- never used it, but I've heard others around here swear by it as being pretty good.
-tg
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|