Design time support for custom control
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Design time support for custom control

Hybrid View

  1. #1
    Join Date
    Apr 2012
    Posts
    3

    Question Design time support for custom control

    Hi all,

    I'm creating a specific DataGridView control that includes two scrollbars so that I can control their size and visibility. So, I have created a User control that contains the DataGridView and the two scrollbars inside a 2x2 TableLayoutPanel.

    The issue I'm facing is that when I add my control to a Windows Form, I can't set my DataGridView's DataSource or define the column styles from the design view, because the link that I use for that when I use a common DataGridView (that little white arrow appearing on the top right side of the DataGridView) is not appearing.

    While doing some testing, I saw that if I inherit my control from DataGridView, that link appears. But if I do that, I guess that I can only extend the functionality for the DataGridView, but I need my control to be a composite control.

    So, that's my question: how can I get those design-time capabilities on my custom control? I am specially interested on the columns layout and the datasource.

    Best regards.

  2. #2
    Join Date
    Apr 2012
    Posts
    2

    Re: Design time support for custom control

    I'm sure you figured it out by now, but in case you didn't....

    your user control needs to have properties exposing its controls.

    In your user control class add following property:

    Code:
            public DataGridView InnerDataGrid
            {
                get
                {
                    return dataGridView1;  //This should be the name of your DGV
                }
            }
    Now, in design time ,you'll see the property "InnerDataGrid"

  3. #3
    Join Date
    Apr 2012
    Posts
    3

    Re: Design time support for custom control

    Quote Originally Posted by Wamican-IT View Post
    I'm sure you figured it out by now, but in case you didn't....

    your user control needs to have properties exposing its controls.

    In your user control class add following property:

    Code:
            public DataGridView InnerDataGrid
            {
                get
                {
                    return dataGridView1;  //This should be the name of your DGV
                }
            }
    Now, in design time ,you'll see the property "InnerDataGrid"
    Hi Wamican,

    First of all, thanks for your response.

    I already did the thing you mention. I have defined several other properties (MyDataSource, for example) and I have had no problem with them. The thing is that I have defined a public property called "MyColumns", that I was thinking to use as a link to my inner grid's "Columns" property.

    I have programmed the property and I have assigned it some attributes so that it appears in design-time:

    [Description("Determines the column layout for the control grid."), Browsable(true), DefaultValue(null), Category("MySettings")]
    public DataGridViewColumnCollection MyColumns
    {
    ...
    }

    It does, but the DataGridView Columns editor is not the same one as on a Common Controls DataGridView's Columns property editor. It is the editor for a generic DataGridViewColumnCollection, without the possibility to set the columns' "ColumnType" property, making it impossible to define DataGridViewComboBoxColumns, for example.

    So, my question is that: how can I have on my custom control the same "Columns" property editor as on a normal DataGridView?

    Best regards.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Design time support for custom control

    Design-time support requires additional effort, and if you need some of the more advanced capabilities, this effort is not trivial, but it's generally also not too overwhelming, so if you're up for it, here's the documentation you need to consult.

    For simpler tasks, it can be no more complicated than adding some attributes to properties which make them visible in the Properties window in the IDE. But tasks like providing custom editors involve creating your own editor classes - by deriving from the existing ones, or from scratch.

  5. #5
    Join Date
    Jan 2010
    Posts
    1,102

    Re: Design time support for custom control

    P.S. Oh, but, given that you're implementing a composite control, you could maybe manage to avoid writing too much code, and re-inventing the wheel.
    I'm talking off the top of my head here, but, you might be able to re-use existing editor classes - by making a custom editor that only determines which of the components of the composite is selected, and if it's the DataGridView, it forwards all the calls to the existing data grid view editor, provided by the framework.

  6. #6
    Join Date
    Apr 2012
    Posts
    3

    Re: Design time support for custom control

    Quote Originally Posted by TheGreatCthulhu View Post
    P.S. Oh, but, given that you're implementing a composite control, you could maybe manage to avoid writing too much code, and re-inventing the wheel.
    I'm talking off the top of my head here, but, you might be able to re-use existing editor classes - by making a custom editor that only determines which of the components of the composite is selected, and if it's the DataGridView, it forwards all the calls to the existing data grid view editor, provided by the framework.
    Hi,

    Thanks for your response. The approach you are telling me was the one I was thinking on.

    I have written an attribute for my property like this:

    [Editor("DataGridViewColumnCollectionEditor", "UITypeEditor")]
    public DataGridViewColumnCollection MyColumns
    {
    .....
    }

    Doing that makes a generic DataGridView column collection editor appear on design-time, but it's slightly different from the DataGridView's columns editor. See the two images below:

    My editor:
    http://imageshack.us/photo/my-images/193/mycontrol.png/

    DataGridView editor:
    http://imageshack.us/photo/my-images...agridview.png/

    I guess that if I put the correct attributes to my property, I could achieve that the DataGridView's editor appears on my property when clicking on it on design-time, but I don't really know what to put there so that it works like that.

    Best regards.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center