Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have the following code in a portion of my program that hides/shows certain elements based on the status of a certain checkbox:

private void enableFolderVariableRemoval_CheckedChanged(object sender, EventArgs e)
{
    if (enableFolderVariableRemoval.Checked)
    {
        cleanFolderTextPanel.Visible = true;
        cleanTextPanel.Visible = true;
    }
    else
    {
        cleanFolderTextPanel.Visible = false;
        if (cleanFilenameTextPanel.Visible == false)
        {
            cleanTextPanel.Visible = false;
        }
    }
}

Is there a better way to handle this without a whole bunch of conditionals that set other controls to hide/show?

share|improve this question

3 Answers

up vote 6 down vote accepted

Not sure if there are any other constraints but here is one possible solution:

private void enableFolderVariableRemoval_CheckedChanged(object sender, EventArgs e)
{
    cleanFolderTextPanel.Visible = enableFolderVariableRemoval.Checked; 
    cleanTextPanel.Visible = cleanFolderTextPanel.Visible || cleanFilenameTextPanel.Visible;        
}
share|improve this answer

I'm not sure what the logic is in your code, the else and nested if in it is really confusing. But from what I can understand:

You can set the Visible Attribute for cleanFolderTextPanel straight from the checked value of enableFolderVariableRemoval.

The Visible Attribute for cleanTextPanel can then be calculated using a new Method and an inline if:

private void enableFolderVariableRemoval_CheckedChanged(object sender, EventArgs e)
{
    var enableFolderVariableRemoval = enableFolderVariableRemoval.Checked;

    cleanFolderTextPanel.Visible = enableFolderVariableRemoval;
    cleanTextPanel.Visible = CleanTextPanelShouldBeHidden(enableFolderVariableRemoval ) ? false : cleanTextPanel.Visible
}

...

private static void CleanTextPanelShouldBeHidden(bool enableFolderVariableRemoval)
{
     return !cleanFilenameTextPanel.Visible && !enableFolderVariableRemoval
}
share|improve this answer

I hope this is sufficient:

private void enableFolderVariableRemoval_CheckedChanged(object sender, EventArgs e)
{
    cleanFolderTextPanel.Visible = enableFolderVariableRemoval.Checked; 
    cleanTextPanel.Visible = enableFolderVariableRemoval.Checked;
}
share|improve this answer
You're missing the condition cleanFilenameTextPanel.Visible == false – Larry Battle Sep 28 '12 at 2:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.