Windows Develop Bookmark and Share   
 index > Windows Forms General > Property Grid Custom Tab Focus.
 

Property Grid Custom Tab Focus.

Hi all, I'm using a custom designer and a collection of custom controls, some properties of my control use an attribute to be displayed in a custom tab of my property grid.
I would like to be able to set the focus on the custom tab of the property grid by a custom event, any hints?
Campa.
Campa  Tuesday, December 02, 2008 8:30 AM

Here's a sample tomake yourpropertyGrid display what you want to diplay.

It's a little long but good to use.

Wish it will help.

Code Snippet

System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

PropertyGridEx

public partial class Form1 : Form

public Form1()

private void InitializePropertyGrid()

CustomPropertyCollection collection = new CustomPropertyCollection();

new CustomProperty(", "Text", ", "what you need, label1, typeof(System.ComponentModel.Design.MultilineStringEditor)));

new CustomProperty("backColor, "BackColor", ", ", label1));

new CustomProperty(", "ForeColor", "Appearance"", label1));

new CustomProperty(", "Font", "Appearance""font, label1));

The other three class will be shown next.

Transformer Yu  Wednesday, December 03, 2008 6:52 AM

Here's a sample tomake yourpropertyGrid display what you want to diplay.

It's a little long but good to use.

Wish it will help.

Code Snippet

System;

System.Collections.Generic;

System.ComponentModel;

System.Data;

System.Drawing;

System.Linq;

System.Text;

System.Windows.Forms;

PropertyGridEx

public partial class Form1 : Form

public Form1()

private void InitializePropertyGrid()

CustomPropertyCollection collection = new CustomPropertyCollection();

new CustomProperty(", "Text", ", "what you need, label1, typeof(System.ComponentModel.Design.MultilineStringEditor)));

new CustomProperty("backColor, "BackColor", ", ", label1));

new CustomProperty(", "ForeColor", "Appearance"", label1));

new CustomProperty(", "Font", "Appearance""font, label1));

The other three class will be shown next.

Transformer Yu  Wednesday, December 03, 2008 6:52 AM

Code Snippet

System;

System.Collections.Generic;

System.Text;

System.Reflection;

System.ComponentModel;

PropertyGridEx

public class CustomProperty

{

#region private string _name = string.Empty;

private object _defaultValue = null;

private object _value = null;

private object _objectSource = null;

private PropertyInfo[] _propertyInfos = null;

#endregion

#region 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

Code Snippet

#region 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 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

Code Snippet

#region public void ResetValue()

{

Value = DefaultValue;

}

#endregion

}

Transformer Yu  Wednesday, December 03, 2008 6:56 AM

Code Snippet

System;

System.Collections.Generic;

System.Text;

System.ComponentModel;

PropertyGridEx

public class CustomPropertyCollection : List<CustomProperty>, ICustomTypeDescriptor

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)

new BrowsableAttribute(cp.IsBrowsable));

//[ReadOnly(true)]

if (cp.IsReadOnly)

new ReadOnlyAttribute(cp.IsReadOnly));

//[Editor(typeof(editor),typeof(UITypeEditor))]

if (cp.EditorType != null)

new EditorAttribute(cp.EditorType, typeof(System.Drawing.Design.UITypeEditor)));

new CustomPropertyDescriptor(cp, attrs.ToArray()));

return properties;

public PropertyDescriptorCollection GetProperties()

return TypeDescriptor.GetProperties(this, true);

public object GetPropertyOwner(PropertyDescriptor pd)

return this;

Code Snippet

System;

System.Collections.Generic;

System.Text;

System.ComponentModel;

PropertyGridEx

public class CustomPropertyDescriptor : PropertyDescriptor

private CustomProperty _customProperty = null;

public CustomPropertyDescriptor(CustomProperty customProperty, Attribute[] attrs)

base(customProperty.Name, attrs)

public override bool CanResetValue(object component)

return _customProperty.DefaultValue != null;

public override Type ComponentType

get { return _customProperty.GetType(); }

public override object GetValue(object component)

return _customProperty.Value;

public override bool IsReadOnly

get { return _customProperty.IsReadOnly; }

public override Type PropertyType

get { return _customProperty.ValueType; }

public override void ResetValue(object component)

public override void SetValue(object component, object value)

public override bool ShouldSerializeValue(object component)

return true;

//

public override string Description

get

return _customProperty.Description;

public override string Category

get

return _customProperty.Category;

public override string DisplayName

get

return _customProperty.Name;

public override bool IsBrowsable

get

return _customProperty.IsBrowsable;

public object CustomProperty

get

return _customProperty;

Really hope this will help you!

Best regards,

TransformerYu

Transformer Yu  Wednesday, December 03, 2008 6:59 AM
thx for your help, but i still miss how to set the focus on a custom tab.

The property "PropertyGrid.SelectedTab" is a get only, how can set the selected tab?
Campa  Tuesday, December 09, 2008 11:05 AM
I've also been looking for a way to set the SelectedTab of a PropertyGrid, but since nothing seems forthcoming from MS I've dug into Reflection to do it instead.

Below is a static helper method to do it -- a better approach would be to create your own control inherited from the PropertyGrid, and provide this as a method on it.

publicstaticvoidSetSelectedTabInPropertyGrid(PropertyGridpropGrid,intselectedTabIndex)
{
if((selectedTabIndex<0)||(selectedTabIndex>=propGrid.PropertyTabs.Count))
{
thrownewArgumentException("Invalidtabindextoselect:"+selectedTabIndex);
}
FieldInfobuttonsField=propGrid.GetType().GetField("viewTabButtons",BindingFlags.NonPublic|BindingFlags.Instance);
ToolStripButton[]viewTabButtons=(ToolStripButton[])buttonsField.GetValue(propGrid);
ToolStripButtonviewTabButton=viewTabButtons[selectedTabIndex];
propGrid.GetType().InvokeMember("OnViewTabButtonClick",BindingFlags.NonPublic|BindingFlags.Instance|BindingFlags.InvokeMethod,null,propGrid,newobject[]{viewTabButton,EventArgs.Empty});
}

I still think that a settable SelectedTab property is a basic thing to have on the PropertyGrid, and it should be added to the framework.


  • Proposed As Answer byNot_Sure Monday, February 09, 2009 7:24 PM
  •  
orange2000  Wednesday, February 04, 2009 8:50 PM
it works!

Thanks!
Campa  Thursday, September 10, 2009 3:24 PM

You can use google to search for other answers

Custom Search

More Threads

• How to use just one windows form for an application
• Thread communication
• how to redraw titlebar and borderframe
• Will there be a Data Repeater control released in C# for Windows Forms?
• How to speed up the Forms Load event
• How to cancel a drag operation?
• Making a listview thread-safe
• Capture a snapshot from Windows Media Control?
• why some part of my mode not working
• VB.NET Form Events V's C# Form Events