Click here to Skip to main content
15,891,136 members
Home / Discussions / C#
   

C#

 
QuestionRe: RectangleToScreen on a WinForm Bounding Box gives strange co-ordinates ? Pin
Richard MacCutchan20-Jun-14 1:33
mveRichard MacCutchan20-Jun-14 1:33 
QuestionBinding two ComboBoxes Pin
sbn101019-Jun-14 9:18
sbn101019-Jun-14 9:18 
AnswerRe: Binding two ComboBoxes Pin
Eddy Vluggen20-Jun-14 8:06
professionalEddy Vluggen20-Jun-14 8:06 
Questionextract data from excel Pin
solo919-Jun-14 8:37
solo919-Jun-14 8:37 
AnswerRe: extract data from excel Pin
Richard Deeming19-Jun-14 8:54
mveRichard Deeming19-Jun-14 8:54 
AnswerRe: extract data from excel Pin
CatchExAs19-Jun-14 12:31
professionalCatchExAs19-Jun-14 12:31 
AnswerRe: extract data from excel Pin
Richard MacCutchan19-Jun-14 22:53
mveRichard MacCutchan19-Jun-14 22:53 
QuestionChange Properties Category during runtime for PropertyGrid Pin
Mc_Topaz19-Jun-14 4:12
Mc_Topaz19-Jun-14 4:12 
I have been trying to change the Category field of a property displayed in a PropertyGrid in runtime.

I know how to change DisplayName and Browseable in runtime and I thought it would be the same way. Just modifying the code. But nope...

The class I want to display in the PropertyGrid is this:
C#
public class Person
{
    int age = 10;

    [DisplayName("Age")]
    [Category("Fact")]
    public int Age
    {
        get { return age; }
    }
}


I have a Windows form application containing a PropertyGrid (propertyGrid1) and a button (button1) to switch Person.Age's category between "Fact" and "Info".
C#
public Form1()
{
    InitializeComponent();

    propertyGrid1.SelectedObject = new Person();
}

private void button1_Click(object sender, EventArgs e)
{
    SetCategory();
    SetDisplayName();
}


To switch the DisplayName I have this method:
C#
private void SetDisplayName()
{
    Person person = propertyGrid1.SelectedObject as Person;

    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(person.GetType())["Age"];
    DisplayNameAttribute nameAttribute = (DisplayNameAttribute)descriptor.Attributes[typeof(DisplayNameAttribute)];
    FieldInfo displayName = nameAttribute.GetType().GetField("_displayName", BindingFlags.NonPublic | BindingFlags.Instance);

    string name = displayName.GetValue(nameAttribute).ToString() == "Age" ? "The age" : "Age";
    displayName.SetValue(nameAttribute, name);

    propertyGrid1.SelectedObject = person; // Mandetory or the DisplayName won't change in PropertyGrid...
}

This works well.

I "copied" the SetDisplayName method and made a SetCategory method like this:
C#
private void SetCategory()
{
    Person person = propertyGrid1.SelectedObject as Person;

    PropertyDescriptor descriptor = TypeDescriptor.GetProperties(person.GetType())["Age"];
    CategoryAttribute categoryAttribute = (CategoryAttribute)descriptor.Attributes[typeof(CategoryAttribute)];
    FieldInfo category = categoryAttribute.GetType().GetField("categoryValue", BindingFlags.NonPublic | BindingFlags.Instance);

    string text = category.GetValue(categoryAttribute).ToString() == "Fact" ? "Info" : "Fact";

    category.SetValue(categoryAttribute, text);
    propertyGrid1.SelectedObject = person;
}

This works all well. When the category.SetValue() has been executed the intelisence of categoryAttribute show that the Category property is "Info" and not "Fact" as previous. Just as I want it.
When the method is finished the category of Person.Age has not been changed and the propertyGrid1 still show "Fact" as category.

I have tried to make any sense of this article which show that it's possible: Dynamic Properties for PropertyGrid[^]
But that example is to big to scope and I get confused.

Can anyone help me?

/Steffe
AnswerRe: Change Properties Category during runtime for PropertyGrid Pin
Bernhard Hiller19-Jun-14 20:49
Bernhard Hiller19-Jun-14 20:49 
RantRe: Change Properties Category during runtime for PropertyGrid Pin
Mc_Topaz19-Jun-14 21:17
Mc_Topaz19-Jun-14 21:17 
RantRe: Change Properties Category during runtime for PropertyGrid Pin
Eddy Vluggen20-Jun-14 8:22
professionalEddy Vluggen20-Jun-14 8:22 
QuestionNon-XML ini-file supporting arrays, in plain text format? Pin
arnold_w19-Jun-14 3:30
arnold_w19-Jun-14 3:30 
AnswerRe: Non-XML ini-file supporting arrays, in plain text format? Pin
V.19-Jun-14 4:07
professionalV.19-Jun-14 4:07 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
arnold_w19-Jun-14 5:07
arnold_w19-Jun-14 5:07 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
Eddy Vluggen19-Jun-14 5:13
professionalEddy Vluggen19-Jun-14 5:13 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
Dave Kreskowiak19-Jun-14 5:52
mveDave Kreskowiak19-Jun-14 5:52 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
arnold_w19-Jun-14 21:41
arnold_w19-Jun-14 21:41 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
V.19-Jun-14 21:44
professionalV.19-Jun-14 21:44 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
arnold_w19-Jun-14 21:47
arnold_w19-Jun-14 21:47 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
V.19-Jun-14 21:48
professionalV.19-Jun-14 21:48 
GeneralRe: Non-XML ini-file supporting arrays, in plain text format? Pin
Dave Kreskowiak20-Jun-14 1:48
mveDave Kreskowiak20-Jun-14 1:48 
Question"One or more exceptions" occurred at Task.Start() Pin
Sharath C V19-Jun-14 3:23
professionalSharath C V19-Jun-14 3:23 
AnswerRe: "One or more exceptions" occurred at Task.Start() Pin
OriginalGriff19-Jun-14 3:55
mveOriginalGriff19-Jun-14 3:55 
SuggestionRe: "One or more exceptions" occurred at Task.Start() Pin
Richard Deeming19-Jun-14 4:07
mveRichard Deeming19-Jun-14 4:07 
GeneralRe: "One or more exceptions" occurred at Task.Start() Pin
OriginalGriff19-Jun-14 4:15
mveOriginalGriff19-Jun-14 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.