Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai

I want to create a usercontrol with a Datagrid And a listbox.I want to show Listbox below the cell for the column DataGridViewTextBoxCell.
I inherit a class with datagridview called IGrid. And added a Listbox called Lstbox.
I want a popup of listbox . for this i use this code.
I want to make visible Listbox as stated in the code below.
I tried to create an instance of Igrid, also created new variable for listbox,but bothcase i cant make visible. Please show a method how to do this.

Public Class IGrid
     Inherits DataGridView
#Region "Main"
    Public WithEvents LstBox As New System.Windows.Forms.ListBox
    Public Sub New()
        InitializeComponent()
        Me.Controls.Add(LstBox)
        LstBox.Visible = False
        InitializeComponent()
    End Sub
#End Region

#Region "TlBox"
    Public Class DataGridViewTLBoxColumn
        Inherits DataGridViewColumn
        Public Sub New()
            MyBase.New(New TLBoxCell())
        End Sub
        Public Overrides Property CellTemplate() As DataGridViewCell
            Get
                Return MyBase.CellTemplate
            End Get
            Set(ByVal value As DataGridViewCell)
                MyBase.CellTemplate = value
            End Set
        End Property
    End Class

    Public Class TLBoxCell
        Inherits DataGridViewTextBoxCell
        Public Overrides Sub InitializeEditingControl(ByVal rowIndex As Integer, ByVal initialFormattedValue As Object, ByVal dataGridViewCellStyle As DataGridViewCellStyle)
            MyBase.InitializeEditingControl(rowIndex, initialFormattedValue, dataGridViewCellStyle)
            Dim ctl As TLBoxEditingControl = CType(DataGridView.EditingControl, TLBoxEditingControl)
            ctl.Text = Me.Value
        End Sub
        Public Overrides ReadOnly Property EditType() As Type
            Get
                Return GetType(TLBoxEditingControl)
            End Get
        End Property
        Public Overrides ReadOnly Property ValueType() As Type
            Get
                Return GetType(String)
            End Get
        End Property
        Public Overrides ReadOnly Property DefaultNewRowValue() As Object
            Get
                Return Me.Value
            End Get
        End Property
    End Class

    Private Class TLBoxEditingControl
        Inherits TextBox
        Implements IDataGridViewEditingControl
        Private DataGridViewControl As DataGridView
        Private ValueIsChanged As Boolean = False
        Private RowIndexNum As Integer


        'Public WithEvents Lbox As New IGrid
        'Or
        Public WithEvents Lbox As New ListBox


        Public Property EditingControlFormattedValue() As Object Implements IDataGridViewEditingControl.EditingControlFormattedValue
            Get
                Return Me.Text
            End Get
            Set(ByVal value As Object)
                Me.Text = value
            End Set
        End Property
        Public Function GetEditingControlFormattedValue(ByVal context As DataGridViewDataErrorContexts) As Object Implements IDataGridViewEditingControl.GetEditingControlFormattedValue
            Return Me.Text
        End Function
        Public Sub ApplyCellStyleToEditingControl(ByVal dataGridViewCellStyle As DataGridViewCellStyle) Implements IDataGridViewEditingControl.ApplyCellStyleToEditingControl

        End Sub
        Public Property EditingControlRowIndex() As Integer Implements IDataGridViewEditingControl.EditingControlRowIndex
            Get
                Return RowIndexNum
            End Get
            Set(ByVal value As Integer)
                RowIndexNum = value
            End Set
        End Property
        Public Function EditingControlWantsInputKey(ByVal key As Keys, ByVal dataGridViewWantsInputKey As Boolean) As Boolean Implements IDataGridViewEditingControl.EditingControlWantsInputKey
            Select Case key And Keys.KeyCode
                Case Keys.Down
                    Lbox.Focus()
                    Lbox.SelectedIndex = 0
                Case Keys.Up
            End Select
            Return True
        End Function
        Public Sub PrepareEditingControlForEdit(ByVal selectAll As Boolean) Implements IDataGridViewEditingControl.PrepareEditingControlForEdit
            ' No preparation needs to be done.
        End Sub
        Public ReadOnly Property RepositionEditingControlOnValueChange() As Boolean Implements IDataGridViewEditingControl.RepositionEditingControlOnValueChange
            Get
                Return False
            End Get
        End Property
        Public Property EditingControlDataGridView() As DataGridView Implements IDataGridViewEditingControl.EditingControlDataGridView
            Get
                Return DataGridViewControl
            End Get
            Set(ByVal value As DataGridView)
                DataGridViewControl = value
            End Set
        End Property
        Public Property EditingControlValueChanged() As Boolean Implements IDataGridViewEditingControl.EditingControlValueChanged
            Get
                Return ValueIsChanged
            End Get
            Set(ByVal value As Boolean)
                ValueIsChanged = value
            End Set
        End Property
        Public ReadOnly Property EditingControlCursor() As Cursor Implements IDataGridViewEditingControl.EditingPanelCursor
            Get
                Return MyBase.Cursor
            End Get
        End Property
        Protected Overrides Sub OnTextChanged(ByVal eventargs As EventArgs)
            ValueIsChanged = True
            Me.EditingControlDataGridView.NotifyCurrentCellDirty(True)
            MyBase.OnTextChanged(eventargs)
        End Sub

        Private Sub TlBoxEditingControl_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.TextChanged
            If Len(Me.Text) > 1 Then
                Lbox.Items.Add("111")
                Lbox.Items.Add("222")
                Lbox.Items.Add("333")
                Lbox.Visible = True
            Else
                Lbox.Items.Clear()
                Lbox.Visible = False
            End If
        End Sub
    End Class
#End Region
End Class


I got an example from codeproject but in that i cant get down arrow key. My purpose is while editing when i press down arrow key i want to get focus listbox and move in listbox and when i press enterkey i want the selected text of listbox in the gridcell.
Posted
Updated 12-Dec-11 18:45pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900