System.ComponentModel.Design.ArrayEditor - How to

Discussion in 'ASP .Net Building Controls' started by Peter Ehli, Aug 19, 2004.

  1. Peter Ehli

    Peter Ehli Guest

    I have a custom control which is a set of tab images like in hotmail. In my control I have a hard coded array that sets the string
    value of the tabs.

    private String[] tabCaptions = { "Home", "Tab 1", "Tab 2", "Tab 3" };

    I would like these tab captions set by the user of my control via the properties window. i.e. the code below that I have so far.

    using System;
    using System.ComponentModel.Design;

    namespace NetParadigm.WebServerControls
    {
    public class TabEditor : ArrayEditor
    {
    public TabEditor() : base (typeof ( String ) )
    {
    }
    }
    }

    Then I specify this editor via the editor attribute for my TabCaptions property as below

    private String[] tabCaptions;

    [EditorAttribute("TabEditor",typeof(System.Drawing.Design.UITypeEditor))]
    public String[] TabCaptions
    {
    get
    {
    return tabCaptions;
    }
    set
    {
    tabCaptions = value;
    }
    }

    This is just stubbed code and as far as I could get. This gives me an editor (i.e. click on the ellipse) window exactly like a
    windows forms textbox when you set the Lines property values.

    When these string values are set via this edior when my control is on a aspx page I get the below in Html view for the control

    <cc1:NP_TabStrip id=NP_TabStrip1 runat="server" TabCaptions="String[] Array">

    Of course I need to persist these values via view state and TabCaptions="String[] Array" bombs big time!

    I've searched everywhere for an example of how to set a string array in my control and come up with a lot of fragments and unrelated
    stuff. The Kothari book has an example of a CollectionEditor that is way too complex to disseminate for my use. Any ideas or a good
    example of how to do this would be greatly appreciated. Thanks in advance.

    Peter Ehli
     
    Peter Ehli, Aug 19, 2004
    #1
    1. Advertising

  2. Peter Ehli

    ChrisAdams Guest

    You need to change the default serialization behaviour that the designer uses
    by
    adding attributes to the property.

    [DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
    PersistenceMode( PersistenceMode.Attribute )]

    This should force the dte to serialize the content of your property ie the
    string values, instead of the property itself, ie an array.

    Chris
     
    ChrisAdams, Aug 20, 2004
    #2
    1. Advertising

  3. Peter Ehli

    Peter Ehli Guest

    Hi Chris,
    My property attributes are below and as you can see I added the DesignerSerializationVisibility and PersistenceMode attribute to my
    property as you suggested.

    [EditorAttribute( "TabEditor",typeof( System.Drawing.Design.UITypeEditor )),
    DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
    PersistenceMode( PersistenceMode.Attribute )]
    public String[] TabCaptions
    {
    get
    {
    return tabCaptions;
    }
    set
    {
    tabCaptions = value;
    }
    }

    private String[] tabCaptions;

    When I set the string values i.e. TabCaptons via the editor for my control on an aspx page I get the below in Html view from the
    aspx page.

    <cc1:NP_TabStrip id=NP_TabStrip1 runat="server" TabCaptions-Rank="1" TabCaptions-IsReadOnly="False"
    TabCaptions-SyncRoot="System.String[]" TabCaptions-IsSynchronized="False" TabCaptions-IsFixedSize="True" TabCaptions-Length="4"
    TabCaptions-LongLength="4" TabCaptions="String[] Array">

    This gives me a parser error below when trying to render the page in the browser

    Parser Error Message: Cannot create an object of type 'System.String[]' from its string representation 'String[] Array' for the
    'TabCaptions' property.

    Again below is my designer class.

    using System;
    using System.ComponentModel.Design;

    namespace NetParadigm.WebServerControls
    {
    public class TabEditor : ArrayEditor
    {
    public TabEditor() : base (typeof ( String ) )
    {
    }
    }
    }

    Any ideas on whats wrong? Thanks Chris!

    <nospam=""/><nospam>



    "ChrisAdams" <> wrote in message news:...
    > You need to change the default serialization behaviour that the designer uses
    > by
    > adding attributes to the property.
    >
    > [DesignerSerializationVisibility( DesignerSerializationVisibility.Content ),
    > PersistenceMode( PersistenceMode.Attribute )]
    >
    > This should force the dte to serialize the content of your property ie the
    > string values, instead of the property itself, ie an array.
    >
    > Chris
     
    Peter Ehli, Aug 20, 2004
    #3
  4. Peter Ehli

    ChrisAdams Guest

    Whoops,

    I forgot that it only works for properties that expose a collection object,
    and you need to
    persist it as an inner property.

    What you probably need to do is specify the TypeConverter that should be
    used to get
    it from an array to a string representation. Take a look at ArrayConverter
    and the
    attribute you need to apply is.

    [TypeConverter( typeof( System.ComponentModel.ArrayConverter) )]

    Chris.
     
    ChrisAdams, Aug 20, 2004
    #4
  5. Peter Ehli

    Peter Ehli Guest

    Thanks Chris, I believe my next step is to work through chapter 10 and the examples of the Kothari book. This chapter explains
    typeconveters, inner
    property persistence, and designer serialization and so forth. Again many thanks for pointing me in the right direction.

    Peter Ehli



    "ChrisAdams" <> wrote in message news:...
    > Whoops,
    >
    > I forgot that it only works for properties that expose a collection object,
    > and you need to
    > persist it as an inner property.
    >
    > What you probably need to do is specify the TypeConverter that should be
    > used to get
    > it from an array to a string representation. Take a look at ArrayConverter
    > and the
    > attribute you need to apply is.
    >
    > [TypeConverter( typeof( System.ComponentModel.ArrayConverter) )]
    >
    > Chris.
     
    Peter Ehli, Aug 20, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?QVNQIERldmVsb3Blcg==?=

    System.ComponentModel.DefaultValue doesn't work with user control

    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=, Sep 13, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,688
    =?Utf-8?B?QVNQIERldmVsb3Blcg==?=
    Sep 13, 2006
  2. Replies:
    0
    Views:
    484
  3. shahin
    Replies:
    0
    Views:
    481
    shahin
    Aug 30, 2007
  4. brik

    Preventing users from adding items in ArrayEditor

    brik, Oct 25, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    147
  5. brik

    Preventing users from adding items in ArrayEditor

    brik, Oct 25, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    115
Loading...

Share This Page