Needed small code rather than big code for check boxes

Posted by Rajivksharma under C# on 5/20/2009 | Views : 1764 | Status : [Member] | Replies : 11
My Problem is:
Im having five check boxes ch1, ch2, ch3, ch4, ch5

if(ch1.checked == true && ch2.checked == true)//if user select check box ch1 and ch2 then result some result
{
Some result;
}


Total for five check boxes there are so many combinations like ch1 and ch3, ch1 and ch4 , ch1 and ch5 etc..


I want some code which is small and can help me in putting these so many combination s




Responses

Posted by: Nareshgoradara on: 5/21/2009 [Member] Starter

Up
0
Down
Hello,

What you actually want??

Did you want that any of two checkbox must be checked or ???

Please specify.

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajivksharma on: 5/21/2009 [Member] Starter

Up
0
Down
Any combination of check box can be selected

for eg. combination of ch1 and ch2,
combination of ch1, ch2 and ch3

Note: Means combination of five check boxes can select.

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Syedshakeer on: 5/21/2009 [Member] Starter

Up
0
Down
Hi

How to get a value of Selected item from a ChekboxList

1)first drag and drop a CheckBoxList on your webPage.
2)Add Some Items.
3)Drag and drop one Button.

After selecting Your items from Checkboxlist, Click on Button to get values or text selected by you.

protected void Submit_Click(object sender, EventArgs e)
{
String str=null ;
for (int i = 0; i < CheckBoxList1.Items.Count ; i++)
{
if (CheckBoxList1.Items.Selected == true)
{
Response.Write (str = CheckBoxList1 .Items .Text .ToString ()+"<br>");

}
}
}


Plz give me feedback on my code
Thanks for posting your question....

Syed Shakeer Hussain

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 5/21/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Afternoon Rajivksharma

Todays was so hectic, am sorry i could not answer you in the morning. The Answer is Simple, Micorsoft reliazed the Problem and they have added a Control that allows you to have Multiple check boxes as if they are put on a panel. Go to your Toolbox and check the "General Section" there is something called "CheckBoxList" and this is how you use them

string msg = "You have selected the following items:<br />";



if (check1.Items[0].Selected) {

msg = msg + check1.Items[0].Text + "<br />";

}

if (check1.Items[1].Selected) {

msg = msg + check1.Items[1].Text + "<br />";

}

if (check1.Items[2].Selected) {

msg = msg + check1.Items[2].Text + "<br />";

}



Message.Text = msg


and if you are using VB.net this is how its done

http://www.java2s.com/Code/ASP/Asp-Control/LoopthroughaspcheckboxlistVBnet.htm

Thank you for using Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajivksharma on: 5/21/2009 [Member] Starter

Up
0
Down
Thanks for replying to my question but im having some different problem plz take a look


My problem is: I already taken five check boxes not check box list.

And im having five check boxes , User can select any combination of check boxes.

For eg. Five check Boxes Are: Ch1, ch2, ch3, ch4, ch5

if(ch1.checked == true)
{
This result;
}
if(ch1. checked == true || ch2.checked == true || ch3. checked == true )
{
other result;
}
Like this for five check boxes there are so may combinations So many if conditions i have to implement.

I want some small code rather than so may if conditions.

Plz help me. Thanks in Advance.

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajivksharma on: 5/21/2009 [Member] Starter

Up
0
Down
Thanks for replying .. Plz take a look about this issue below


Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nareshgoradara on: 5/22/2009 [Member] Starter

Up
0
Down
Hi,

Let me tell what I understand in your problem:

You have 5 checkbox: ch1,ch2,ch3,ch4,ch5

You have different different condition for different selection.
like

if(ch1.checked)

{
Condition 1
}
if(ch2.checked)
{
Condition 2
}
if(ch3.checked)
{
Condition 3
}
if(ch4.checked)
{
Condition 4
}
if(ch5.checked)
{
Condition 5
}
if(ch1.checked || ch2.checked)
{
Condition 5
}
....
...
if(ch1.checked || ch2.checked || ch3.checked || ch4.checked || ch5.checked)
{
Condition 60
}



Like above??

ANd you want any short cut method for doing this repeted work.

Am I understand right???
Please confirm.

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajivksharma on: 5/22/2009 [Member] Starter

Up
0
Down
Yes you are absolutely right . Exactly this is my problem. Thanks for understanding. Plz provide some solution

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nareshgoradara on: 5/23/2009 [Member] Starter

Up
0
Down
Thanks for confirm.
Is there any similarity in applying logic for CheckBoxes?

Can you divide your logic for checkboxes?

it depends on your logic, that which type of logic you are applying. Generally you do it manually if there is no similarity.





Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rajivksharma on: 5/23/2009 [Member] Starter

Up
0
Down
Sorry, im not getting about dividing logic.

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 5/26/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Rajivksharma you did not Consider to use Checkboxlist and use Switch on your login ?

Thank you for posting at Dotnetfunda
[Administrator]

Rajivksharma, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response