Flag Enums in .NET

Posted by Aliaksandr Tarasevich on 09-Feb-2012 07:52

Hi Guys,

Is there any way to use flag enums in GUI.NET? Here is what I'm trying to accomplish:

NEW Font("Arial", 12, FontStyle:Bold | FontStyle:Italic).

Thanks,

Aliaks

All Replies

Posted by Peter Judge on 09-Feb-2012 07:58

Yep, check out the Progress.Util.EnumHelper class (it's needed because there's no operator overloading in the ABL).

-- peter

Posted by shakitney on 04-May-2012 04:26

I really can't find anything about how to write it in OpenEdge. I can't understand how an Enum is translated in OE...

Nothing on the PSDN to help me, I tried every methods of the EnumHelper. But nothing helps. I need to do the same thing and enumerate fontStyles to create a word processor for Gui.Net. But nothig is explained on how to manage a simple thing as Enum is.

Could Anyone help me PLEASE !!!!

Posted by shakitney on 04-May-2012 04:28

Hi,

Did you find the way to write it in OpenEdge? Because I'm really stuck with it and can't find any solution to resolve the same problem...

Please, if you see it, can you show me the way you used the EnumHelper to enumerate your fontStyles?

Thank you so much !

Posted by neil.fern on 04-May-2012 07:27

Hi Michael,

do you mean you want to 'add' together enums.  From the GUI for .NET manual there's a sample code using anchor styles of a button.  You will need to change the anchor styles to your font enums.:

USING System.Windows.Forms.* FROM ASSEMBLY.
USING Progress.Util.* FROM ASSEMBLY.
DEFINE VARIABLE rButton AS Button.
DEFINE VARIABLE rEnum AS System.Enum.
rButton = NEW Button( ).

/* adds both bottom and Right to the Enum of styles*/
rEnum = EnumHelper:Or( AnchorStyles:Bottom, AnchorStyles:Right ).


rButton:Anchor = CAST(rEnum, AnchorStyles).
MESSAGE rButton:Anchor VIEW-AS ALERT-BOX.

This then puts both Bottom and Right into the rEnum of styles, you will obviously substitute your font styles for these anchor styles.

I hope this is what you're after.

Posted by shakitney on 04-May-2012 08:30

Hi Neil, and thanx for your answer.

Actually I'm trying to do exactly what Aliaksandr explained in the first post of this topic.

NEW Font("Arial", 12, FontStyle:Bold | FontStyle:Italic).

I tried lots of things and I'm on a point that I cannot understand anymore where the logic is,

just to write something as simple as "FontStyle:Bold | FontStyle:Italic"...

I'm here now :

When the user clicks on any of the buttons "Bold", "Italics", "Underline" or "StrikeOut", the

program must keep the previous FontStyle so it's not overwritten by the new FontStyle.

The new fontStyle must be added to the existing FontStyle.

the .net class System.Drawing.FontStyle returns an Enum type, so I've been tryin' that, even

though it doesn't work :

DEFINE VARIABLE cl_style AS System.Enum NO-UNDO.

IF e:Tool:Key = "bold" OR e:Tool:Key = "Italics" OR e:Tool:Key = "Underline" OR e:Tool:Key = "Strike" THEN

DO:

     cl_style = mFontStyle(e:Tool:CaptionResolved).

     CAST(cl_style, System.Drawing.FontStyle).

     THIS-OBJECT:rtb_astuces:SelectionFont = NEW System.Drawing.Font(THIS-OBJECT:rtb_astuces:SelectionFont:FontFamily,

                                                                                                             THIS-OBJECT:rtb_astuces:SelectionFont:Size.

                                                                                                             cl_style,

                                                                                                              System.Drawing.GraphicsUnit:Point,

                                                                                                              System.Convert:ToByte(0)).

END.

/* And here is the mFontStyle method : */

METHOD PRIVATE System.Enum mFontStyle(cl_styleClick AS CHARACTER):

DEFINE VARIABLE cl_style as System.Enum NO-UNDO.

DEFINE VARIABLE cl_Enum  as System.Enum NO-UNDO.

cl_style = THIS-OBJECT:rtb_astuces:SelectionFont:Style.

cl_Enum = cl_style.

IF NOT THIS-OBJECT:rtb_astuces:SelectionFont:Bold THEN

DO:

IF cl_StyleClick = "Bold" THEN

DO:

cl_Enum = Progress.Util.EnumHelper:AND(cl_style, FontStyle:Bold).

cl_style = cl_Enum.

END.

END

/* same thing for other Font Styles */

RETURN cl_style.

END METHOD.

rtb_astuces is a RichTextBox.

This does not work and I can't understand why.

this sentence :

THIS-OBJECT:rtb_astuces:SelectionFont = NEW System.Drawing.Font(THIS-OBJECT:rtb_astuces:SelectionFont:FontFamily,

THIS-OBJECT:rtb_astuces:SelectionFont:Size,

cl_style,

System.Drawing.GraphicsUnit:Point,

System.Convert:ToByte(0)).

always shows an error if :

1 cl_style is an Enum that was casted into FontStyle after the assignment of the values.

2 cl_style is of FontStyle type and is processed as an Enum... though IT IS AN ENUM !!!

3 If I don't do like that I lose every FontStyling each time someone clicks on a style button...

I'm lost...

Ce message a été modifié par: Michael Garrissac

Posted by shakitney on 04-May-2012 09:15

Hi Neil, think I'm done with this problem, I didn't put the returned value of the cast into another variable....

Thank you for your answer.

I must now try to look if the rest of the program works.

Posted by Laura Stern on 04-May-2012 13:02

I'm not totally sure what you're doing here. But let me just clarify how enums work You cannot use any ABL operator with an enum, including the equal operator.  For example, you cannot say:

if myStyleEnum:Bold = "Bold" ..

or even

if myStyleEnum:Bold = someObj:FontStyle

An enum is just an object.  It's value is an object reference, not an integer or a string.  Plus it is also a value type.  Therefore, whenever we reference an enum, we get back a unique object reference (a copy of the object).  If we access the same enum twice, we get back two different, distinct object references.  They will never be equal.  That is why you must use the EnumHelper object to do any operation, such as ANDs ORs, comparisons, etc.

Plus you had this statement in your code:

CAST(cl_style, System.Drawing.FontStyle).

This is kind of a no-op.  It does verify that cl_style is of type FontStyle.  But it doesn't do anything to the object.  It is really to tell the compiler to treat an object defined one way as if it were something else (generally a child type), because you know it will be that type at runtime.  So I'm not sure why this is here.

We plan to add a compiler warning when you use an ABL operator with an enum so that you'll know that it is not going to give you the behavior you want.

This thread is closed