Reputation: 229
I have two sections(primary and secondary) in a form with several textboxes which display information. On the secondary section I've added a checkbox (which when checked) I want to copy the information from the fields on the primary section into the fields on the secondary side. If its unchecked I want it to leave the field as blank.
Here is some sample code to help see what I am trying to do:
The CheckChanged event calls four different methods (each method contains logic for a specific checkbox):
private void CheckBoxCheckedChanged(
{
Method1();
Method2();
Method3();
Method4();
}
When Method4 is called I'd like to process the logic mentioned above if its checked, and leave blank if its not.
private void Method4()
{
if (checkBox4.Checked = true)
{
secondaryTextbox.Text = primaryTextbox.Text;
}
else if (checkBox4.Checked = false)
{
secondaryTextbox.Text = "";
}
}
The issue I'm having is that when I do this once the checkbox is checked, I can no longer 'uncheck' to change the field back to being blank.
What am I doing wrong, or is there a better way to go about this?
I apologize ahead of time if I posted this wrong, this is my first time using SO.
Upvotes: 0
Views: 534
Reputation: 838086
The code you wrote makes an assignment (=
) inside the if statement's expression, but this is not what you mean. You should use ==
if you want to make a comparison. Or even better, just do this instead:
if (checkBox4.Checked)
{
secondaryTextbox.Text = primaryTextbox.Text;
}
else
{
secondaryTextbox.Text = "";
}
As Paolo pointed out the code you tried gives a compiler warning. If you try to never write code that produces warnings you can catch simple errors like this more quickly. There's even an option to treat warnings as errors which you can use.
Upvotes: 4
Reputation: 1272
You have a very small problem in your code:
you are using = instead of ==
also it is better to define an array of check-box and text-box instead of func1,func2,func3,func4
Upvotes: 1
Reputation: 34128
You are doing assignment when you mean to be testing
use this
if (checkBox4.Checked == false)
or better this
if (false == checkBox4.Checked)
or even better this
if ( ! checkBox4.Checked)
instead of this
if (checkBox4.Checked = false)
Upvotes: 1
Reputation: 57892
You are using a single "=" to test of the checkbox is checked. THis has the effect of setting the checkbox state.
Use "==", as in
if (checkBox4.Checked == true)
or in fact, much better:
if (checkBox4.Checked)
Upvotes: 0
Reputation: 5922
You need
if (checkBox4.Checked == true)
to check if true, the way you were doing it was always assigning it the value "true"
Upvotes: 2