This sample demonstrates the technique for setting two items of a property grid from the drop-down list. These items have the type of string, but it must be simple to remake them for any other type.
Firstly, inherit the class from UITypeEditor:
public class SelEditor : System.Drawing.Design.UITypeEditor
{
//this is a container for strings, which can be picked-out
ListBox Box1 = new ListBox();
IWindowsFormsEditorService edSvc;
//this is a string array for drop-down list
public static string[] strList;
public SelEditor()
{
Box1.BorderStyle=BorderStyle.None;
//add event handler for drop-down box when item will be selected
Box1.Click+=new EventHandler(Box1_Click);
}
public override System.Drawing.Design.UITypeEditorEditStyle GetEditStyle
(System.ComponentModel.ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
// Displays the UI for value selection.
public override object EditValue
(System.ComponentModel.ITypeDescriptorContext context, System.IServiceProvider provider, object
value)
{
Box1.Items.Clear();
Box1.Items.AddRange(strList);
Box1.Height=Box1.PreferredHeight;
// Uses the IWindowsFormsEditorService to display a
// drop-down UI in the Properties window.
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof
(IWindowsFormsEditorService));
if( edSvc != null )
{
edSvc.DropDownControl( Box1 );
return Box1.SelectedItem;
}
return value;
}
private void Box1_Click(object sender, EventArgs e)
{
edSvc.CloseDropDown();
}
}
Secondly, describe a property in the class, displayed in the property grid:
public class Class1
{
//These are string arrays for different drop-down list.
string[] Str1= {"AAA","BBB","CCC","DDDD"};
string[] Str2= {"WW","EEE"};
string s1,s2;
public Class1()
{
//
// TODO: Add constructor logic here
//
}
[EditorAttribute(typeof(SelEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string STR_1
{
get{SelEditor.strList=Str1; return s1;}
set{s1=value;}
}
[EditorAttribute(typeof(SelEditor), typeof(System.Drawing.Design.UITypeEditor))]
public string STR_2
{
get{SelEditor.strList=Str2; return s2;}
set{s2=value;}
}
}
Showing posts with label PropertyGrid. Show all posts
Showing posts with label PropertyGrid. Show all posts
Sunday, 25 November 2012
Thursday, 6 September 2012
Dynamic Properties for PropertyGrid
http://www.codeproject.com/Articles/189521/Dynamic-Properties-for-PropertyGrid
http://www.codeproject.com/Articles/11383/Hiding-Inherited-Properties-from-the-PropertyGrid
http://www.codeproject.com/Articles/48015/Exploring-the-Behaviour-of-Property-Grid
http://www.codeproject.com/Articles/11383/Hiding-Inherited-Properties-from-the-PropertyGrid
http://www.codeproject.com/Articles/48015/Exploring-the-Behaviour-of-Property-Grid
Saturday, 9 June 2012
Saturday, 14 April 2012
Sample code to customize the .Net PropertyGrid
Sample code to customize the .Net PropertyGrid:
Add Custom Properties to a PropertyGrid:
http://www.codeproject.com/KB/vb/PropertyGridEx.aspx
Filtering properties in a PropertyGrid:
http://www.codeproject.com/KB/cs/FilteredPropertyGrid.aspx
PropertyGrid Utilities
http://www.codeproject.com/Articles/7852/PropertyGrid-utilities
Add Custom Properties to a PropertyGrid:
http://www.codeproject.com/KB/vb/PropertyGridEx.aspx
Filtering properties in a PropertyGrid:
http://www.codeproject.com/KB/cs/FilteredPropertyGrid.aspx
PropertyGrid Utilities
http://www.codeproject.com/Articles/7852/PropertyGrid-utilities
Friday, 6 April 2012
Validating Properties In A .Net Propertygrid Control
The easiest way to validate a certain property in a .Net propertygrid control is using the PropertyValueChanged event.The code bellow shows how to limit the valid range of Age property to be 1-150
using System.Windows.Forms;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//init person object
Person person = new Person();
person.FirstName = "George";
person.Age = 33;
propertyGrid.SelectedObject = person;
propertyGrid.PropertyValueChanged+= new PropertyValueChangedEventHandler( propertyGrid_PropertyValueChanged );
}
private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
if (e.ChangedItem.Label == "Age" && !IsAgeValid((int)e.ChangedItem.Value) )
{
// the entered age value is wrong - show error message
e.ChangedItem.PropertyDescriptor.SetValue( propertyGrid.SelectedObject, e.OldValue);
MessageBox.Show("Wrong age", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
///<summary>
/// Determines whether age is valid
/// </summary>
/// <param name="age">The age.</param>
/// <returns>
/// <c>true</c> if is age valid ; otherwise, <c>false</c>.
/// </returns>
private static bool IsAgeValid( int age )
{
return ((age > 0) && (age < 150));
}
}
}
Thursday, 5 April 2012
Getting Started on the .NET Framework PropertyGrid Control
Steps on creating a .Net property grid control
http://msdn.microsoft.com/en-us/library/aa302326.aspx
http://msdn.microsoft.com/en-us/library/aa302326.aspx
Subscribe to:
Posts (Atom)