Click here to Skip to main content
15,883,623 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
hi all...

i want to check the checkbox if the checkbox in another form is unchecked..
if checkbox1 is unchecked then the checkbox of other form must be checked..

my code is...

in checkedchanged event of 2nd checkbox...


C#
Form1 f1 = new Form1();

bool chk = false;

if (!checkBox2.Checked)
    chk = true;
f1.checkBox1.Checked = chk;

here checkbox2 is checkbox of 2nd form...
its not working...
can anyone help me...

Thanks in Advance...
Posted
Comments
Sergey Alexandrovich Kryukov 23-May-11 3:05am    
Not clear what's "not working". There is no such thing for a software developer. There are compilation errors, exceptions, exception message and stack, etc.
--SA

A popular question. The most robust approach to form collaboration is implementing some interface in the form class.
Please see my past answer, other answers and the discussion:
How to copy all the items between listboxes in two forms[^].

—SA
 
Share this answer
 
When you click the button or something to open the Form1, place following code on that location.

Form1 f1 = new Form1();
f1.checkBox1.Checked = !checkBox2.Checked;
f1.Show();


This is enough to do what you want.

Currently you create a new object everytime you check/uncheck the check box, and I am sure you must be creating a new object where you show the form too.

[Edit]
If form is already open or you don't want the form to open at all; then put your declaration of form Form1 f1 = new Form1(); at the top of class to get the single object of the form.
 
Share this answer
 
v2
Comments
Harish Reddy K 23-May-11 4:09am    
hi.. thanks for ur reply..
but i dont want to show the second form..
i just want to check the checkbox2 when checkbox1 is unchecked and viseversa...
where checkbox1 is the control of form1 and checkbox2 is control of form2...

thanks...
Prerak Patel 23-May-11 4:23am    
Sorry, but it is not quite clear what you want to do. If form is already open or you don't want the form to open at all; then put your declaration of form Form1 f1 = new Form1(); at the top of class to get the single object of the form.
Harish Reddy K 23-May-11 4:47am    
first we open the form1... there if we check the checkbox1 the automatically the checkbox2 will be unchecked which is in form2..

in some times the form2 opens if the user checked the checkbox2 then automatically checkbox1 in form1 will be unchecked...

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

  Print Answers RSS


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