Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Enums, PMapper and Comboboxes

8 views
Skip to first unread message

Erik Reischl

unread,
Jan 20, 2006, 2:49:08 AM1/20/06
to

Hello all,

actually, I wanted to continue the thread "How to implement a
gender attribute" by Kostas Terzides from july 2005, but it's no
longer available (found it on google groups, see
http://groups.google.de/group/borland.public.delphi.modeldrivenarchitecture.eco/browse_thread/thread/36bb2e06d7a085bb/db9186a1c6587a50?q=combo&rnum=9#db9186a1c6587a50),
so please excuse me for opening a new thread on an old theme.

My problem is pretty similar: I have a class with some attributes
of (static) enums and want a user to select from these values in
a combobox (and by this, of course, change the value of the
current class instance).

So far, I was able to create a custom enum data type, let's say
for yn_Yes, yn_No and yn_Maybe (actually it's only Yes and No,
but as this thread is about enums, I want to eliminate a simple
solution like 'use boolean then'). I have a PMapper for this with
internal values as char(1) ('Y', 'N', 'M') and the external
representations and can even show these external strings instead
of the original enum values in the Data Grid (thanks to Alois
Schmid in the ecospace.de forum!).

Now, all that is missing is a combobox to be filled with the
external representations of that enum data type, so that the user
can select from this, the attribute to be set to the enum value
and the database later on to get the internal value for storage
(the latter is no problem - PMapper works fine).

My problem: I can't use DataSource, because for this (at least I
assume so???) I would need a handle bound to a class (and the
expression to xxx.allInstances), but there is no such class.

Okay, I could model a class for this, instanciate the enum
values, as Jonas explained in his post a few months ago. However,
that class wouldn't really be associated with my enum data type
but rather another thing itself.

My second approach was to simply fill the Combobox manually, with
the Items property. With databindings-text the selected value
will even be selected in the combobox, but changes there don't
affect the current class instance. For this, of course, I would
need the ValueMember property, but no ValueMember without a
DataSource! I also tried other combinations of databings
properties, such as SelectedItem, SelectedValue and
DisplayMember, but I didn't acheive my goal.

Please help me, I'm tackling with that problem for days now and I
fear to go mad pretty soon :-)

I tried to keep my posting short, anyway I hope you understand
the situation any my problems. Otherwise, please tell me, I will
give more detailed information as needed.

Many thanks,

Erik

Jan Nordén [Borland]

unread,
Jan 20, 2006, 4:10:15 AM1/20/06
to
Erik Reischl wrote:

.
>
> Please help me, I'm tackling with that problem for days now and I
> fear to go mad pretty soon :-)
>
> I tried to keep my posting short, anyway I hope you understand
> the situation any my problems. Otherwise, please tell me, I will
> give more detailed information as needed.
>
> Many thanks,
>
> Erik

Add the following code to the constructor of the form (after the call
to InitializeCompoent):

comboBox1.DataSource = System.Enum.GetValues(typeof(<NameOfEnum>));

In the designer, set the databinding for SelectedValue to the thing you
want to bind to, eg "ehAllPersons - Sex"

No other properties on the conbobox need to be set.

Note that the change will only be applied when you leave the combox.

You can get more direct action by the hack in my blog entry on combobox
binding.

--
Jan Nordén
Senior Software Architect
MDA products group

Erik Reischl

unread,
Jan 23, 2006, 3:13:05 AM1/23/06
to

Hi Jan,

thanks for your immediate reply (I wrote already from home on
friday, but there seemed to be a ng-server problem).

It put me on the right direction and saved my weekend mood :-))

However, allow me a few remarks and some more questions:

>In the designer, set the databinding for SelectedValue to the thing you
>want to bind to

With SelectedValue I received an error, but SelectedItem worked
in this case.

>You can get more direct action by the hack in my blog entry on combobox
>binding.

I've read it a couple of weeks ago, great article, btw.

And now my question:

The current solution allows me to display the internal enum
datatype names, such as 'yn_Yes' and 'yn_No'. However, that's
not really nice.

What I did was to write a custom OCL operation, called
"DisplayEnum" to be used on any of these enum types. For
instance, adding a column to a handle with an expression like
'self.<attribute>->DisplayEnum' works fine.

Now, it would be great to use that OCL operation on what is
displayed in the combobox. However, in your solution, the
DataSource is no handle but we use a system function call. So I
wouldn't know where to use any OCL expression.

So what can I do to display my own representations for the enum
instead of the internal .NET enum strings?

Erik

Jan Nordén [Borland]

unread,
Jan 23, 2006, 9:11:38 AM1/23/06
to
Erik Reischl wrote:

>
> Hi Jan,
>
> thanks for your immediate reply (I wrote already from home on
> friday, but there seemed to be a ng-server problem).
>
> It put me on the right direction and saved my weekend mood :-))
>
> However, allow me a few remarks and some more questions:
>
> > In the designer, set the databinding for SelectedValue to the thing
> > you want to bind to
>
> With SelectedValue I received an error, but SelectedItem worked
> in this case.
>

You are right, my type.

> > You can get more direct action by the hack in my blog entry on
> > combobox binding.
>
> I've read it a couple of weeks ago, great article, btw.
>
> And now my question:
>
> The current solution allows me to display the internal enum
> datatype names, such as 'yn_Yes' and 'yn_No'. However, that's
> not really nice.
>
> What I did was to write a custom OCL operation, called
> "DisplayEnum" to be used on any of these enum types. For
> instance, adding a column to a handle with an expression like
> 'self.<attribute>->DisplayEnum' works fine.
>
> Now, it would be great to use that OCL operation on what is
> displayed in the combobox. However, in your solution, the
> DataSource is no handle but we use a system function call. So I
> wouldn't know where to use any OCL expression.
>
> So what can I do to display my own representations for the enum
> instead of the internal .NET enum strings?
>
> Erik

I see. There are a few options, I will get back to you on this.

Erik Reischl

unread,
Jan 23, 2006, 11:58:43 PM1/23/06
to

=?iso-8859-1?Q?Jan_Nord=E9n_=5BBorland=5D?= <jan.n...@borland.com> wrote:
>I see. There are a few options, I will get back to you on this.

Thanks, Jan. Looking forward to this.

Best wishes,

Erik

Jan Nordén [Borland]

unread,
Jan 24, 2006, 6:44:50 AM1/24/06
to
Erik Reischl wrote:

Actually, come to think of it, the simplest option is just to have an
ExpressionHandle with the expression <EnumName>.allInstances.

Then add two columns to that handle, a "Myself" column with the
expression "self", and a "DisplayString" column with the expression
"self.DisplayEnum".

From there on, the steps in by blog entry should work out of the box.

Erik Reischl

unread,
Jan 25, 2006, 2:57:44 PM1/25/06
to
"Jan Nordén [Borland]" <jan.n...@borland.com> schrieb im Newsbeitrag
news:43d62142$1...@newsgroups.borland.com...

> Actually, come to think of it, the simplest option is just to have an
> ExpressionHandle with the expression <EnumName>.allInstances.

Wow, I didn't expect such a thing to be possible! I'll try it first thing
tomorrow morning in the office.

Thanx!

Erik


Jan Nordén [Borland]

unread,
Jan 25, 2006, 11:21:32 PM1/25/06
to
Erik Reischl wrote:

The evaluator can handle Sex.allInstances. For obvious reasons we did
not implement Double.allInstaces, or even Int32.allInstances:-).

Erik Reischl

unread,
Jan 26, 2006, 12:46:09 AM1/26/06
to

Hi Jan, hi all,

it worked, thanx for the help.

Just two remarks, in case someone else is running into this
problem:

1) For DataBindings now the SelectedValue should be set, as
opposed to the previous method, where it had to be SelectedItem

2) If there are several ComboBoxes each displaying one value of
the same enum type, each ComboBox needs an extra
ExpressionHandle, otherwise, all ComboBoxes would display the
same value. It's a little bit of work if you have quite a number
of these on your form, but it gets the job done (if there is an
easier way, don't hesitate to let me know).

Cheers,

Erik

Jan Nordén [Borland]

unread,
Jan 26, 2006, 1:43:18 AM1/26/06
to
Erik Reischl wrote:

Depending on what find easier;

Note that you you want to you can connect the extra expressionhandler
to the first one with an empty expression, rather than letting them all
have the expression MyEnum.allInstances.

This can also be used in other cases where you need two different
"current positions" in the same list on the same form form.

0 new messages