🏠 » Software, Coding & Hosting » NewPast Software » Time Edit Control

Time Edit Control

    Introduction

    Similar Library

    1. Xceed Combo and Grid
    2. DXperience

    How this Library Differs

    Xceed DXperience This Library
    Direct assignment to any control to combo Yes No Yes
    Direct assignment to any control to column Yes Choose for pre stored inline editors Yes
    Supported Grid Xceed Grid DXperience Grid DataGridView
    Can choose control that is not present in the form No No Yes
    Freeware No No Yes
    Lightweight simple library No No Yes
    Has many other useful components Yes Yes No

    Why Do I Create This Application?

    1. I need to extend the DataGridViewColumn without rewriting it each time.
    2. I need a flexible combobox that accepts any control as a dropdown.

    What Does This Application Do and Why is it Useful?

    1. It could be used to build your own grid column or your own combo in a simple way.
    2. Use the included control convert to clone copy and create controls dynamically at run and design time.
    3. Use the included control editor to change the control type that you create at design time.
    4. Both control converter and control editor could be used as TypeConverter and Editor attribute in developing new controls.

    What Problem is Solved

    1. Our component has a control property that creates controls of different types at design time and stores the created control as simple text value.
    2. Value property name of the drop down control or editing control could be specified as needed at design time, for example:
      Control Value Property Name
      NumericUpDown Text
      CheckedListBox CheckedItems
      TimeEdit Value
    3. Value property name could be list or collection and the value will converted to string that contains all items in the collection.

    Using the Code

    1. Create new forms in Windows application.
    2. Add reference to AnyControlComboColumn.
    3. Add new Data Grid View and add new column and set its type to GridColumn.
    4. In the property window, go to EditingControl(Create New) property and choose the control type from the drop down list.
    5. From tool box, add to the form DropDownCombo.
    6. In the property window, go to DropDownButton property and expand it.
    7. Under DropDownButton, go to DropDownControl(Create New) property and choose the control type form the drop down list.

    The Demo Application

    The demo application contains DropDownCombo that we could choose the drop down control of it to be: CheckedListBox or DirList or FileList, and it contains a DataGridView with two columns – the first with NumericUpDown as an editing control and the second column is with Mask Text Box as an editing control.

    About this Library

    This library contains the following classes:

    1. DropDownButton: This class provides a drop down button that accepts any control as a drop down control and any other control to be the display area.
    2. DropDownCombo is a text box that contains the above drop down button and so it provides text box with drop down control that could be set or changed at run time or at design time.
    3. ControlConverter and ControlEditor provide technique to serialize any control to simple text to be stored in the form designer.
    4. EditingControl, GridCell and GridColumn provide the ability to use any control as a editing control in DataGridView.

    About C# Code in This Article

    This library is developed using VB. The C# code herein is obtained by code converters. For more information about code converters, please see:

    About the DropDownButton

    1. The drop down button is the main class for creating drop down combos.
    2. When it is added to any control, it will be docked to the right side.
    3. It could be assigned with two controls: the first is the drop down control and the second is the display area.
    4. The display area in the control will show the value of the created combo and could be a text box or rich text box or any control
    5. The drop down control is the control that will be dropped down when the drop down button is clicked.
    6. To set the drop down control, we could choose a control in the form or choose from a list that will contain all control types in system.windows.forms assembly and assemblies that the application include a reference to them.
    7. When clicking the drop down button, the drop down value will be changed to meet the value of the display area
    8. When the drop down popup is closed, the display area value will be changed to meet the value of the drop down control
    9. DropDownCombo is a Text box that contains drop down button.

    About the GridColumn

    1. GridColumn is the class which is used to extend the DataGridView to accept any control as editing control.
    2. To use this class, we need to use DataGridView and add GridColum to it from DataGridView Designer window.
    3. To set the editing control, we could choose a control in the form or choose form a list that will contain all control types in system.windows.forms assembly and assemblies that the application include a reference to them.

    About ControlConverter and Control Properties Serialization

    Before building the control property of the drop down control, we need to serialize it to string to be stored in the form designer but don’t use XmlSerializer to serialize control properties because:

    1. XmlSerializer not supported types:
      • ISite
      • IDictionary
      • Any class that Implements unsupported interface
      • Any class has property that return unsupported interface
      • Control
      • PropertyDescriptorCollection
      • SortedList
      • Collection
      • TreeNodeCollection
      • TreeNode
      • Any class that Inherits not supported class
      • Any class has property that returns unsupported class
    2. Also XmlSerializer can serialize the following types, but it can’t de-serialize them:
      • ArrayList
      • List of Objects
      • ListBox.ObjectCollection
      • CheckedListBox.ObjectCollection
    3. XmlSerializer returns multi line value with and it is better to store the control property as simple text
    4. To test XmlSerializer, we could use the following code:
    Imports System.Xml.Serialization
    Imports System.Reflection
    Imports System.ComponentModel
    Imports AnyControlComboColumn
    Imports System.Windows.Forms
    
    Public Module Module1
        Public Function Serialize(ByVal Value As Object) As String
            Try
                If Value Is Nothing Then
                    Return ""
                    Exit Function
                End If
                Dim Serializer As New XmlSerializer(Value.GetType)
                Dim vMemoryStream As New IO.MemoryStream
                Serializer.Serialize(vMemoryStream, Value)
                Dim s = System.Text.Encoding.UTF8.GetString(vMemoryStream.ToArray)
                s = s.Replace("<?xml version=""1.0""?>", _
    		"<?xml version=""1.0"" encoding=""utf-8""?>")
                vMemoryStream = New IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s))
    
                s = System.Text.Encoding.UTF8.GetString(vMemoryStream.ToArray)
    
                'Try to Deserialize
                Try
                    Dim f = Serializer.Deserialize(vMemoryStream)
                Catch ex As Exception
                    Return ""
                End Try
                Return s
            Catch ex As Exception
                Return ""
            End Try
        End Function
    End Module
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Xml.Serialization;
    using System.Reflection;
    using System.ComponentModel;
    using AnyControlComboColumn;
    using System.Windows.Forms;
    
    namespace AnyControlComboColumn {
        public static class MSerialize {
            public static string Serialize(object Value) {
                try {
                    if (Value == null) {
                        return "";
                        return null;
                    }
                    XmlSerializer Serializer = new XmlSerializer(Value.GetType());
                    System.IO.MemoryStream vMemoryStream = new System.IO.MemoryStream();
                    Serializer.Serialize(vMemoryStream, Value);
                    var s = System.Text.Encoding.UTF8.GetString(vMemoryStream.ToArray());
                    s = s.Replace("<?xml version=\"1.0\"?>", "<?xml version=\"1.0\" encoding=\"utf-8\"?>");
    
                    vMemoryStream = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(s));
    
                    s = System.Text.Encoding.UTF8.GetString(vMemoryStream.ToArray());
    
                    //>Try to Deserialize
                    try {
                        var f = Serializer.Deserialize(vMemoryStream);
                    } catch (Exception ex) {
    
                        return "";
                    }
                    return s;
                } catch (Exception ex) {
                    return "";
                }
            }
        }
    }

    We don’t use the default ComponentConverter because:

    1. It only allows to select from a pre created controls in the form.
    2. It does not allow to choose a control that is not present in the form.
    3. It can be used at design time only and can’t be used at run time.

    What ComponentConverter does:

    1. Creates an Instance of the control from the selected type.
    2. Converts the control to string to be stored as ControlText.
    3. Converts the control text to control object.

    Points of Interest

    To assign drop down control to the drop down button, we use the following properties:

    'Don't add TypeConverter attribute so this property use the default ComponentConverter
    <DisplayName("DropDownControl(Created in the form)")> _
    Public Property DropDownControl() As Control
    
    <TypeConverter(GetType(ControlConverter)), _
    Editor(GetType(ControlEditor), GetType(UITypeEditor)), _
    DisplayName("DropDownControl(Create New)")> _
    Public Property NewDropDownControl() As Control
    
    <Description("a text that will be used to create new DropDownControl")> _
    Public Overridable Property ControlText As String
    //dont put DesignerSerializationVisibility(DesignerSerializationVisibility.Content
    //this is only good for readonly property
    //ControlConverter does not work automatically if property type inherits form componenet or control
    //so we need a ControlText property
    [TypeConverter(typeof(ControlConverter)), Editor(typeof(ControlEditor), typeof(UITypeEditor)), 
    DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden), 
    Description("Create new control displayed when OpenDropDown."), 
    DefaultValue(typeof(Control), "Nothing"), Category("DropDown"), 
    DisplayName("DropDownControl(Create New)"), EditorBrowsable(EditorBrowsableState.Advanced)]
    public Control NewDropDownControl { 
    //...........
    }
    
    [DefaultValue(""), Browsable(false), 
            Description("a text that will be used to create new DropDownControl")]
    public virtual string ControlText {
    //.................
    }
    
    //Don't add TypeConverter attribute so this property use the default ComponentConverter
    [Description("The control displayed when OpenDropDown."), 
    DefaultValue(typeof(Control), "Nothing"), Category("DropDown"), 
    DisplayName("DropDownControl(Select form the form controls)")]
    public Control DropDownControl {
    //................
    }
    1. The DropDownControl property is named in the property windows as DropDownControl (created in the form) uses the default ComponentConverter to choose the control from already created control that is present in the parent form
    2. NewDropDownControl property is named in the property windows as DropDownControl (create New) uses our control converter and control editor to select a control from a list containing all supported controls for applications or we could type the control name directly such as: TextBox, Button and so on.
    3. ControlText property is a simple text property that accepts and returns a text that includes the control type name followed with the assembly name and the control none default properties values.

    The control converter uses the following code to covert the text to control:

     Public Overrides Function ConvertFrom(ByVal context As ITypeDescriptorContext, _
     ByVal culture As System.Globalization.CultureInfo, ByVal value As Object) As Object
        Try
            If value Is Nothing OrElse (Not TypeOf value Is String) _
            OrElse String.IsNullOrEmpty(CType(value, String)) Then
                Return Nothing
                Exit Function
            End If
            Dim vPropertyValues As New Dictionary(Of String, String)
    
            Dim TypeParts = Split(CType(value, String), "@")
            If UBound(TypeParts) <> 2 Then
                ReDim Preserve TypeParts(2)
            End If
            If String.IsNullOrEmpty(TypeParts(1)) Then
                TypeParts(1) = WinFormsAssemblyName
            End If
            vPropertyValues.Add(c_TypeName, TypeParts(0))
            vPropertyValues.Add(c_AssemblyName, TypeParts(1))
    
            Dim aPropertyValues() As String = Split(TypeParts(2), ",")
               For i As Integer = 0 To UBound(aPropertyValues)
                  If (Not String.IsNullOrEmpty(aPropertyValues(i))) AndAlso _
                    aPropertyValues(i).Contains(":"c) Then
                    Dim KeyAndValue = Split(aPropertyValues(i), ":")
                    vPropertyValues.Add(KeyAndValue(0).Trim, KeyAndValue(1).Trim)
                End If
            Next
            Return Me.CreateInstance(Nothing, vPropertyValues)
        Catch ex As Exception
            ErrMsg(ex)
            Return Nothing
        End Try
    End Function
    public override object ConvertFrom(ITypeDescriptorContext context, 
                    System.Globalization.CultureInfo culture, object value) {
        try {
            if (value == null || (! (value is string)) || string.IsNullOrEmpty(Convert.ToString(value))) {
                return null;
                return null;
            }
            Dictionary<string,> vPropertyValues = new Dictionary<string,>();
    
            string[] TypeParts = Microsoft.VisualBasic.Strings.Split(Convert.ToString(value), 
                                     "@", -1, Microsoft.VisualBasic.CompareMethod.Binary);
            if (TypeParts.GetUpperBound(0) != 2)
                Array.Resize(ref TypeParts, 3);
            if (string.IsNullOrEmpty(TypeParts[1]))
                TypeParts[1] = WinFormsAssemblyName;
            vPropertyValues.Add(c_TypeName, TypeParts[0]);
            vPropertyValues.Add(c_AssemblyName, TypeParts[1]);
    
            string[] aPropertyValues = Microsoft.VisualBasic.Strings.Split(TypeParts[2], 
                                        ",", -1, Microsoft.VisualBasic.CompareMethod.Binary);
            for (int i = 0; i <= aPropertyValues.GetUpperBound(0); i++) {
                if ((! (string.IsNullOrEmpty(aPropertyValues[i]))) && aPropertyValues[i].Contains(":")) {
                    string[] KeyAndValue = Microsoft.VisualBasic.Strings.Split(aPropertyValues[i], ":", 
                                                 -1, Microsoft.VisualBasic.CompareMethod.Binary);
                    vPropertyValues.Add(KeyAndValue[0].Trim(), KeyAndValue[1].Trim());
                }
            }
            return CreateInstance(null, vPropertyValues);
        } catch (Exception ex) {
            _ErrMsg.ErrMsg(ex);
            return null;
        }
    }
    1. The Text format is [email protected]@ControlPropertyValues.
    2. First of all, the converter gets the type name and the assembly name properties values.
    3. It create a properties values dictionary which consist of a pair of properties names and their values converted to string using the property assigned type converter in addition to the assembly and type names.
    4. Create an instance of the control using CreateInstance method.
    5. To create the instance of the object form the type object, we use the following code:
      Public Overloads Shared Function CreateInstance(ByVal vType As Type) As Control
              Try
                  '####### Dim c As New vType is not correct
                  If vType Is Nothing Then
                      Return Nothing
                      Exit Function
                  End If
                  Dim ConstructorInfo = vType.GetConstructor({})
                  If ConstructorInfo Is Nothing Then
                      Return Nothing
                      Exit Function
                  End If
                  Return CType(ConstructorInfo.Invoke({}), Control)
              Catch ex As Exception
                  ErrMsg(ex)
                  Return Nothing
              End Try
          End Function
      public static Control CreateInstance(Type vType) {
          try {
              //> Dim c As New vType is not correct
              if (vType == null) {
                  return null;
                  return null;
              }
              var ConstructorInfo = vType.GetConstructor(new System.Type[0]);
              if (ConstructorInfo == null) {
                  return null;
                  return null;
              }
              return (Control)(ConstructorInfo.Invoke(new System.Type[0]));
          } catch (Exception ex) {
              _ErrMsg.ErrMsg(ex);
              return null;
          }
      }
    6. To get the type object from the type name, we use the following code:
      Dim vAssembly = System.Reflection.Assembly.Load(AssemblyName)
      Dim vType = vAssembly.GetType(TypeName)
      var vAssembly = System.Reflection.Assembly.Load(AssemblyName);
      var vType = vAssembly.GetType(TypeName);

    To view the drop down control, I use ToolStripDropDown and ToolStripControlHost:

    Private WithEvents Popup As ToolStripDropDown
    Private Host As ToolStripControlHost
    
    Public Property DropDownControl() As Control
        Get
            Return _DropDownControl
        End Get
        Set(ByVal value As Control)
            Try
               '....some code for testing and preparing value the cloning it to
                NewControl = ControlConverter.Clone(value)
                Popup.Items.Clear()
                Host = Nothing
                _DropDownControl = NewControl
                _DropDownControl.Margin = Padding.Empty
                _DropDownControl.Padding = Padding.Empty
                Host = New ToolStripControlHost(_DropDownControl)
                Host.Margin = Padding.Empty
                Host.Padding = Padding.Empty
                Host.AutoSize = False
                Host.Size = value.Size
                Popup.Items.Add(Host)
                Popup.Size = Host.Size
                Popup.AutoSize = True
                _DropDownControl.Location = New Point(0, 0)
                If _DisplayArea IsNot Nothing Then
                    DisplayAreaValue = DropDownControlValue
                End If
                OnClosed(Nothing, Nothing)
            Catch ex As Exception
                ErrMsg(ex)
            End Try
        End Set
    End Property
    
    Protected Overrides Sub OnClick(ByVal e As EventArgs)
        Try
            MyBase.OnClick(e)
            If Me.DroppedDown Then
                Me.CloseDropDown()
            Else
                Me.OpenDropDown() 'Using Popup.Show method
            End If
        Catch ex As Exception
            ErrMsg(ex)
        End Try
    End Sub
    
    Public Overridable Sub OpenDropDown()
        Try
            If _DropDownControl Is Nothing OrElse Host Is Nothing OrElse _
            _DisplayArea Is Nothing Then Exit Sub
            Dim frm = _DisplayArea.FindForm
            Dim Pos As Point
            'additional code to adjust the position according to Drop Down Direction
            Pos = New Point(_DisplayArea.Left, _DisplayArea.Bottom + 1)
            Popup.Show(frm, Pos, ToolStripDropDownDirection.BelowRight)
    
        Catch ex As Exception
            ErrMsg(ex)
        End Try
    End Sub

    History

    • Initial version

    Links

    https://www.codeproject.com/KB/combobox/FlexibleEditingControl.aspx

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    End