Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / XML

Flexible ComboBox and EditingControl

Rate me:
Please Sign up or sign in to vote.
4.80/5 (10 votes)
30 Apr 2012CPOL6 min read 54K   5.7K   37  
Using any Control In ComboBox or in EditingControl for DataGridView
' By NewPast.Net
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Drawing.Design
Imports System.Drawing


<ToolboxBitmap(GetType(GridColumn), "GridColumn.bmp")> _
Public Class GridColumn
    Inherits DataGridViewColumn

    Private _Control As Control
    'dont put DesignerSerializationVisibility(DesignerSerializationVisibility.Content
    'this is only good for readonly property
    'ControlConverter does not work aumatically if property type inhert form componenet or control
    'so we need a ControlText property
    <TypeConverter(GetType(ControlConverter)), _
    Editor(GetType(ControlEditor), GetType(UITypeEditor)), _
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), _
    DefaultValue(GetType(Control), "Nothing"), _
    DisplayName("EditingControl(Create New)"), _
    EditorBrowsable(EditorBrowsableState.Advanced)> _
    Public Overridable Property NewControl() As Control
        Get
            Try
                If _Control Is Nothing OrElse Not String.IsNullOrEmpty(_Control.Name) Then
                    Return Nothing
                Else
                    Return _Control
                End If                
            Catch ex As Exception
                ErrMsg(ex)
                Return Nothing
            End Try
        End Get
        Set(ByVal value As Control)
            Me.Control = value
        End Set
    End Property

    <DefaultValue(GetType(Control), "Nothing"), _
    DisplayName("EditingControl(Created in the form)")> _
        Public Overridable Property Control() As Control
        Get
            Try
                Return _Control
            Catch ex As Exception
                ErrMsg(ex)
                Return Nothing
            End Try
        End Get
        Set(ByVal value As Control)
            Try
                If value Is Nothing Then
                    Exit Property
                ElseIf value Is Me.DataGridView OrElse _
                value.Contains(Me.DataGridView) OrElse
                GetType(Form).IsAssignableFrom(value.GetType) Then
                    Exit Property
                Else
                    _Control = value
                End If
            Catch ex As Exception
                ErrMsg(ex)
            End Try
        End Set
    End Property

    <DefaultValue(""), Browsable(False)> _
    Public Overridable Property ControlText As String
        Get
            Try
                If _Control Is Nothing OrElse Not String.IsNullOrEmpty(_Control.Name) Then
                    Return ""
                Else
                    Dim ControlConverter As New ControlConverter
                    Return CStr(ControlConverter.ConvertTo(_Control, GetType(String)))
                End If
            Catch ex As Exception
                ErrMsg(ex)
                Return Nothing
            End Try
        End Get
        Set(ByVal value As String)
            Try
                If value Is Nothing Then Exit Property
                Dim ControlConverter As New ControlConverter
                Me.Control = CType(ControlConverter.ConvertFrom(value), Control)
            Catch ex As Exception
                ErrMsg(ex)
            End Try
        End Set
    End Property

    <DefaultValue("Text")> _
    Public Overridable Property ValuePropertyName As String = "Text"

    <DefaultValue("")> _
    Public Overridable Property HasValuePropertyName As String = ""

    Public Sub New()
        MyBase.New()

        'NEH
        MyBase.CellTemplate = New GridCell
        MyBase.ValueType = GetType(Object)
    End Sub

    Public Overridable Function EditingControlFormattedValue(ByVal Value As Object, _
                                                ByVal FormattedValue As Object) As Object
        Return FormattedValue
    End Function

    Public Overridable Function PaintFormattedValue(ByVal Value As Object, ByVal FormattedValue _
                                        As Object) As String
        Try
        If FormattedValue Is Nothing Then
            Return ""
        Else
            Return FormattedValue.ToString
        End If
        Catch ex As Exception
            ErrMsg(ex)
            Return ""
        End Try
    End Function

    Public Overridable Function GetValue(ByVal Value As Object) As Object
        Return Value
    End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Lebanon Lebanon
ASP.Net Hosting on Linux server
---------------------------
There is a developer behind every piece of code!
DNA is too complex what about it!
No junk DNA; There is a functional role for noncoding DNA

Comments and Discussions