Solved raise up event

Posted on 2011-10-06
Last Modified: 2012-06-27

I don't know how to wire up the event on one of my objects, created dynamically (object name is _ctl, type: ComboBoxEditingControl). I am getting an error: 'Handles clause requires a WithEvents variable defined in the containing type or one of its base types'. Here is my code:

Public Class ComboBoxCell
    Inherits DataGridViewTextBoxCell

    Private _ctl As ComboBoxEditingControl

    Public Sub New()
    End Sub

    Private Sub _ctl_Leave(ByVal sender As Object, ByVal e As EventArgs) Handles _ctl.Leave
        If DirectCast(sender, ComboBox).SelectedItem Is Nothing Then
        End If
        Me.DataGridView.CurrentCell.Value = (DirectCast(DirectCast(sender, ComboBox).SelectedItem, System.Data.DataRowView).Row).ItemArray(1).ToString()
    End Sub
End Class

I want to wire up _ctl_Leave event.
_ctl object is initialized in this sub (part of the class ComboBoxCell):

    Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
        MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)

        _ctl = TryCast(DataGridView.EditingControl, ComboBoxEditingControl)

        Dim col As ComboBoxColumn = TryCast(Me.OwningColumn, ComboBoxColumn)
        _ctl.DataSource = col.DataSource
        _ctl.DisplayMember = col.DisplayMember
        _ctl.ValueMember = col.ValueMember

    End Sub

Greetings, Frenky
Question by:AntonioRodrigo
    LVL 83

    Expert Comment

    Use the AddHandler clause

    AddHandler _ctl.SelectIndexChanged, Addressof yoursubname
    LVL 83

    Expert Comment

    And remove the Handles clause.
    LVL 85

    Accepted Solution


        Private _ctl As ComboBoxEditingControl


        Private WithEvents _ctl As ComboBoxEditingControl
    LVL 83

    Expert Comment


    Author Closing Comment

    I think that both solutions are OK, but this one is easier to implement.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now