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.
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: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.