Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2007

Tip: Looking for answers? Try searching our database.

TypeConverter question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
--== Alain ==-- - 12 Feb 2007 19:33 GMT
Hi,

i've developed my own TypeConverter and i have some issue.

Here is my custom control class definition :

[Category("Appearance")]
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Description("Setup Style, Type and Color of gridlines to draw.")]
[TypeConverter(typeof(CGridLineConverter))]
public CGridLine GridLines
{
...
}

and here is my TypeConverter class :
public class CGridLineConverter : ExpandableObjectConverter
{
...
}

and my CGridLine class :
public class CGridLine
{
...
}

When i test m custom control in TestContainer i have the following behavior.

test 1.
If i test it like that, the property "GridLines" from my custom control
is not displayed in the propertyGrid of TestContainer window.

test 2.
If i comment the line
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
and
[TypeConverter(typeof(CGridLineConverter))]
the property "GridLines" is displayed but disable (tge same for its
value field)

test 3.
If i comment the line
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
and
[TypeConverter(typeof(CGridLineConverter))] is not commented.
the "GridLines" property is disbled, but its value field is colored in
black (the full cell in property Grid)

test 4.
If
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
is not commented
and
[TypeConverter(typeof(CGridLineConverter))] is commented

the "GridLines" property is displayed as accessible, but its value field
displays : ARListView.Design.CGridLine

which correspond to Namespace1.Namespace2.ClassName of my property.

So where is the problem ?
Why property is not displayed when both attributes are "not commented",
so active ?

It's already 4 days that i'm searching and searching without finding
some reason.

thanks a lot for your help,

Al.
Oliver Sturm - 13 Feb 2007 18:00 GMT
Hello Al,

I'm sorry, I'm sure this is not too helpful - but I admit I'm having a
hard time reading and following your long-ish description. If you can post
some code I can just copy and paste into a fresh VS project, I'm willing
to do so and see whether I can find a solution for you.

               Oliver Sturm
Signature

http://www.sturmnet.org/blog

--== Alain ==-- - 13 Feb 2007 19:53 GMT
Ok, so sorry for those who dislike this...here is the code.

My custom control code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MyComponent
{
  public partial class Frame : UserControl
  {

    public Frame()
    {
      InitializeComponent();
      //this.m_GridLines = new CGridLine();
    }

    private CGridLine m_GridLines= new CGridLine();

    [Category("Appearance")]
    [Browsable(true)]

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    [Description("Setup Style, Type and Color of gridlines to draw.")]
    [TypeConverter(typeof(CGridLineConverter))]
    public CGridLine GridLines
    {
      get
      {
        return this.m_GridLines;
      }
      set
      {
        this.m_GridLines = value;
      }
    }

  }
}

my GridLines class and its converter code :

using System;
using System.Drawing;
using System.ComponentModel;
using System.Globalization;
using System.Diagnostics;

namespace MyComponent
{
    /// <summary>
    /// Specifies how a Table draws grid lines between its rows and columns
    /// </summary>
    public enum GridLines
    {
        /// <summary>
        /// No grid lines are drawn
        /// </summary>
        None = 0,

        /// <summary>
        /// Grid lines are only drawn between columns
        /// </summary>
        Columns = 1,

        /// <summary>
        /// Grid lines are only drawn between rows
        /// </summary>
        Rows = 2,

        /// <summary>
        /// Grid lines are drawn between rows and columns
        /// </summary>
        All = 3
    }

  /// <summary>
  /// Specifies the style of the lines drawn when a Table draws its
grid lines
  /// </summary>
  public enum GridLineStyle
  {
    /// <summary>
    /// Specifies a solid line
    /// </summary>
    Solid = 0,

    /// <summary>
    /// Specifies a line consisting of dashes
    /// </summary>
    Dash = 1,

    /// <summary>
    /// Specifies a line consisting of dots
    /// </summary>
    Dot = 2,

    /// <summary>
    /// Specifies a line consisting of a repeating pattern of dash-dot
    /// </summary>
    DashDot = 3,

    /// <summary>
    /// Specifies a line consisting of a repeating pattern of dash-dot-dot
    /// </summary>
    DashDotDot = 4
  }

  [TypeConverter(typeof(CGridLineConverter))]
  public class CGridLine
  {
    #region Class Data

    private GridLines m_GridLines;
    private GridLineStyle m_GridLinesStyle;
    private Color m_GridLinesColor;

    #endregion

    #region Constructor

    public CGridLine()
    {
      this.m_GridLines = GridLines.None;
      this.m_GridLinesColor = System.Drawing.SystemColors.ActiveBorder;
      this.m_GridLinesStyle = GridLineStyle.Solid;
    }

    public CGridLine(GridLines LineType, GridLineStyle LineStyle, Color
LineColor)
    {
      this.m_GridLines = LineType;
      this.m_GridLinesColor = LineColor;
      this.m_GridLinesStyle = LineStyle;
    }
    #endregion

    #region Properties

    /// <summary>
    /// Style of the GridLines
    /// </summary>
    ///
    [RefreshProperties(RefreshProperties.Repaint)]
    [NotifyParentProperty(true)]
    [DefaultValue(typeof(GridLineStyle), "Solid")]
    public GridLineStyle Style
    {
      get
      {
        return this.m_GridLinesStyle;
      }
      set
      {
        this.m_GridLinesStyle = value;
      }
    }

    /// <summary>
    /// Types of GridLines
    /// </summary>
    [RefreshProperties(RefreshProperties.Repaint)]
    [NotifyParentProperty(true)]
    [DefaultValue(typeof(GridLines), "None")]
    public GridLines Lines
    {
      get
      {
        return this.m_GridLines;
      }
      set
      {
        this.m_GridLines = value;
      }
    }

    /// <summary>
    /// Color of the GridLine
    /// </summary>
    [RefreshProperties(RefreshProperties.Repaint)]
    [NotifyParentProperty(true)]
    [DefaultValue(typeof(Color), "ActiveBorder")]
    public Color Color
    {
      get
      {
        return this.m_GridLinesColor;
      }
      set
      {
        this.m_GridLinesColor = value;
      }
    }

    #endregion

  }

  public class CGridLineConverter : ExpandableObjectConverter
  {
    /// <summary>
    /// Check if it the parameter (context) is a string and can convert
it to class type (destinationType)
    /// </summary>
    /// <param name="context">string that holds the class type
information</param>
    /// <param name="destinationType">class type to which the context
must be converted</param>
    /// <returns>true if it's possible, or false in case of impossible
conversion</returns>

    public override bool CanConvertTo(ITypeDescriptorContext
context,Type destinationType)
    {
      if (destinationType == typeof(string))
      {
        return true;
      }
      else
      {
        return base.CanConvertTo(context, destinationType);
      }
    }

    /// <summary>
    ///
    /// </summary>
    /// <param name="context"></param>
    /// <param name="culture"></param>
    /// <param name="value"></param>
    /// <param name="destinationType"></param>
    /// <returns></returns>

    public override object ConvertTo(ITypeDescriptorContext context,
CultureInfo culture, object value, Type destinationType)
    {
      if (destinationType == typeof(string))
      {
        return ToString(value);
      }
      else
      {
        return base.ConvertTo(context, culture, value, destinationType);
      }
    }

    /// <summary>
    /// ConverToString whole subproperties
    /// </summary>
    /// <param name="value">value holds by CGridLine property</param>
    /// <returns>Formatted string that holds the CGridLine property
converted into text</returns>
    private string ToString(object value)
    {
      CGridLine m_GridLine = (CGridLine)value;
      ColorConverter ColConverter = new ColorConverter();
      StringConverter StrConverter = new StringConverter();

      return String.Format("{0}, {1}, {2}",
        m_GridLine.Lines.ToString(),
        m_GridLine.Style.ToString(),
        ColConverter.ConvertToString(m_GridLine.Color));
    }

    public override bool CanConvertFrom(ITypeDescriptorContext
context,Type sourceType)
    {
      if (sourceType == typeof(string))
      {
        return true;
      }
      else
      {
        return base.CanConvertFrom(context, sourceType);
      }
    }

    public override object ConvertFrom(ITypeDescriptorContext context,
CultureInfo culture, object value)
    {
      if (value is string)
      {
        return FromString(value);
      }
      else
      {
        return base.ConvertFrom(context, culture, value);
      }
    }

    private CGridLine FromString(object value)
    {
      string[] values = ((string)value).Split(',');
      if (values.Length != 3)
        throw new ArgumentException("Could not convert the value !");
      try
      {
        CGridLine my_GridLine = new CGridLine();
        ColorConverter ColConverter = new ColorConverter();
        StringConverter StrConverter = new StringConverter();

        my_GridLine.Lines = (GridLines)Enum.Parse(typeof(GridLines),
values[0], true);
        my_GridLine.Style =
(GridLineStyle)Enum.Parse(typeof(GridLineStyle), values[1], true);
        my_GridLine.Color =
(Color)ColConverter.ConvertFromString(values[2]);

        return my_GridLine;
      }
      catch (Exception err)
      {
        String ErrorMsg = "Could not convert the value \n\n Error
Message : " + err.Message;
        throw new ArgumentException(ErrorMsg);
      }
    }
  }
}

You can cut and paste into a brand new usercontrol and check what is the
behavior based on my previous post regarding test process.

Alain.

> Hello Al,
>
[quoted text clipped - 4 lines]
>
>                Oliver Sturm

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.