开发者

Reflection and custom ControlDesigner doesn't seem to work in c#

开发者 https://www.devze.com 2023-01-21 01:44 出处:网络
I have made a custom ControlDesigner that I need to include and exclude properties shown in the property grid. But for some reason it seems just to ignore the code? I don\'t know what I might have don

I have made a custom ControlDesigner that I need to include and exclude properties shown in the property grid. But for some reason it seems just to ignore the code? I don't know what I might have done wrong? Could I be missing something? Do I need to setup VS or something?

Also in the examples I have found they seem to disagree about where the remove call should be. In some examples they call it in preFilterProperties method and in some examples they call it in postFilterProperties() method which confuses me. In some examples they call it after running the base.preFilterProperties() method and sometimes before? Can someone please clarify?

Here is the code so far. Preferrably I would also like to add a property with a list of property names that would be excluded but I don't know when to set such a property since it seems that reflection is run before runtime? Or is there any way to accomplish that?

using System.ComponentModel;
using System.Drawing.Design;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;
using System;
using System.Collections;
using System.Collections.Generic;

namespace PropertyEditorProject
{
    [Designer(typeof(YourClassDesigner))]
    class MyPropertyClass
    {
        protected List<string> _MyList;
        protected string _MyName;
        protected string _MyCarModel;
        protected int _MyAge;

        public List<string> MyList 
        {
            get
            {
                return _MyList;
            }
            set
            {
                _MyList = value;
            }
        }

        public string MyName
        {
            get
            {
                return _MyName;
            }
            set
            {
                _MyName = value;
            }
        }

        public string MyCarModel
        {
            get
            {
                return _MyCarModel;
            }
            set
            {
                _MyCarModel = value;
            }
        }

        public int MyAge
        {
            get
            {
                return _MyAge;
            }
       开发者_开发知识库     set
            {
                _MyAge = value;
            }
        }

        public MyPropertyClass()
        {
            _MyList = new List<string>();
            _MyList.Add("Test");
            _MyList.Add("Testing 2");

            _MyName = "TestName";
            _MyCarModel = "Subaru";
            _MyAge = 20;

        }
    }

    internal class YourClassDesigner : ControlDesigner 
    {
        protected override void PreFilterProperties(System.Collections.IDictionary properties)
        {
            properties.Remove("MyAge");
            base.PreFilterProperties(properties);

        }
    }

    static class MainProgram
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Form form = new Form();
            PropertyGrid grid = new PropertyGrid();
            grid.Dock = DockStyle.Fill;
            form.Controls.Add(grid);
            grid.SelectedObject = new MyPropertyClass();
            Application.Run(form);
        }
    }
}

Anyone have any ideas? Grateful for any help


After a bit of searching I found the answer to one of my questions:

"The general rule to follow is to add or remove items in the "PreFilter" methods, and modify existing items in the "PostFilter" methods. Always call the base method first in the PreFilter methods and call the base method last in the PostFilter methods. This ensures that all designer classes are given the proper opportunity to apply their changes."

http://msdn.microsoft.com/en-us/library/ms973820.aspx

Still I can't figure out why the code won't work though :(

0

精彩评论

暂无评论...
验证码 换一张
取 消