By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,460 Members | 1,508 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,460 IT Pros & Developers. It's quick & easy.

Create Flaggable Enum From Checkbox List

P: n/a
I have an enum for the days of the week: DaysOfTheWeek.Sunday,
DaysOfTheWeek.Monday, etc. that is flaggable so that mutliple days of
the week can be indicated.

I also have a checkbox list representing the days of the week. How
can I "convert" the checked items into a DaysOfTheWeek enum that
represents which days are selected in the checked list box?

Thanks!
Bryan Green
Jul 3 '08 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You should be able to cast each item in turn to a DaysOfTheWeek, and
"or" them together; example below.

Marc

using System;
using System.Windows.Forms;
[Flags]
enum DOW
{
None = 0, Mon = 1, Tue = 2, Wed = 4, Thur = 8, Fri = 16, Sat = 32,
Sun = 64
}
static class Program
{
static void Main()
{
Application.EnableVisualStyles();

CheckedListBox clb = new CheckedListBox { Dock =
DockStyle.Fill };
foreach(DOW dow in Enum.GetValues(typeof(DOW))) {
clb.Items.Add(dow,false);
}
Button btn = new Button { Dock = DockStyle.Bottom, Text =
"Show Me"};
btn.Click += delegate
{
DOW tally = DOW.None;
foreach (DOW dow in clb.CheckedItems)
{
tally |= dow;
}
MessageBox.Show(btn, tally.ToString(), "Checked days");
};
Application.Run(new Form { Controls = { clb, btn } });
}
}
Jul 3 '08 #2

P: n/a
Worked like a champ! Thank you VERY much.

Bryan

Jul 3 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.