addhandler in EditingControlShowing in vb net RRS feed

  • Question

  • I have this code in DataGridView_EditingControlShowing

        If TypeOf e.Control Is ComboBox Then
            With DirectCast(e.Control, ComboBox)
                .DropDownStyle = ComboBoxStyle.DropDown
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .DroppedDown = False
            End With
            AddHandler e.Control.KeyDown, AddressOf cell_KeyDown      
        End If
    i want addhandler when press key to make (DirectCast(e.Control, ComboBox).DroppedDown = False ) but its addressof refer to outer sub where i cant use the e.control . because e.control only works with DataGridView_EditingControlShowing event sub Any Solution ?
    Friday, August 31, 2018 8:34 PM

All replies

  • You can access the combo box in the key down event e.g.

    Imports System.Runtime.CompilerServices
    Public Class Form1
        Private AutoList As AutoCompleteStringCollection =
                    New AutoCompleteStringCollection
        Private cbo As ComboBox
        Private ComboColumnName As String = "C1"
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim source = New String() {"Add", "Subtract", "Divide", "Multiply"}
            Dim DataGridViewCellStyle1 As DataGridViewCellStyle = New DataGridViewCellStyle()
            DataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleCenter
            Dim column1 As New DataGridViewComboBoxColumn With
                        .DataSource = source,
                        .DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing,
                        .DefaultCellStyle = DataGridViewCellStyle1,
                        .Name = ComboColumnName,
                        .HeaderText = "Demo",
                        .SortMode = DataGridViewColumnSortMode.NotSortable
            Dim column2 As New DataGridViewCheckBoxColumn With
                        .Name = "C2",
                        .HeaderText = "Col 2"
            Dim column3 As New DataGridViewTextBoxColumn With
                        .Name = "C3",
                        .HeaderText = "Col 3"
            DataGridView1.Columns.AddRange(New DataGridViewColumn() {column1, column2, column3})
            DataGridView1.Rows.Add(New Object() {"Divide", True, "Karen"})
            DataGridView1.Rows.Add(New Object() {"Add", False, "Kevin"})
            DataGridView1.Rows.Add(New Object() {"Subtract", True, "Anne"})
            DataGridView1.Rows.Add(New Object() {"Add", True, "Joe"})
            DataGridView1.Rows.Add(New Object() {"Multiply", True, "Jean"})
        End Sub
        Private Sub DataGridView1_CellLeave(sender As Object, e As DataGridViewCellEventArgs) _
            Handles DataGridView1.CellLeave
            If cbo IsNot Nothing Then
                If Not String.IsNullOrWhiteSpace(cbo.Text) Then
                    If Not AutoList.Contains(cbo.Text) Then
                        Dim cb = CType(DataGridView1.Columns(0), DataGridViewComboBoxColumn)
                        Dim items As List(Of String) = CType(cb.DataSource, String()).ToList
                        Dim index = items.FindIndex(Function(x) x.ToLower = cbo.Text.ToLower)
                        cb.DataSource = items.ToArray
                        cbo.SelectedIndex = index
                    End If
                End If
            End If
        End Sub
        Private Sub DataGridView1_EditingControlShowing(
            sender As Object,
            e As DataGridViewEditingControlShowingEventArgs) _
            Handles DataGridView1.EditingControlShowing
            If DataGridView1.CurrentCell.IsComboBoxCell Then
                If DataGridView1.Columns(DataGridView1.CurrentCell.ColumnIndex).Name = ComboColumnName Then
                    cbo = TryCast(e.Control, ComboBox)
                    RemoveHandler cbo.KeyDown, AddressOf CboKeyDown
                    cbo.DropDownStyle = ComboBoxStyle.DropDown
                    cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend
                    cbo.AutoCompleteSource = AutoCompleteSource.ListItems
                    AddHandler cbo.KeyDown, AddressOf CboKeyDown
                End If
            End If
        End Sub
        Private Sub CboKeyDown(sender As Object, e As KeyEventArgs)
        End Sub
    End Class
    Module DatagridViewExtensions
        Public Function IsComboBoxCell(ByVal sender As DataGridViewCell) As Boolean
            Dim result As Boolean = False
            If sender.EditType IsNot Nothing Then
                If sender.EditType Is GetType(DataGridViewComboBoxEditingControl) Then
                    result = True
                End If
            End If
            Return result
        End Function
    End Module

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, August 31, 2018 10:37 PM