CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2011
    Posts
    59

    limit number of Checkboxes checked

    Hey guys

    I got some working code here, but it´s waaay too long,
    what it does is:
    I have a GroupBox containing 4 Group Boxes.
    In these GroupBoxes are
    -3 checkboxes (in groupbox wann)
    -2 checkboxes (in groupbox wie)
    -3 checkboxes (in groupbox womit)
    -6 checkboxes (in groupbox woher)

    when the button is clicked, store the names of each checked checkbox into an arraylist. when the count is bigger than 4 the arraylist is cleared.
    also i store each checked checkbox name in a seperate arraylist for each groupbox. when the count is bigger than 1 the arraylist is cleared and the checked boxes unchecked.

    what i want:
    from each of the 4 groupboxes (wann, wie, womit, woher) only ONE checkbox may be checked.
    The names of the 4 selected one shal then be displayed in a ListBox.

    i already thought i could somehow forget the first part of my code. Because when only one box per groupbox may be checked, that means, that no more than 4 CAN be checked altogether.
    but somehow i don´t get that working..
    maybe u can help me shorten my code.. this is really way too long
    (I use Visual Studio 2010, writing in C#
    i chose to chow the code in PHP cause i find it easier to follow with the different colorings)

    thx in advance
    Cheers


    PHP Code:
       /// <summary>
            /// The Kategories are selected here.
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            
    private void acceptButton_Click(object senderEventArgs e)
            {
                
    //Create 4 new Arraylists, containing the Kategories and GroupBoxes.
                
    ArrayList Kategorien = new ArrayList();
                
    ArrayList Wann = new ArrayList();
                
    ArrayList Woher = new ArrayList();
                
    ArrayList Womit = new ArrayList();
                
    ArrayList Wie = new ArrayList();

                
    //For each Checked Box in the Groupbox "Wann"
                //The Name is added to the ArrayList.
                //The Special Checkbox Name is also moved to the Array for the Groupbox.
                
    foreach (CheckBox c in WannGroupbox.Controls)
                    if (
    c.Checked)
                        
    Kategorien.Add(c.Text);
                foreach (
    CheckBox k in WannGroupbox.Controls)
                    if (
    k.Checked)
                        
    Wann.Add(k.Text);
                
    //For each Checked Box in the Groupbox "Wie"
                //The Name is added to the ArrayList.
                //The Special Checkbox Name is also moved to the Array for the Groupbox.
                
    foreach (CheckBox c in WieGroupbox.Controls)
                    if (
    c.Checked)
                        
    Kategorien.Add(c.Text);
                foreach (
    CheckBox k in WieGroupbox.Controls)
                    if (
    k.Checked)
                        
    Wie.Add(k.Text);
                
    //For each Checked Box in the Groupbox "Womit"
                //The Name is added to the ArrayList.
                //The Special Checkbox Name is also moved to the Array for the Groupbox.
                
    foreach (CheckBox c in WomitGroupbox.Controls)
                    if (
    c.Checked)
                        
    Kategorien.Add(c.Text);
                foreach (
    CheckBox k in WomitGroupbox.Controls)
                    if (
    k.Checked)
                        
    Womit.Add(k.Text);
                
    //For each Checked Box in the Groupbox "Woher"
                //The Name is added to the ArrayList.
                //The Special Checkbox Name is also moved to the Array for the Groupbox.
                
    foreach (CheckBox c in WoherGroupbox.Controls)
                    if (
    c.Checked)
                        
    Kategorien.Add(c.Text);
                foreach (
    CheckBox k in WoherGroupbox.Controls)
                    if (
    k.Checked)
                        
    Woher.Add(k.Text);

                
    //The Arraylist may only contain 4 items alltogether.
                //If there are more the Arraylist is cleared.
                
    if (Kategorien.Count 4)
                {
                    
    Kategorien.Clear();
                    
    MessageBox.Show("Bitte nur 4 Kategorien auswählen"); 
                }
                    
    //If the ArrayList contains less than one Item, a Messagebox is shown.
                
    else if (Kategorien.Count 1)
                                
    MessageBox.Show("Bitte mindestens eine Kategorie auswählen");

                
    //The special ArrayList for each GroupBox are checked. Each may contain only one Item
                //To make sure, the kategories are specific and not confusing.
                //If more than one box in checked in a Groupbox, the selection is resettet.
                
    if (Wann.Count 1)
                {
                    
    MessageBox.Show("Bitte nur eine Kategorie pro Box");

                    foreach (
    CheckBox chck in WannGroupbox.Controls)
                    {
                        
    chck.Checked false;
                    }
                }

                
    //The special ArrayList for each GroupBox are checked. Each may contain only one Item
                //To make sure, the kategories are specific and not confusing.
                //If more than one box in checked in a Groupbox, the selection is resettet.
                
    if (Wie.Count 1)
                {
                    
    MessageBox.Show("Bitte nur eine Kategorie pro Box");

                    foreach (
    CheckBox chck in WieGroupbox.Controls)
                    {
                        
    chck.Checked false;
                    }
                }

                
    //The special ArrayList for each GroupBox are checked. Each may contain only one Item
                //To make sure, the kategories are specific and not confusing.
                //If more than one box in checked in a Groupbox, the selection is resettet.
                
    if (Womit.Count 1)
                {
                    
    MessageBox.Show("Bitte nur eine Kategorie pro Box");

                    foreach (
    CheckBox chck in WomitGroupbox.Controls)
                    {
                        
    chck.Checked false;
                    }
                }

                
    //The special ArrayList for each GroupBox are checked. Each may contain only one Item
                //To make sure, the kategories are specific and not confusing.
                //If more than one box in checked in a Groupbox, the selection is resettet.
                
    if (Woher.Count 1)
                {
                    
    MessageBox.Show("Bitte nur eine Kategorie pro Box");

                    foreach (
    CheckBox chck in WoherGroupbox.Controls)
                    {
                        
    chck.Checked false;
                    }
                }

                
    //The Listbox shows the final solution.
                //At least one, at most 4 Kategories
                //Maximum one of each GroupBox.
                
    listBox1.DataSource Kategorien;
            } 
    Attached Images Attached Images  

  2. #2
    Join Date
    Jun 2011
    Posts
    3

    Re: limit number of Checkboxes checked

    Ratio Box? If only one of each category can be clicked....

  3. #3
    Join Date
    Jan 2011
    Posts
    59

    Re: limit number of Checkboxes checked

    hmm,
    can&#180;t find that one in my toolbox



    ah ok Radio u mean

    u mean putting them in a groupbox?
    Last edited by LisaWvL; June 7th, 2011 at 08:51 AM.

  4. #4
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: limit number of Checkboxes checked

    This is the whole reason why a checkbox and a radiobutton exist. What I'm trying to say is that use a Radio Button for single choices. Use checkboxes for multiple choices. Any PC noob learns it like that...

  5. #5
    Join Date
    Jan 2011
    Posts
    59

    Re: limit number of Checkboxes checked

    i just thought it is multiple choice.
    out of all checkboxes (14) you are allowed to choose 4

    but only 1 in each groupbox.

    i didn´t know the groupbox limits the radiobuttons it contains so much.
    thx anyways and no reason to get ******
    Last edited by HanneSThEGreaT; June 8th, 2011 at 01:02 AM. Reason: removed profanity

  6. #6
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    Re: limit number of Checkboxes checked

    Quote Originally Posted by Cheers1337 View Post
    thx anyways and no reason to get ******
    Why would I be ****** at you. Just trying to give some advice.

    Seems like one of those mornings that I shouldn't have woken up.

    Geez!

    Trust me dude, you haven't seen me get ****** ......... yet

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: limit number of Checkboxes checked

    Quote Originally Posted by Cheers1337 View Post
    i just thought it is multiple choice.
    out of all checkboxes (14) you are allowed to choose 4

    but only 1 in each groupbox.

    i didn´t know the groupbox limits the radiobuttons it contains so much.
    thx anyways and no reason to get ******
    Radio buttons should be used when exclusive selection within a group is desired. Use checkboxes in a group when multiple selections are required.

  8. #8
    Join Date
    Jan 2010
    Posts
    1,133

    Re: limit number of Checkboxes checked

    Quote Originally Posted by Arjay View Post
    Radio buttons should be used when exclusive selection within a group is desired. Use checkboxes in a group when multiple selections are required.
    Just to rephrase that and emphasize certain parts, to avoid any potential confusion in the future:
    Radio buttons should be used when exclusive selection within a group is desired. Use checkboxes in a group when multiple selections within a group are required.
    Anyway, the point Arjay is making is that (a) radio-buttons are meant to be used for a set of mutually exclusive options, and (b) radio-buttons have this behavior built-in, whereby several radio-buttons are considered to be related if they are in the same container (a group-box in your case). Well, as long as the AutoCheck property is set to true.

    Off topic:
    Quote Originally Posted by HanneSThEGreaT View Post
    Why would I be ****** at you. Just trying to give some advice.
    LOL
    Well, you did call him/her a:
    Quote Originally Posted by HanneSThEGreaT View Post
    PC noob
    I guess the the OP interpreted that as you being ******.

    But, funny how the d4mn system filters out words like d4mn, but you had to edit the post to replace ****** with "******". That's just wrong - and now I'm ****** at the system.
    ( This is just too much fun...)

  9. #9
    Join Date
    Jan 2011
    Posts
    59

    Thumbs up Re: limit number of Checkboxes checked

    Quote Originally Posted by TheGreatCthulhu View Post
    But, funny how the d4mn system filters out words like d4mn, but you had to edit the post to replace ****** with "******". That's just wrong - and now I'm ****** at the system.
    ( This is just too much fun...)
    lol

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured