Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: Any suggestions for "compiling" strings? Pin
SledgeHammer0111-Sep-14 16:02
SledgeHammer0111-Sep-14 16:02 
GeneralRe: Any suggestions for "compiling" strings? Pin
SledgeHammer0111-Sep-14 19:37
SledgeHammer0111-Sep-14 19:37 
AnswerRe: Any suggestions for "compiling" strings? Pin
Pete O'Hanlon11-Sep-14 22:38
mvePete O'Hanlon11-Sep-14 22:38 
AnswerRe: Any suggestions for "compiling" strings? Pin
jschell13-Sep-14 6:57
jschell13-Sep-14 6:57 
QuestionDatagridcombocell Problem Pin
hussainroyal11-Sep-14 10:39
hussainroyal11-Sep-14 10:39 
SuggestionRe: Datagridcombocell Problem Pin
Richard MacCutchan11-Sep-14 21:32
mveRichard MacCutchan11-Sep-14 21:32 
GeneralRe: Datagridcombocell Problem Pin
hussainroyal11-Sep-14 22:03
hussainroyal11-Sep-14 22:03 
QuestionGet a property from an internal .NET framework class Pin
Mc_Topaz11-Sep-14 4:20
Mc_Topaz11-Sep-14 4:20 
I have a PropertyGrid in an application. I have a problem with a functionallity reverting to a old value if I detect the new value as invalid.

Here is some short information abut the PropertyGrid:
* There are properties which are strings.
* I cannot allow an empty string for any properties.
* I want a revert functionallity to revert to the old value.
* The PropertyGrid is localized so the DisplayName of each property is NOT the same as the name of the property.

With the event
PropertyGrid.PropertyValuechanged
I can get what property was changed.
The event's callback pass a
PropertyCalueChangedEventArgs
object which I use revert with these properties:
PropertyValueChangedEventArgs.OldValue
PropertyValueChangedEventArgs.ChangedItem.Label

This gets the old value and the DisplayName of the property which was changed.

Here is the class I display in the PropertyGrid:
C#
public class Person
{
    public Person()
    {
    }

    public string FirtName
    {
        get;
        set;
    }

    public string LastName
    {
        get;
        set;
    }

    public int Age
    {
        get;
        set;
    }
}

I use this code to revert back to the old value:
C#
/// <summary>
/// Revert back to previous value of property
/// </summary>
/// <param name="obj">The object which is displayed in the PropertyGrid</param>
/// <param name="name">The property which was changed</param>
/// <param name="oldValue">The old value of the property</param>
private void Revert(object obj, string name, string oldValue)
{
    Type type = obj.GetType();
    PropertyInfo property = type.GetProperty(name);
    property.SetValue(obj, oldValue, null);
}

The PropertyGrid.PropertyValuechanged event has this callback:
C#
void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
    if (e.ChangedItem.Value is string && e.ChangedItem.Value.ToString() == "")
    {
        Revert(propertyGrid1.SelectedObject, e.ChangedItem.Label, e.OldValue.ToString());
    }
}

The IF-satement will detect any new values which is an empty string.

Here is the problem
As the DisplayName of each property in the PropertyGrid is localized the PropertyValueChangedEventArgs.ChangedItem.Label will contain the localized name and not the actual name of the property.

For instance the Person.FirstName property will be localized to "First name". Therefore the Revert() method will try to get a property called "First name" as name instead of "FirstName". This of course will not work.

I have search for ways to get the actual property which was changed. It seems almost impossible...


But with the debug in Visual Studio I can see that the
PropertyValueChangedEventArgs.ChangedItem

is a base class of the internal .Net Framework class
System.Windows.Forms.PropertyGridInternal.PropertyDescriptorGridEntry
Read more about it here[^]


This class contain a property called "PropertyName" which is the exact name as the property's actual name!!!


With reflection I have tried to get this class by trying:
C#
void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
{
    if (e.ChangedItem.Value.ToString() == "")
    {
        GridItem item = e.ChangedItem;
        object[] objs = item.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
        Console.WriteLine(objs.Length);

        object o = item.GetType().GetMember("parentPE");
    }

}

In the object[] array it seems to be a field which is name "parentPE". This is of type:
System.Windows.Forms.PropertyGridInternal.GridEntry

I have no luck getting the object Frown | :(


Is there a solution for this?

Note:
I'm aware I should not use reflection to access internal classes of the .NET framework. But I cannot see any other solution in the moment. I'm all for any other solutions.

Best regards,
/Steffe
AnswerRe: Get a property from an internal .NET framework class Pin
Richard Deeming11-Sep-14 4:38
mveRichard Deeming11-Sep-14 4:38 
GeneralRe: Get a property from an internal .NET framework class Pin
Mc_Topaz11-Sep-14 4:46
Mc_Topaz11-Sep-14 4:46 
AnswerRe: Get a property from an internal .NET framework class Pin
Dave Kreskowiak11-Sep-14 4:45
mveDave Kreskowiak11-Sep-14 4:45 
AnswerRe: Get a property from an internal .NET framework class Pin
Eddy Vluggen11-Sep-14 5:20
professionalEddy Vluggen11-Sep-14 5:20 
GeneralRe: Get a property from an internal .NET framework class Pin
OriginalGriff11-Sep-14 5:29
mveOriginalGriff11-Sep-14 5:29 
GeneralRe: Get a property from an internal .NET framework class Pin
PIEBALDconsult11-Sep-14 6:19
mvePIEBALDconsult11-Sep-14 6:19 
GeneralRe: Get a property from an internal .NET framework class Pin
Eddy Vluggen11-Sep-14 6:51
professionalEddy Vluggen11-Sep-14 6:51 
GeneralRe: Get a property from an internal .NET framework class Pin
OriginalGriff11-Sep-14 8:50
mveOriginalGriff11-Sep-14 8:50 
GeneralRe: Get a property from an internal .NET framework class Pin
Eddy Vluggen11-Sep-14 9:39
professionalEddy Vluggen11-Sep-14 9:39 
QuestionTX TextControl 21.0 Document viewer example in c# Pin
pitchaiyan11-Sep-14 3:44
pitchaiyan11-Sep-14 3:44 
QuestionRe: TX TextControl 21.0 Document viewer example in c# Pin
ZurdoDev11-Sep-14 4:27
professionalZurdoDev11-Sep-14 4:27 
AnswerRe: TX TextControl 21.0 Document viewer example in c# Pin
Dave Kreskowiak11-Sep-14 4:38
mveDave Kreskowiak11-Sep-14 4:38 
GeneralRe: TX TextControl 21.0 Document viewer example in c# Pin
PIEBALDconsult12-Sep-14 5:34
mvePIEBALDconsult12-Sep-14 5:34 
GeneralRe: TX TextControl 21.0 Document viewer example in c# Pin
Dave Kreskowiak12-Sep-14 5:39
mveDave Kreskowiak12-Sep-14 5:39 
QuestionHow to loop in timers Pin
Member 1107411510-Sep-14 21:02
Member 1107411510-Sep-14 21:02 
AnswerRe: How to loop in timers Pin
OriginalGriff10-Sep-14 21:52
mveOriginalGriff10-Sep-14 21:52 
GeneralRe: How to loop in timers Pin
Member 1107411510-Sep-14 22:02
Member 1107411510-Sep-14 22:02 

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.