基本信息
源码名称:Winform,自定义属性网格
源码大小:0.04M
文件格式:.zip
开发语言:C#
更新时间:2025-09-18
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
自定义属性网格
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 } }