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 / June 2007

Tip: Looking for answers? Try searching our database.

Enum question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bill Gower - 26 Jun 2007 02:07 GMT
I have a group of 7 checkboxes representing the days of the week.  I have an
enum that that corresponds to the days ex. DaysOfWeek.Sunday,
DaysOfWeek.Monday etc.  I have a library that has a function that I call and
need to pass the days of the week that the user has selected concatenated by
a |.

What is the easiest and cleanest way to go through all the checkboxes and if
the value is true concatenate the enum values together to pass to the
function?

Bill
Peter Duniho - 26 Jun 2007 02:52 GMT
> [...]
> What is the easiest and cleanest way to go through all the checkboxes  
> and if
> the value is true concatenate the enum values together to pass to the
> function?

I will refrain from making absolute claims of "easiest" or "cleanest", but  
it seems to me that you could take advantage of the Tag property of the  
controls and group them together with a group box.  Then your code would  
just enumerate all of the controls in the group box, parsing each one's  
Tag if it's checked, and or-ing that into your enumeration.

For example:

    MyEnum myenum = MyEnum.None;

    foreach (CheckBox chk in groupBox1.Controls)
    {
        if (chk.Checked)
        {
            myenum |= (MyEnum)Enum.Parse(typeof(MyEnum),  
chk.Tag.ToString());
        }
    }

If the Name or Text property of the checkboxes just happened to coincide  
with the names of the enum values, you could even just use either of those  
properties instead as appropriate.

Pete
Christof Nordiek - 26 Jun 2007 08:32 GMT
On Mon, 25 Jun 2007 18:07:56 -0700, Bill Gower <billgower@charter.net>
wrote:

>> [...]
>> What is the easiest and cleanest way to go through all the checkboxes
>> and if
>> the value is true concatenate the enum values together to pass to the
>> function?

> For example:
>
[quoted text clipped - 8 lines]
>         }
>     }

It would be easier, to assign the Enum-values to the Tag properties.

Christof
Diego Jancic - 26 Jun 2007 03:00 GMT
Hi,
If you can modify the function that receives the days in a string, I
suggest use the enum as a binary flag, for example:

[Flags]
enum Days
{
    Monday = 1,
   Tuesday = 2,
   Wednesday = 4,
   Thursday = 8,
   Friday = 16,
   Saturday = 32
}

So you can manage it better using bitwise operators, to read all the
selected values:

Days d;
if (chkMonday.Checked)
    d |= Days.Monday;
if (chkTuesday.Checked)
    d |= Days.Tuesday;
...

To check whether the user choosen a day, you can simply test without
using substrings:

if (selectedDay & Days.Sunday == Days.Sunday)

Anyway, if you can't change that function, I would do it this way:

CheckBox[] chks = new CheckBox[] { chkMonday, chkTuesday,
chkWednesday, ... }
StringBuilder days = new ...();
for (int d = 0 ; d < 7 /* or chks.Length */ ; d++)
{
    if (d != 0)
         days.Append("|");
   days.Append(d);
}

Hope this helps,
Diego
PS - 26 Jun 2007 04:53 GMT
>I have a group of 7 checkboxes representing the days of the week.  I have
>an enum that that corresponds to the days ex. DaysOfWeek.Sunday,
[quoted text clipped - 5 lines]
> if the value is true concatenate the enum values together to pass to the
> function?

If you are going to be dealing with with enum a lot then I would have an
utility class that takes boolean values, which you can pass the checkbox
Checked property.

public static DaysOfWeek Build(
bool sunday,
bool monday,
bool tuesday,
bool wednesday,
bool thursday,
bool friday,
bool saturday)
{
DaysOfWeek daysOfWeek = new DaysOfWeek();
if(sunday) daysOfWeek = daysOfWeek | DaysOfWeek.Sunday;
if(monday) daysOfWeek = daysOfWeek | DaysOfWeek.Monday;
.......
return DaysOfWeek;
}

PS

> Bill
Bob Powell [MVP] - 30 Jun 2007 21:44 GMT
It would be easier for you if the enumeration declaration is tagged with the
Flags attribute.
Then the enum would be or-able.

You can create your text boxes in any of the ways suggested by other posters
and then pass a single value containing the sum of the selected days.

Signature

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>I have a group of 7 checkboxes representing the days of the week.  I have
>an enum that that corresponds to the days ex. DaysOfWeek.Sunday,
[quoted text clipped - 7 lines]
>
> Bill

Rate this thread:







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.