Click here to Skip to main content
14,639,719 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want some help!!

I have two check box named :Book and another :CD.

After form load default Book is checked.Now i want to check CD, but what happens is that both book and CD are check.
But i want like as when i click on CD, book should be unchecked, and when i click on book, then CD should be unchecked and in one case if i wish to click both Book and CD then both should be clicked.

I have tried using radio button but in radio button ,both Book and CD are not check at the sometime.


Can somebody help me out?
Posted
Updated 11-Jul-11 2:02am
v2
Comments
Shahin Khorshidnia 11-Jul-11 7:57am
   
Do you have 3 items? 1.Book, 2.Cd, 3.Both
if not, how can you click both book and cd?
advancedansh 11-Jul-11 8:08am
   
hi i think first u hv to take three check box(books,cd,both)then it would be possible
Rate this:
Please Sign up or sign in to vote.

Solution 6

In this exact case, you still need to use radio buttons:

You need 3 (three) of them: 1) CD, 2) Book, 3) Both.

Keep it simple!

—SA
   
Comments
Ankur\m/ 12-Jul-11 3:19am
   
I agree, my 5 too!
   
Thank you, Ankur,
--SA
Espen Harlinn 12-Jul-11 9:55am
   
Nice reply, would simplify things a bit, my 5
Sergey Alexandrovich Kryukov 12-Jul-11 10:36am
   
Thank you, Espen.
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 5

when i click on CD, book should be unchecked, and when i click on book, then CD should be unchecked
So far so good.
Now when you say, in one case if i wish to click both Book and CD then both should be clicked.
How do you think that will be possible? Checking a checkbox, un-checks other. So you could logically never check both the checkboxes.

The only way you could do this keeping your 1st requirement intact is trigger check both option using a different control, say another checkbox which says 'Check Both'. (The way how to do it using events is already described in the previous answers).
I hope you understand what I am saying. Let me know if you have any doubts.

Cheers
Ankur
   
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 21:54pm
   
Agree, my 5. See my solution which logically resolved it.
--Sa
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hello

If you have 3 Items (Book, Cd, Both):

private void Both_CheckedChanged(object sender, EventArgs e)
{
    Book.Checked = true;
    Cd.Checked = true;
}

private void Book_CheckedChanged(object sender, EventArgs e)
{
    if (!Both.Checked)
        Cd.Checked = !Book.Checked;
    if (!Book.Checked)
        Both.Checked = false;
}

private void Cd_CheckedChanged(object sender, EventArgs e)
{
    if (!Both.Checked)
        Book.Checked = !Cd.Checked;
    if (!Cd.Checked)
        Both.Checked = false;
}
   
Comments
RaviRanjanKr 11-Jul-11 8:13am
   
Nice, My 5 :)
Shahin Khorshidnia 11-Jul-11 8:23am
   
Thank you RaviRanjankr.
Rate this:
Please Sign up or sign in to vote.

Solution 7

hi,

if u need to check one at a time u can use radio buttons if you group them they allow to check one at time . if check-box need then u have to use events... or try simple j Query that will be useful



Happy Coding
   
Comments
Christian Graus 12-Jul-11 0:03am
   
This has been answered at length. I'm not sure what you think this added, except for an assumption that this is about a web site. I don't see where anyone says that is true.
Rate this:
Please Sign up or sign in to vote.

Solution 2

If u want to check both then use checkboxes
eg:
// both selected
if((checkbox1.Checked==true)&&(checkbox2.Checked==true)

   label1.Text="Selected First and second";
			
	else if(checkbox1 .Checked ==true) 
			label1.Text="Selected First";
	else if (checkbox2 .Checked ==true)
			label1.Text="Selected second"; 
		else		 	
	label1.Text ="PleaseSelect";// doesnt check


Hope this helps.
   
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

Hi,

You'll have to use checkbox checked event
and there you can checked some thing like this

if(cd.checked)
book.unchecked

else
book.checked
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

Hi,

I think your requirement is like as.

After form load if any check box is checked by default then is should be unchecked after clicking on another Check Box.
You can use a bool variable which value is set true only on form load. Then use it CheckBox.check event.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100