• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 665
  • Last Modified:

Vb.net raise up event

Hello,

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()
        MyBase.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
            Return
        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
0
AntonioRodrigo
Asked:
AntonioRodrigo
  • 3
1 Solution
 
CodeCruiserCommented:
Use the AddHandler clause

AddHandler _ctl.SelectIndexChanged, Addressof yoursubname
0
 
CodeCruiserCommented:
And remove the Handles clause.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Change:

    Private _ctl As ComboBoxEditingControl

To:

    Private WithEvents _ctl As ComboBoxEditingControl
0
 
AntonioRodrigoAuthor Commented:
I think that both solutions are OK, but this one is easier to implement.
0

Featured Post

Industry Leaders: 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!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now