Results 1 to 8 of 8

Thread: [RESOLVED] Event Handler Error

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    233

    Resolved [RESOLVED] Event Handler Error

    I get this error on my form, not sure what to do:

    Error 1 Option Strict On does not allow narrowing in implicit type conversions between method 'Private Sub searchResult_SearchFileMatches(sender As Object, e As Jam.Shell.FileMatchEventArgs)' and delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'.

    on RED Text

    Code:
      Private ReadOnly Property FileMatchEventHandler() As EventHandler
            Get
                If (FileMatchEventHandler Is Nothing) Then
                    FileMatchEventHandler = AddressOf searchResult_SearchFileMatches
                End If
                Return FileMatchEventHandler
            End Get
        End Property

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Event Handler Error

    Use the generic EventHandler type

    Code:
        Private ReadOnly Property FileMatchEventHandler() As EventHandler(Of Jam.Shell.FileMatchEventArgs)
            Get
                If (FileMatchEventHandler Is Nothing) Then
                    FileMatchEventHandler = AddressOf searchResult_SearchFileMatches
                End If
                Return FileMatchEventHandler
            End Get
        End Property

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    233

    Re: Event Handler Error

    I have another error:-

    Not sure what to do?

    Error 1 'Public Event SearchFileMatches(sender As Object, e As Jam.Shell.FileMatchEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

    Error 1
    Code:
     Private Sub searchResults_SearchCompleted(ByVal sender As System.Object, ByVal e As Jam.Shell.SearchCompletedEventArgs) Handles searchResults.SearchCompleted
            'remove the filematching eventhandler if specified, see buttStartSearch_Click
            searchResults.SearchFileMatches -= FileMatchEventHandler
    
            buttStopSearch.Enabled = False
            buttStartSearch.Enabled = True
    
            'reset the progress indicator
            SearchProgressBar.Visible = False
            SearchProgressBar.Value = 0
    
            'allow sorting again
            searchResults.HeaderStyle = ColumnHeaderStyle.Clickable
    
            'the search was cancelled by the user
            If e.Cancelled Then
                toolStripStatusLabel1.Text = "The search operation was cancelled by the user."
                Exit Sub
            End If
    
            'an error/exception occurred during the search, inform the user
            If e.[Error] IsNot Nothing Then
                Dim errString As String = "An Error occurred: " & e.[Error].Message
                MessageBox.Show(errString, "Search error", MessageBoxButtons.OK, MessageBoxIcon.Information)
                toolStripStatusLabel1.Text = errString
            Else
                'all went ok, the search is complete
                toolStripStatusLabel1.Text = "The search operation completed."
            End If
        End Sub

  4. #4
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Event Handler Error

    VB does not support C# style +=/-= syntax for event wireup. You should use AddHandler and RemoveHandler to add and remove event handlers.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Event Handler Error

    Are you switching to VB from C# by any chance? It looks like you're trying to use C# syntax in VB. You use AddHandler and RemoveHandler to add and remove event handlers in VB, e.g.
    vb.net Code:
    1. RemoveHandler searchResults.SearchFileMatches, AddressOf FileMatchEventHandler
    Of course, the event handler would have to have been added using AddHandler in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    233

    Re: Event Handler Error

    Yes, I converted the code from c#.

    I have this code at the start:-

    Code:
     Private m_fileMatchEventHandler As EventHandler(Of FileMatchEventArgs)
    
        ''' <summary>
        ''' provide on-access to our selfdefined file matching handler that extends the
        ''' search filter.
        ''' </summary>
        Private ReadOnly Property FileMatchEventHandler() As EventHandler(Of FileMatchEventArgs)
            Get
                If m_fileMatchEventHandler Is Nothing Then
                    m_fileMatchEventHandler = New EventHandler(Of FileMatchEventArgs)(AddressOf searchResults_SearchFileMatches)
                End If
                Return m_fileMatchEventHandler
            End Get
        End Property

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Event Handler Error

    I don't understand why you have a property returning an event handler. What is the purpose of that?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    233

    Re: Event Handler Error

    Thanks for your help, managed to get it working with:

    Code:
    RemoveHandler searchResults.SearchFileMatches, FileMatchEventHandler

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