Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
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    
hii think first u hv to take three check box(books,cd,both)then it would be possible

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
 
Share this answer
 
Comments
Ankur\m/ 12-Jul-11 3:19am    
I agree, my 5 too!
Sergey Alexandrovich Kryukov 12-Jul-11 4:06am    
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
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
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jul-11 21:54pm    
Agree, my 5. See my solution which logically resolved it.
--Sa
Hello

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

C#
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;
}
 
Share this answer
 
Comments
RaviRanjanKr 11-Jul-11 8:13am    
Nice, My 5 :)
Shahin Khorshidnia 11-Jul-11 8:23am    
Thank you RaviRanjankr.
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
 
Share this answer
 
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.
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.
 
Share this answer
 
v3
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
 
Share this answer
 
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.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900