Results 1 to 7 of 7

Thread: Handles Clause on WithEvent

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    2

    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.

  2. #2
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,187

    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.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,141

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    23,486

    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,331

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2020
    Posts
    2

    Re: Handles Clause on WithEvent

    Thanks. I think what I may need is a better VB course/tutorial.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,021

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width