Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2005

Tip: Looking for answers? Try searching our database.

DefaultAttribute not working in PropertyGrid on runtime generated

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Robert Magnusson - 18 Jan 2005 23:43 GMT
Hi All,

I am testing some stuff out at the moment with the runtime generation of
classes relating to schema-validated XML elements. I was hoping to get to the
point that I could include the use of the DefaultValueAttribute on any
property that requires it.  The PropertyGrid was then to be used to display
the various properties for editing but, this DefaultValueAttribute doesn't
seem to stick enough for the PropertyGrid to correctly bold/unbold values
entered.

EXAMPLE:

private static void _addAttribute(TypeBuilder tb, string PropertyName)
{
 FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
FieldAttributes.Private);

 PropertyBuilder pb = tb.DefineProperty(PropertyName,
PropertyAttributes.HasDefault, typeof(string), new Type[] { typeof(string) });

 Type t = typeof(System.ComponentModel.CategoryAttribute);

 ConstructorInfo ci = t.GetConstructor(new Type[1]{typeof(string)});    
 CustomAttributeBuilder cab = new CustomAttributeBuilder(ci, new
string[1]{PropertyName});

 pb.SetCustomAttribute(cab);

 if (PropertyName == "CustomerName")
 {
   t = typeof(System.ComponentModel.DefaultValueAttribute);

   ci = t.GetConstructor(new Type[1]{typeof(string)});
   cab = new CustomAttributeBuilder(ci, new string[1]{"Robert Magnusson"});

   pb.SetCustomAttribute(cab);
 }

 // First, we'll define the behavior of the "get" property for CustomerName
as a method.
 MethodBuilder mbGet = tb.DefineMethod("Get" + PropertyName,
MethodAttributes.Public, typeof(string), new Type[] { });

 ILGenerator custNameGetIL = mbGet.GetILGenerator();

 custNameGetIL.Emit(OpCodes.Ldarg_0);
 custNameGetIL.Emit(OpCodes.Ldfld, fb);
 custNameGetIL.Emit(OpCodes.Ret);

 // Now, we'll define the behavior of the "set" property for CustomerName.
 MethodBuilder mbSet = tb.DefineMethod("Set" + PropertyName,
MethodAttributes.Public, null, new Type[] { typeof(string) });

 ILGenerator custNameSetIL = mbSet.GetILGenerator();

 custNameSetIL.Emit(OpCodes.Ldarg_0);
 custNameSetIL.Emit(OpCodes.Ldarg_1);
 custNameSetIL.Emit(OpCodes.Stfld, fb);
 custNameSetIL.Emit(OpCodes.Ret);

 // Last, we must map the two methods created above to our PropertyBuilder
to
 // their corresponding behaviors, "get" and "set" respectively.
 pb.SetGetMethod(mbGet);
 pb.SetSetMethod(mbSet);
}
Claes Bergefall - 19 Jan 2005 10:28 GMT
You say that it doesn't correctly bold/unbold values.
Which is it (alwyas bold or always normal)?
Is Reset enabled or disabled?

What does your generated code look like?

Have you intialized the property to its default value?
Just providing the the attribute isn't enough

   /claes

> Hi All,
>
[quoted text clipped - 62 lines]
>   pb.SetSetMethod(mbSet);
> }
Robert Magnusson - 20 Jan 2005 11:11 GMT
Claes,

> What does your generated code look like?
How can I see it?

> Which is it (alwyas bold or always normal)?
It always stays bold.  I have added the SetConstant call as below but I
don't see any difference to what's happening.

FieldBuilder fb = tb.DefineField("_" + PropertyName, typeof(string),
(PropertyName == "CustomerName" ? FieldAttributes.Private |
FieldAttributes.HasDefault : FieldAttributes.Private));
           
fb.SetConstant(@"Robert Magnusson");

Thanks for your help...

> You say that it doesn't correctly bold/unbold values.
> Which is it (alwyas bold or always normal)?
[quoted text clipped - 80 lines]
> >   pb.SetSetMethod(mbSet);
> > }
Claes Bergefall - 21 Jan 2005 08:22 GMT
> Claes,
>
> > What does your generated code look like?
> How can I see it?

I don't know. I was hoping you could tell me :-)
Isn't there some kind of persisted ouput you can look at
(perhaps some dll that you can decompile)?
Perhaps you could save it to a file somehow.

> > Which is it (alwyas bold or always normal)?
> It always stays bold.  I have added the SetConstant call as below but I
[quoted text clipped - 5 lines]
>
> fb.SetConstant(@"Robert Magnusson");

There is a SetValue method in the FieldBuilder class aswell.
Have you tried that? I don't really know what the difference
is but it's worth trying.

   /claes

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.