Solved

Delegate error “Error binding to target method.”

Posted on 2011-03-02
1
439 Views
Last Modified: 2012-05-11
Hi,

I was wondering if someone is able to help. Please refer to the attached code snippet.
I have created a sub that populates a screen with panels. It is my intention that when a panel is clicked the following method is triggered “PanelClick”.

This method is located within the objSenderForm object that I pass through. I have confirmed during runtime that this can be triggered correctly by doing the following in the immediate window:

DirectCast(objSenderForm,PDM.frmLoadForm).PanelClick

PanelClick is set to only return a message box and I can confirm that this functions correctly.

The issue is that I receive the following error message “Error binding to target method.” When the following line is being executed:

Dim obj_event_delegate As [Delegate] = [Delegate].CreateDelegate(obj_event_info.EventHandlerType, DirectCast(objSenderForm, PDM.frmLoadForm), strHandler)

I am unsure how to resolve this issue.

The target is the same as the passed in object that the controls are being added and the strEvent is = to “MouseClick” and the handler is = “PanelClick” which is a public method within frmLoadForm (the passed in object).

Any help would be appreciated. I have possibly just been looking at this too long.

Regards,

Ross

Public Sub getNewPanel(ByVal xCord As Integer, ByVal yCord As Integer, _
                                ByVal height As Integer, ByVal width As Integer, _
                                ByVal strEvent As String, ByVal strHandler As String, _
                                ByVal strQuestion As String, ByVal strControl As String, _
                                ByVal strContronName As String, ByVal strSendTo As String, _
                                ByVal booEnabled As Boolean, _
                                ByVal objSenderForm As Object)

        objPanel = New Panel

        objPanel.Height = height
        objPanel.Width = width
        objPanel.Text = ""
        objPanel.BackColor = Color.Blue
        objPanel.Name = strContronName
        objPanel.Enabled = booEnabled
        objPanel.Location = New System.Drawing.Point(xCord, yCord)
        DirectCast(objSenderForm, PDM.frmLoadForm).Controls.Add(objPanel)


        If strHandler <> "" Then
            Dim obj_type = objPanel.GetType

            Dim obj_event_info = obj_type.GetEvent(strEvent, Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
            Dim obj_event_delegate As [Delegate] = [Delegate].CreateDelegate(obj_event_info.EventHandlerType, DirectCast(objSenderForm, PDM.frmLoadForm), strHandler)
            'objSenderForm
            'DirectCast(Me.objSenderForm,PDM.frmLoadForm)

            obj_event_info.AddEventHandler(objPanel, obj_event_delegate)
            obj_event_delegate = Nothing
        End If


    End Sub

Open in new window

0
Comment
Question by:ross13
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 35028644
1) What does "strHandler" do for you, that you need to check for an empty string?

2) Usually, when adding an event handler, I would do something like this:

Dim panel As New Panel()
Dim textBox As New TextBox()

panel.Controls.Add(textBox)

Me.Controls.Add(panel)

AddHandler panel.Click, AddressOf PanelClickHandler

...

Private Sub PanelClickHandler(ByVal sender As Object, ByVal e As EventArgs)
End Sub

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Convert number to hex in Lua 16 80
FTP recursive download files 5 26
Select coumns form Data table? 3 25
Concurrency Error Persistent using Data Grid View Update C# 2 26
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question