实例介绍
【实例简介】
自定义属性网格
【实例截图】
【核心代码】
using System;
using System.Collections;
using System.Resources;
using System.Windows.Forms;
namespace PropertyGridFormattedEntries
{
#region CustomPropertyGridHeader
//************************************************************
// Type Name: CustomPropertyGrid
/// <summary>
/// Summary description for CustomPropertyGrid.
/// </summary>
///
/// <remarks></remarks>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>February 3, 2005</creationDate>
//************************************************************
#endregion CustomPropertyGridHeader
public class CustomPropertyGrid: System.Windows.Forms.PropertyGrid
{
private System.Windows.Forms.Control.ControlCollection gridControls = null;
private TextBox m_TextBox = null;
private int lastSelection;
private string formatingString = "";
private char formatSeparator = '.';
#region PROPERTIES
#endregion PROPERTIES
#region METHODS
#region CustomPropertyGrid Method
//************************************************************
// Method Name: CustomPropertyGrid
/// <summary>
///
/// </summary>
///
/// <remarks></remarks>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>February 4, 2005</creationDate>
//************************************************************
public CustomPropertyGrid() : base()
{
}
#endregion CustomPropertyGrid Method
#region OnSelectedGridItemChanged Method
//************************************************************
// Method Name: OnSelectedGridItemChanged
/// <summary>
///
/// </summary>
///
/// <remarks></remarks>
///
/// <param name="e"></param>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>February 14, 2005</creationDate>
//************************************************************
protected override void OnSelectedGridItemChanged(SelectedGridItemChangedEventArgs e)
{
Control activeControl;
GridItem selectedItem;
activeControl = base.ActiveControl;
selectedItem = base.SelectedGridItem;
if( activeControl != null && selectedItem != null )
{
this.SetupPropertyStyle( activeControl, selectedItem );
}
if (m_TextBox != null )
{
m_TextBox.TextChanged -= new EventHandler(m_TextBox_TextChanged);
}
if( m_TextBox == null )
{
foreach (Control control in base.ActiveControl.Controls)
{
if (control.GetType().ToString() == "System.Windows.Forms.PropertyGridInternal.PropertyGridView GridViewEdit" )
{
if (control is TextBox)
{
m_TextBox = control as TextBox;
break;
}
}
}
}
foreach( System.Attribute attribute in selectedItem.PropertyDescriptor.Attributes )
{
if( attribute.GetType() == typeof(PropertyViewFormatAttribute) )
{
if( ( ((PropertyViewFormatAttribute)attribute).AttributeFlags & PropertyViewAttributeEnum.PropertyViewAttributes.FormattedField)
== PropertyViewAttributeEnum.PropertyViewAttributes.FormattedField )
{
if( m_TextBox != null )
{
lastSelection = m_TextBox.SelectionStart;
m_TextBox.TextChanged = new EventHandler(m_TextBox_TextChanged);
}
break;
}
}
}
base.OnSelectedGridItemChanged (e);
}
#endregion OnSelectedGridItemChanged Method
#region OnSelectedObjectsChanged Method
//************************************************************
// Method Name: OnSelectedObjectsChanged
/// <summary>
///
/// </summary>
///
/// <remarks></remarks>
///
/// <param name="e"></param>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>March 10, 2005</creationDate>
//************************************************************
protected override void OnSelectedObjectsChanged(EventArgs e)
{
if( this.SelectedObject is IPropertyGrid )
{
this.formatingString = ((IPropertyGrid)this.SelectedObject).FormatString;
this.formatSeparator = ((IPropertyGrid)this.SelectedObject).FormatSeparator;
}
if( gridControls != null )
this.ResetPropertyStyle( gridControls );
base.OnSelectedObjectsChanged (e);
}
#endregion OnSelectedObjectsChanged Method
#region m_TextBox_TextChanged Method
//************************************************************
// Method Name: m_TextBox_TextChanged
/// <summary>
///
/// </summary>
///
/// <remarks>
/// KJS
/// This method is called automatically when the selection in the grid is changed.
/// This is outside the control of the logic in this class.
/// The grid internaly sets the value of the TextBox on the grid selection change
/// which invokes this call-back automatically.
/// This causes problems since the current text can be erased by the formatted string.
/// To prevent this from happening the grid internal property "Modified" is used.
/// See below for details
/// </remarks>
///
/// <param name="sender"></param>
/// <param name="e"></param>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>November 23, 2005</creationDate>
//************************************************************
private void m_TextBox_TextChanged(object sender, EventArgs e)
{
System.Text.StringBuilder newText;
string oldText = this.m_TextBox.Text;
int stringLen = this.m_TextBox.Text.Length;
int currentSelection = this.m_TextBox.SelectionStart;
//
// KJS. Get the caller type.
//
System.Type senderType = sender.GetType();
//
// KJS. It was established that the caller has "Modified" property.
// This property is set only when the data was modified, not when the
// data is originally setup.
// Since this is very usefull information we are looking for this property.
System.Reflection.PropertyInfo property = senderType.GetProperty( "Modified" );
//
// KJS. Now get the property value
//
bool mod = (bool)property.GetValue( sender, null );
//
// KJS. Proceed with formatting only if data was actually changed
// which is indicated by set "Modified" property.
//
if( !mod )
return;
newText = new System.Text.StringBuilder(formatingString.Length);
//
// KJS. The string can only be as loong as the format allows
//
for( int i = 0, j = 0; i < formatingString.Length; i )
{
if( i < stringLen )
{
if( oldText[i] >= '0' && oldText[i] <= '9' )
{
if( formatingString[i] == formatSeparator )
{
newText.Insert( j, formatingString[i] );
j ;
newText.Insert( j, oldText[i] );
if( this.lastSelection < currentSelection )
currentSelection ;
else
currentSelection--;
}
else
{
newText.Insert( j, oldText[i] );
}
}
else
newText.Insert( j, formatingString[i] );
}
else
newText.Insert( j, formatingString[i] );
j ;
}
this.m_TextBox.Text = newText.ToString();
if( currentSelection < 0 )
currentSelection = 0;
this.m_TextBox.Select(currentSelection, 0);
this.lastSelection = currentSelection;
}
#endregion m_TextBox_TextChanged Method
#region SetupPropertyStyle Method
//************************************************************
// Method Name: SetupPropertyStyle
/// <summary>
///
/// </summary>
///
/// <remarks></remarks>
///
/// <param name="thisControl"></param>
/// <param name="viewItem"></param>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>February 14, 2005</creationDate>
//************************************************************
private void SetupPropertyStyle(Control thisControl, GridItem viewItem)
{
bool passwordField;
System.ComponentModel.PropertyDescriptor viewProperty;
System.ComponentModel.AttributeCollection attributes;
System.Windows.Forms.Control.ControlCollection controls = thisControl.Controls;
gridControls = controls;
viewProperty = viewItem.PropertyDescriptor;
attributes = viewProperty.Attributes;
passwordField = false;
foreach( System.Attribute attribute in attributes )
{
if( attribute.GetType() == typeof(PropertyViewFormatAttribute) )
{
if( ( ((PropertyViewFormatAttribute)attribute).AttributeFlags & PropertyViewAttributeEnum.PropertyViewAttributes.PasswordField)
== PropertyViewAttributeEnum.PropertyViewAttributes.PasswordField )
{
passwordField = true;
break;
}
}
}
if( passwordField )
{
foreach( Control control in controls )
{
System.Type baseType = control.GetType().BaseType;
System.Reflection.PropertyInfo passwordCharProperty = baseType.GetProperty( "PasswordChar" );
if( passwordCharProperty != null )
passwordCharProperty.SetValue( control, '*', null );
}
}
else
{
foreach( Control control in controls )
{
System.Type baseType = control.GetType().BaseType;
System.Reflection.PropertyInfo passwordCharProperty = baseType.GetProperty( "PasswordChar" );
if( passwordCharProperty != null )
passwordCharProperty.SetValue( control, null, null );
}
}
}
#endregion SetupPropertyStyle Method
#region ResetPropertyStyle Method
//************************************************************
// Method Name: ResetPropertyStyle
/// <summary>
///
/// </summary>
///
/// <remarks></remarks>
///
/// <param name="thisControls"></param>
///
/// <authorName>Krzysztof J. Stoj</authorName>
/// <creationDate>March 10, 2005</creationDate>
//************************************************************
private void ResetPropertyStyle(System.Windows.Forms.Control.ControlCollection thisControls)
{
foreach( Control control in thisControls )
{
System.Type baseType = control.GetType().BaseType;
System.Reflection.PropertyInfo passwordCharProperty = baseType.GetProperty( "PasswordChar" );
if( passwordCharProperty != null )
passwordCharProperty.SetValue( control, null, null );
}
}
#endregion ResetPropertyStyle Method
#endregion METHODS
}
}
标签: winform PropertyGrid
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论