Foxtable(狐表)用户栏目专家坐堂 → 请教C#代码如何转还到全局代码执行


  共有1807人关注过本帖树形打印复制链接

主题:请教C#代码如何转还到全局代码执行

帅哥哟,离线,有人找我吗?
jiangxun
  1楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:876 积分:5929 威望:0 精华:0 注册:2014/8/3 12:15:00
请教C#代码如何转还到全局代码执行  发帖心情 Post By:2017/11/11 12:09:00 [只看该作者]

示例窗体程序
namespace PropertyGridEx
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitializePropertyGrid();
        }
        private void InitializePropertyGrid()
        {
            CustomPropertyCollection collection = new CustomPropertyCollection();
            collection.Add(new CustomProperty("标题", "Text", "数据", "要显示的内容。", label1, typeof(System.ComponentModel.Design.MultilineStringEditor)));
            collection.Add(new CustomProperty("背景色", "BackColor", "外观", "背景色。", label1));
            collection.Add(new CustomProperty("颜色", "ForeColor", "外观", "前景色。", label1));
            collection.Add(new CustomProperty("字体", "Font", "外观", "字体。", label1));
            propertyGrid1.SelectedObject = collection;
        }
    }
}
CustomProperty("标题", "Text", "数据", "要显示的内容。", label1, typeof(System.ComponentModel.Design.MultilineStringEditor)));
在如下C#程序里
namespace PropertyGridEx
{
    public class CustomProperty
    {
        #region Private Variables
        private string _name = string.Empty;
        private object _defaultValue = null;
        private object _value = null;
        private object _objectSource = null;
        private PropertyInfo[] _propertyInfos = null;
        #endregion
        #region Contructors
        public CustomProperty()
        {
        }
        public CustomProperty(string name, string category, string description, object objectSource)
            : this(name, name, null, category, description, objectSource, null)
        {
        }
        public CustomProperty(string name, string propertyName, string category, string description, object objectSource)
            : this(name, propertyName, null, category, description, objectSource, null)
        {
        }
        public CustomProperty(string name, string propertyName, string category, string description, object objectSource, Type editorType)
            : this(name, propertyName, null, category, description, objectSource, editorType)
        {
        }
        public CustomProperty(string name, string propertyName, Type valueType, string category, string description,
            object objectSource, Type editorType)
            : this(name, new string[] { propertyName }, valueType, null, null, false, true, category, description, objectSource, editorType)
        {
        }
        public CustomProperty(string name, string[] propertyNames, string category, string description, object objectSource)
            : this(name, propertyNames, category, description, objectSource, null)
        {
        }
        public CustomProperty(string name, string[] propertyNames, string category, string description, object objectSource, Type editorType)
            : this(name, propertyNames, null, category, description, objectSource, editorType)
        {
        }
        public CustomProperty(string name, string[] propertyNames, Type valueType, string category, string description,
            object objectSource, Type editorType)
            : this(name, propertyNames, valueType, null, null, false, true, category, description, objectSource, editorType)
        {
        }
        public CustomProperty(string name, string[] propertyNames, Type valueType, object defaultValue, object value,
            bool isReadOnly, bool isBrowsable, string category, string description, object objectSource, Type editorType)
        {
            Name = name;
            PropertyNames = propertyNames;
            ValueType = valueType;
            _defaultValue = defaultValue;
            _value = value;
            IsReadOnly = isReadOnly;
            IsBrowsable = isBrowsable;
            Category = category;
            Description = description;
            ObjectSource = objectSource;
            EditorType = editorType;
        }
        #endregion
        #region Public Properties
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (PropertyNames == null)
                {
                    PropertyNames = new string[] { _name };
                }
            }
        }
        public string[] PropertyNames { get; set; }
        public Type ValueType { get; set; }
        public object DefaultValue
        {
            get { return _defaultValue; }
            set
            {
                _defaultValue = value;
                if (_defaultValue != null)
                {
                    if (_value == null) _value = _defaultValue;
                    if (ValueType == null) ValueType = _defaultValue.GetType();
                }
            }
        }
        public object Value
        {
            get { return _value; }
            set
            {
                _value = value;
                OnValueChanged();
            }
        }
        public bool IsReadOnly { get; set; }
        public string Description { get; set; }
        public string Category { get; set; }
        public bool IsBrowsable { get; set; }
        public object ObjectSource
        {
            get { return _objectSource; }
            set
            {
                _objectSource = value;
                OnObjectSourceChanged();
            }
        }
        public Type EditorType { get; set; }
        #endregion
        #region Protected Functions
        protected void OnObjectSourceChanged()
        {
            if (PropertyInfos.Length == 0) return;
            object value = PropertyInfos[0].GetValue(_objectSource, null);
            if (_defaultValue == null) DefaultValue = value;
            _value = value;
        }
        protected void OnValueChanged()
        {
            if (_objectSource == null) return;
            foreach (PropertyInfo propertyInfo in PropertyInfos)
            {
                propertyInfo.SetValue(_objectSource, _value, null);
            }
        }
        protected PropertyInfo[] PropertyInfos
        {
            get
            {
                if (_propertyInfos == null)
                {
                    Type type = ObjectSource.GetType();
                    _propertyInfos = new PropertyInfo[PropertyNames.Length];
                    for (int i = 0; i < PropertyNames.Length; i++)
                    {
                        _propertyInfos[i] = type.GetProperty(PropertyNames[i]);
                    }
                }
                return _propertyInfos;
            }
        }
        #endregion
        #region Prublic Functions
        public void ResetValue()
        {
            Value = DefaultValue;
        }
        #endregion
    }
}

CustomPropertyCollection在如下C#程序里
namespace PropertyGridEx
{
    public class CustomPropertyCollection : List<CustomProperty>, ICustomTypeDescriptor
    {
        #region ICustomTypeDescriptor 成员
        public AttributeCollection GetAttributes()
        {
            return TypeDescriptor.GetAttributes(this, true);
        }
        public string GetClassName()
        {
            return TypeDescriptor.GetClassName(this, true);
        }
        public string GetComponentName()
        {
            return TypeDescriptor.GetComponentName(this, true);
        }
        public TypeConverter GetConverter()
        {
            return TypeDescriptor.GetConverter(this, true);
        }
        public EventDescriptor GetDefaultEvent()
        {
            return TypeDescriptor.GetDefaultEvent(this, true);
        }
        public PropertyDescriptor GetDefaultProperty()
        {
            return TypeDescriptor.GetDefaultProperty(this, true);
        }
        public object GetEditor(Type editorBaseType)
        {
            return TypeDescriptor.GetEditor(this, editorBaseType, true);
        }
        public EventDescriptorCollection GetEvents(Attribute[] attributes)
        {
            return TypeDescriptor.GetEvents(this, attributes, true);
        }
        public EventDescriptorCollection GetEvents()
        {
            return TypeDescriptor.GetEvents(this, true);
        }
        public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
        {
            PropertyDescriptorCollection properties = new PropertyDescriptorCollection(null);
            foreach (CustomProperty cp in this)
            {
                List<Attribute> attrs = new List<Attribute>();
                //[Browsable(false)]
                if (!cp.IsBrowsable)
                {
                    attrs.Add(new BrowsableAttribute(cp.IsBrowsable));
                }
                //[ReadOnly(true)]
                if (cp.IsReadOnly)
                {
                    attrs.Add(new ReadOnlyAttribute(cp.IsReadOnly));
                }
                //[Editor(typeof(editor),typeof(UITypeEditor))]
                if (cp.EditorType != null)
                {
                    attrs.Add(new EditorAttribute(cp.EditorType, typeof(System.Drawing.Design.UITypeEditor)));
                }
                properties.Add(new CustomPropertyDescriptor(cp, attrs.ToArray()));
            }
            return properties;
        }
        public PropertyDescriptorCollection GetProperties()
        {
            return TypeDescriptor.GetProperties(this, true);
        }
        public object GetPropertyOwner(PropertyDescriptor pd)
        {
            return this;
        }
        #endregion
    }
}

我的全局代码报错
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim graphControl1 As new Netron.GraphLib.UI.GraphControl()
graphControl1 = Forms("窗口1").Controls("SplitContainer1").panel1.baseControl.Controls("graphControl1")
Dim propertyGrid1 As new System.Windows.Forms.PropertyGrid()
propertyGrid1 = Forms("窗口1").Controls("SplitContainer2").panel2.baseControl.Controls("propertyGrid1")
Dim propertycollection As new Netron.GraphLib.PropertyGridEx.CustomPropertyCollection()
Dim cproperty As new Netron.GraphLib.PropertyGridEx.CustomProperty()
Dim ent As object
Dim ent1 As object
For Each ent1 In graphControl1.shapes
    If ent1.Isselected Then
        ent = ent1
       
        propertycollection.Add(cproperty.CustomProperty("标题", "Text", "数据", "要显示的内容.", ent));
        propertycollection.Add(cproperty.CustomProperty("背景色", "BackColor", "外观", "背景色.", ent));
        propertycollection.Add(cproperty.CustomProperty("颜色", "ForeColor", "外观", "前景色.", ent));
        propertycollection.Add(cproperty.CustomProperty("字体", "Font", "外观", "字体.", ent));
        propertyGrid1.SelectedObject = propertycollection
        ent = Nothing
        ent1 = Nothing
    End If
Next
For Each ent1 In graphControl1.Connections
    If ent1.Isselected Then
        ent = ent1
         propertyGrid1.SelectedObject = ent
        
        ent = Nothing
        ent1 = Nothing
    End If
Next
ent = Nothing
ent1 = Nothing
End Sub

错误提示为:


图片点击可在新窗口打开查看此主题相关图片如下:qq截图20171111120131全局代码报错.png
图片点击可在新窗口打开查看

 

示例C#窗体效果为:


 


图片点击可在新窗口打开查看此主题相关图片如下:3247160_2.jpg
图片点击可在新窗口打开查看

 

 

请求帮助谢谢!


 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  2楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110597 积分:562881 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2017/11/11 14:27:00 [只看该作者]

使用工具转换:http://converter.telerik.com/

 回到顶部
帅哥哟,离线,有人找我吗?
有点蓝
  3楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:超级版主 帖子:110597 积分:562881 威望:0 精华:9 注册:2015/6/24 9:21:00
  发帖心情 Post By:2017/11/11 14:28:00 [只看该作者]


 回到顶部
帅哥哟,离线,有人找我吗?
jiangxun
  4楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:876 积分:5929 威望:0 精华:0 注册:2014/8/3 12:15:00
  发帖心情 Post By:2017/11/12 9:27:00 [只看该作者]

在最开始加入这句“Imports System.ComponentModel

后提示:“编译错误:““Imports” 语句前必须是声明”是什么意思?


 回到顶部
帅哥哟,离线,有人找我吗?
有点甜
  5楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:版主 帖子:85326 积分:427815 威望:0 精华:5 注册:2012/10/18 22:13:00
  发帖心情 Post By:2017/11/12 20:46:00 [只看该作者]

不能写简写,每一个类型,你都要拷贝 System.ComponentModel.xxx 写进去

 

http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=109442&skin=0

 


 回到顶部
帅哥哟,离线,有人找我吗?
jiangxun
  6楼 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信
等级:四尾狐 帖子:876 积分:5929 威望:0 精华:0 注册:2014/8/3 12:15:00
  发帖心情 Post By:2017/11/19 22:18:00 [只看该作者]

图片点击可在新窗口打开查看谢谢已解决,foxtable真强大,图片点击可在新窗口打开查看图片点击可在新窗口打开查看

 回到顶部