From: DanB on

Say I want to add a comboBox to a form and have it populated with the
enum of an object. Is there a way to do this quickly? I've been creating
the list by hand.

Thanks, Dan.

Like for Markers:

namespace System.Windows.Forms.DataVisualization.Charting
{
// Summary:
// Specifies a style for markers.
public enum MarkerStyle
{
// Summary:
// No marker is displayed for the series or data point.
None = 0,
//
// Summary:
// A square marker is displayed.
Square = 1,
//
// Summary:
// A circular marker is displayed.
Circle = 2,
//
// Summary:
// A diamond-shaped marker is displayed.
Diamond = 3,
//
// Summary:
// A triangular marker is displayed.
Triangle = 4,
//
// Summary:
// A cross-shaped marker is displayed.
Cross = 5,
//
// Summary:
// A 4-point star-shaped marker is displayed.
Star4 = 6,
//
// Summary:
// A 5-point star-shaped marker is displayed.
Star5 = 7,
//
// Summary:
// A 6-point star-shaped marker is displayed.
Star6 = 8,
//
// Summary:
// A 10-point star-shaped marker is displayed.
Star10 = 9,
}
}

From: Peter Duniho on
DanB wrote:
>
> Say I want to add a comboBox to a form and have it populated with the
> enum of an object. Is there a way to do this quickly? I've been creating
> the list by hand.

What does "enum of an object" mean? Do you actually mean "values of an
enum"?

If so, this might work for you:

static void AddEnumValues<T>(this ComboBox combo)
{
combo.Items.AddRange(
Enum.GetValues(typeof(T)).Cast<object>().ToArray());
}

used like this (extension method syntax):

comboBox1.AddEnumValues<MarkerStyle>();

or if you don't use the extension method syntax (assuming the method is
in a class named "Util"):

Util.AddEnumValues<MarkerStyle>(comboBox1);

Pete
From: DanB on
Peter Duniho wrote:
> DanB wrote:
>>
>> Say I want to add a comboBox to a form and have it populated with the
>> enum of an object. Is there a way to do this quickly? I've been
>> creating the list by hand.
>
> What does "enum of an object" mean? Do you actually mean "values of an
> enum"?

Thanks Peter,
I meant the 'names' of the numeration. And at 'writing' time. Yes I see
I can get the names at run time. I do that to look up the string name in
the enum to get the value now.

I'm doing this kind of stuff:

To box:
tname= Enum.GetName( typeof( SeriesChartType ), curItem.seriesSet[ pos
].type );
comboBoxChartType1.SelectedIndex= comboBoxChartType.FindStringExact(
tname );;

From box:
curItem.seriesSet[ pos ].type= (SeriesChartType)Enum.Parse(
typeof(SeriesChartType), comboBoxChartType.Text, true );

And because my .type is of type SeriesChartType, it parses out to the
xml by name without the need of my creating an interface, I like that.

This is way different thinking than in c++, I'm still in the deep
learning curve.

Best, Dan.
From: Peter Duniho on
DanB wrote:
> [...]
> To box:
> tname= Enum.GetName( typeof( SeriesChartType ), curItem.seriesSet[ pos
> ].type );
> comboBoxChartType1.SelectedIndex= comboBoxChartType.FindStringExact(
> tname );;

If you initialize the items in the ComboBox to the actual enum values
(e.g. as in the code I posted earlier), then you can simply set the
SelectedItem property to the enum value in question. The ComboBox class
will handle the rest, without you having to explicitly call something
like FindStringExact().

So, when the ComboBox is properly initialized, you get something more
like this for the above code:

comboBoxChartType1.SelectedItem = cutItem.seriesSet[pos].type;

Likewise, this:

> From box:
> curItem.seriesSet[ pos ].type= (SeriesChartType)Enum.Parse(
> typeof(SeriesChartType), comboBoxChartType.Text, true );

�becomes this:

curItem.seriesSet[pos].type =
(SeriesChartType)comboBoxChartType1.SelectedItem;

Pete
From: DanB on
Peter Duniho wrote:
>
> If you initialize the items in the ComboBox to the actual enum values
> (e.g. as in the code I posted earlier), then you can simply set the
> SelectedItem property to the enum value in question. The ComboBox class
> will handle the rest, without you having to explicitly call something
> like FindStringExact().
>
> So, when the ComboBox is properly initialized, you get something more
> like this for the above code:
>
> comboBoxChartType1.SelectedItem = cutItem.seriesSet[pos].type;
>
> Likewise, this:
>
>> From box:
>> curItem.seriesSet[ pos ].type= (SeriesChartType)Enum.Parse(
>> typeof(SeriesChartType), comboBoxChartType.Text, true );
>
> �becomes this:
>
> curItem.seriesSet[pos].type =
> (SeriesChartType)comboBoxChartType1.SelectedItem;

Hi Peter,
Old habits. I would avoid storing data as an index whenever possible.
Now my 'type' would be a plain old int rather than a type with
appropriate baggage. What if there were a revision and a new name where
put in at position 3? If stored by name versioning is automatic. I can
change the order in my combo, add delete stuff without maintinance of
the database.

But then I see that XmlSerialize is rather non forgiving. My xml lib
doesn't care if stuff comes and goes. I have not had to version my
product xml databases ever for small structural changes, additions,
deletions. In fact, if I don't explicitly delete and rewrite a set of
elements, legacy elements will ride along forever. Where as one little
thing out of place and XmlSerialize completly fails on me. It doesn't
return any of my data. It means I'll have to run a backup utility and
preserve/reload if a read fails I guess. Here is a snip from one database:

<VirtualFileTable>
<VirtualItem>
<vName>DEFAULT_PROJECT_NAME</vName>
<path>Default.ars</path>
<flags>SYSTEM,PERMANENT,EXFROMREPORT</flags>
....

The flags are an enum in the program and I can mess with it anytime I'd
like without affecting previous use.

Best, Dan.
 |  Next  |  Last
Pages: 1 2 3
Prev: xml and xml schema
Next: building process