propertygrid控件使用

None.gif <? xml version = " 1.0 "  encoding = " gb2312 "   ?>
None.gif
< Components >
None.gif  
<!--
None.gif  
< Component Name = " SelectTextBox "  Namespace = " STBox "  Asm = " E:\Visual Studio\Projects\SBZMis\SelectTextBox\SelectTextBox\bin\Debug\SelectTextBox.dll " >
None.gif    
< Propertys >
None.gif      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
None.gif      
< Property Name = " Border "  Caption = " 边框样式 "  Group = "" />
None.gif      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
None.gif      
< Property Name = " ForeColor "  Caption = " 字体颜色 "  Group = "" />
None.gif      
< Property Name = " Value "  Caption = " "  Group = "" />
None.gif      
< Property Name = " MultLine "  Caption = " 多行 "  Group = "" />
None.gif      
< Property Name = " TabIndex "  Caption = " Tab顺序 "  Group = "" />
None.gif      
< Property Name = " CaptionColor "  Caption = " 标题颜色 "  Group = "" />
None.gif      
< Property Name = " EditBackColor "  Caption = " 编辑框背景色 "  Group = "" />
None.gif      
< Property Name = " LineColor "  Caption = " 下划线色 "  Group = "" />
None.gif      
< Property Name = " SameColor "  Caption = " 前后同色 "  Group = "" />
None.gif      
< Property Name = " AutoShowButton "  Caption = " 自动显示按钮 "  Group = "" />
None.gif      
< Property Name = " AutoSizeTextField "  Caption = " 自动调整文本框大小 "  Group = "" />
None.gif      
< Property Name = " ButtonCaption "  Caption = " 按钮标题 "  Group = "" />
None.gif      
< Property Name = " ButtonColor "  Caption = " 按钮颜色 "  Group = "" />
None.gif      
< Property Name = " ButtonImage "  Caption = " 按钮图片 "  Group = "" />
None.gif      
< Property Name = " CaptionFont "  Caption = " 标题字体 "  Group = "" />
None.gif      
< Property Name = " ExculdeFilter "  Caption = " 不包含过虑器 "  Group = "" />
None.gif      
< Property Name = " ExcludeFilterString "  Caption = " 不包含字串 "  Group = "" />
None.gif      
< Property Name = " IncludeFilter "  Caption = " 包含过虑器 "  Group = "" />
None.gif      
< Property Name = " IncludeFilterString "  Caption = " 包含字串 "  Group = "" />
None.gif      
< Property Name = " NoButton "  Caption = " 不显示按钮 "  Group = "" />
None.gif      
< Property Name = " TextFont "  Caption = " 编辑框字体 "  Group = "" />
None.gif      
< Property Name = " Type "  Caption = " 类型 "  Group = "" />
None.gif      
< Property Name = " ShowCaption "  Caption = " 显示标题 "  Group = "" />
None.gif      
< Property Name = " ShowScrollBar "  Caption = " 显示滚动条 "  Group = "" />
None.gif      
< Property Name = " TextAlign "  Caption = " 编辑框对齐模式 "  Group = "" />
None.gif    
</ Propertys >
None.gif    
< DataBinding >
None.gif
None.gif    
</ DataBinding >
None.gif  
</ Component >
None.gif  
!-->
None.gif  
< Component Name = " TextBox "  Namespace = " System.Windows.Forms "  Asm = " System.dll " >
None.gif    
< Propertys >
None.gif      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
None.gif      
< Property Name = " BorderStyle "  Caption = " 边框样式 "  Group = "" />
None.gif      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
None.gif      
< Property Name = " ForeColor "  Caption = " 字色 "  Group = "" />
None.gif      
< Property Name = " Text "  Caption = " 内容 "  Group = "" />
None.gif      
< Property Name = " ScrollBars "  Caption = " 滚动条 "  Group = "" />
None.gif      
< Property Name = " TextAlign "  Caption = " 文本对齐 "  Group = "" />
None.gif      
< Property Name = " Multline "  Caption = " 多行 "  Group = "" />
None.gif      
< Property Name = " PasswordChar "  Caption = " 密码文本 "  Group = "" />
None.gif      
< Property Name = " Size "  Caption = " 大小 "  Group = "" />
None.gif      
< Property Name = " Location "  Caption = " 位置 "  Group = "" />
None.gif    
</ Propertys >
None.gif    
< DataBinding >
None.gif
None.gif    
</ DataBinding >
None.gif  
</ Component >
None.gif  
< Component Name = " Label "  Namespace = " System.Windows.Forms "  Asm = " System.dll " >
None.gif    
< Propertys >
None.gif      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
None.gif      
< Property Name = " BorderStyle "  Caption = " 边框样式 "  Group = "" />
None.gif      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
None.gif      
< Property Name = " ForeColor "  Caption = " 字色 "  Group = "" />
None.gif      
< Property Name = " Image "  Caption = " 图片 "  Group = "" />
None.gif      
< Property Name = " ImageAlign "  Caption = " 图片对齐 "  Group = "" />
None.gif      
< Property Name = " Text "  Caption = " 文字 "  Group = "" />
None.gif      
< Property Name = " TextAlign "  Caption = " 文本对齐 "  Group = "" />
None.gif      
< Property Name = " Size "  Caption = " 大小 "  Group = "" />
None.gif      
< Property Name = " Location "  Caption = " 位置 "  Group = "" />
None.gif    
</ Propertys >
None.gif    
< DataBinding >
None.gif
None.gif    
</ DataBinding >
None.gif  
</ Component >
None.gif  
< Component Name = " ListBox "  Namespace = " System.Windows.Forms "  Asm = " System.dll " >
None.gif    
< Propertys >
None.gif      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
None.gif      
< Property Name = " BorderStyle "  Caption = " 边框样式 "  Group = "" />
None.gif      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
None.gif      
< Property Name = " ForeColor "  Caption = " 字色 "  Group = "" />
None.gif      
< Property Name = " FormatString "  Caption = " 格式串 "  Group = "" />
None.gif      
< Property Name = " FormattingEnabled "  Caption = " 启用格式串 "  Group = "" />
None.gif      
< Property Name = " Items "  Caption = " 列表项目 "  Group = "" />
None.gif      
< Property Name = " MultColumn "  Caption = " 多列 "  Group = "" />
None.gif      
< Property Name = " IntegralHeight "  Caption = " 整数高度 "  Group = "" />
None.gif      
< Property Name = " Size "  Caption = " 大小 "  Group = "" />
None.gif      
< Property Name = " Location "  Caption = " 位置 "  Group = "" />
None.gif    
</ Propertys >
None.gif    
< DataBinding >
None.gif
None.gif    
</ DataBinding >
None.gif
None.gif  
</ Component >
None.gif  
< Component Name = " Button "  Namespace = " System.Windows.Forms "  Asm = " System.dll " >
None.gif    
< Propertys >
None.gif      
< Property Name = " BackColor "  Caption = " 背影色 "  Group = "" />
None.gif      
< Property Name = " BorderStyle "  Caption = " 边框样式 "  Group = "" />
None.gif      
< Property Name = " Font "  Caption = " 字体 "  Group = "" />
None.gif      
< Property Name = " ForeColor "  Caption = " 字色 "  Group = "" />
None.gif      
< Property Name = " FlatStyle "  Caption = " 样式 "  Group = "" />
None.gif      
< Property Name = " Text "  Caption = " 文本 "  Group = "" />
None.gif      
< Property Name = " Size "  Caption = " 大小 "  Group = "" />
None.gif      
< Property Name = " Location "  Caption = " 位置 "  Group = "" />
None.gif        
< Property Name = " ImageList "  Caption = " 图象 "  Group = "" />
None.gif        
< Property Name = " ImageAlign "  Caption = " 图象对其的方式 "  Group = "" />
None.gif        
< Property Name = " Image "  Caption = " 控件显示的图象 "  Group = "" />
None.gif    
</ Propertys >
None.gif    
< DataBinding >
None.gif
None.gif    
</ DataBinding >
None.gif
None.gif  
</ Component >
None.gif
</ Components >
None.gif
None.gif using  System;
None.gif
using  System.Collections;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.ComponentModel;
None.gif
using  System.Xml;
None.gif
using  System.Windows.Forms;
None.gif
namespace  WindowsApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif  
class CustomProperty : ICustomTypeDescriptor
ExpandedSubBlockStart.gifContractedSubBlock.gif  
dot.gif{
InBlock.gif    
//当前选择对象
InBlock.gif
    private object mCurrentSelectObject;
InBlock.gif    
private Dictionary<stringstring> mObjectAttribs = new Dictionary<stringstring>();
InBlock.gif    
public CustomProperty(object pSelectObject, XmlNodeList pObjectPropertys)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      mCurrentSelectObject 
= pSelectObject;
InBlock.gif      XmlNode tmpXNode;
InBlock.gif      IEnumerator tmpIe 
= pObjectPropertys.GetEnumerator();
InBlock.gif      
while (tmpIe.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        tmpXNode 
= tmpIe.Current as XmlNode;
InBlock.gif        mObjectAttribs.Add(tmpXNode.Attributes[
"Name"].Value, tmpXNode.Attributes["Caption"].Value);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif      
ContractedSubBlock.gifExpandedSubBlockStart.gif    
ICustomTypeDescriptor Members#region ICustomTypeDescriptor Members
InBlock.gif    
public AttributeCollection GetAttributes()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetAttributes(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string GetClassName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetClassName(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public string GetComponentName()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetComponentName(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public TypeConverter GetConverter()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetConverter(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public EventDescriptor GetDefaultEvent()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetDefaultEvent(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public PropertyDescriptor GetDefaultProperty()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetDefaultProperty(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public object GetEditor(Type editorBaseType)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEditor(mCurrentSelectObject, editorBaseType);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public EventDescriptorCollection GetEvents(Attribute[] attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEvents(mCurrentSelectObject, attributes);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public EventDescriptorCollection GetEvents()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetEvents(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      List
<CustomPropertyDescriptor> tmpPDCLst = new List<CustomPropertyDescriptor>();
InBlock.gif      PropertyDescriptorCollection tmpPDC 
= TypeDescriptor.GetProperties(mCurrentSelectObject, attributes);
InBlock.gif      IEnumerator tmpIe 
= tmpPDC.GetEnumerator();
InBlock.gif      CustomPropertyDescriptor tmpCPD;
InBlock.gif      PropertyDescriptor tmpPD;
InBlock.gif      
while (tmpIe.MoveNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        tmpPD 
= tmpIe.Current as PropertyDescriptor;
InBlock.gif        
if (mObjectAttribs.ContainsKey(tmpPD.Name))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif          tmpCPD 
= new CustomPropertyDescriptor(mCurrentSelectObject, tmpPD);
InBlock.gif          tmpCPD.SetDisplayName(mObjectAttribs[tmpPD.Name]);
InBlock.gif          tmpCPD.SetCategory(tmpPD.Category 
+ "中文");  
InBlock.gif          tmpPDCLst.Add(tmpCPD);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

InBlock.gif      
return new PropertyDescriptorCollection(tmpPDCLst.ToArray());
ExpandedSubBlockEnd.gif    }

InBlock.gif    
public PropertyDescriptorCollection GetProperties()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return TypeDescriptor.GetProperties(mCurrentSelectObject);
ExpandedSubBlockEnd.gif    }

InBlock.gif   
public object GetPropertyOwner(PropertyDescriptor pd)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
return mCurrentSelectObject;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif    
#endregion

InBlock.gif    
class CustomPropertyDescriptor : PropertyDescriptor
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif      
private PropertyDescriptor mProp;
InBlock.gif      
private object mComponent;
InBlock.gif
InBlock.gif      
public CustomPropertyDescriptor(object pComponent, PropertyDescriptor pPD)
InBlock.gif        : 
base(pPD)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        mCategory 
= base.Category;
InBlock.gif        mDisplayName 
= base.DisplayName;
InBlock.gif        mProp 
= pPD;
InBlock.gif        mComponent 
= pComponent;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
private string mCategory;
InBlock.gif      
public override string Category
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn mCategory; }
ExpandedSubBlockEnd.gif      }

InBlock.gif      
private string mDisplayName ;
InBlock.gif      
public override string DisplayName
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn mDisplayName; }
ExpandedSubBlockEnd.gif      }

InBlock.gif      
public void SetDisplayName(string pDispalyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        mDisplayName 
= pDispalyName;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
public void SetCategory(string pCategory)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        mCategory 
= pCategory;
ExpandedSubBlockEnd.gif      }

InBlock.gif      
public override bool CanResetValue(object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return mProp.CanResetValue(component);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override Type ComponentType
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn mProp.ComponentType; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override object GetValue(object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return mProp.GetValue(component);
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override bool IsReadOnly
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn mProp.IsReadOnly; }
ExpandedSubBlockEnd.gif      }

InBlock.gif
InBlock.gif      
public override Type PropertyType
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
get dot.gifreturn mProp.PropertyType; }
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gifContractedSubBlock.gif      
public override void ResetValue(object component) dot.gif{ mProp.ResetValue(component); }
ExpandedSubBlockStart.gifContractedSubBlock.gif      
public override void SetValue(object component, object value) dot.gif{ mProp.SetValue(component, value); }
InBlock.gif      
public override bool ShouldSerializeValue(object component)
ExpandedSubBlockStart.gifContractedSubBlock.gif      
dot.gif{
InBlock.gif        
return mProp.ShouldSerializeValue(component);
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}

None.gif

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Collections;
namespace WindowsApplication1
{
  public partial class Form1 : Form
  {
    XmlDocument mXDoc = new XmlDocument();
    public Form1()
    {
      InitializeComponent();
      mXDoc.Load(Application.StartupPath + "\\Components.xml");
    }

    private void button1_Click(object sender, EventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"Button\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void listBox1_MouseDown(object sender, MouseEventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"ListBox\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void textBox1_MouseDown(object sender, MouseEventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"TextBox\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;

    }

    private void label1_MouseDown(object sender, MouseEventArgs e)
    {
      XmlNode tmpXNode = mXDoc.SelectSingleNode("Components/Component[@Name=\"Label\"]");
      XmlNodeList tmpXPropLst = tmpXNode.SelectNodes("Propertys/Property");
      CustomProperty cp = new CustomProperty(sender, tmpXPropLst);
      propertyGrid1.SelectedObject = cp;

    }
  }
}

转载于:https://www.cnblogs.com/qyfan82/archive/2007/08/27/871193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值