以文本方式查看主题 - Foxtable(狐表) (http://foxtable.net/bbs/index.asp) -- 专家坐堂 (http://foxtable.net/bbs/list.asp?boardid=2) ---- 请教C#代码如何转还到全局代码执行 (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=109416) |
-- 作者:jiangxun -- 发布时间:2017/11/11 12:09:00 -- 请教C#代码如何转还到全局代码执行 示例窗体程序
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
错误提示为:
示例C#窗体效果为:
请求帮助谢谢! |
-- 作者:有点蓝 -- 发布时间:2017/11/11 14:27:00 -- 使用工具转换:http://converter.telerik.com/ |
-- 作者:有点蓝 -- 发布时间:2017/11/11 14:28:00 -- 参考这里 |
-- 作者:jiangxun -- 发布时间:2017/11/12 9:27:00 -- 在最开始加入这句“Imports System.ComponentModel” 后提示:“编译错误:““Imports” 语句前必须是声明”是什么意思? |
-- 作者:有点甜 -- 发布时间:2017/11/12 20:46:00 -- 不能写简写,每一个类型,你都要拷贝 System.ComponentModel.xxx 写进去
http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=109442&skin=0
|
-- 作者:jiangxun -- 发布时间:2017/11/19 22:18:00 -- 谢谢已解决,foxtable真强大, |