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 sender, EventArgs 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");
//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");
//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");
//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");
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...
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:
Originally Posted by HanneSThEGreaT
Why would I be ****** at you. Just trying to give some advice.
LOL
Well, you did call him/her a:
Originally Posted by HanneSThEGreaT
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...)
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...)
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.