Bohn
Bohn

Reputation: 26919

Enable/Disable the Save button correctly

Let's say we have a Win32 form with a Save toolbar button and some sontrols like a CheckBox are on the form, now we write a one line code for onchange event of checkbox to enable/disable the Save button. Let's say checkbox is selected at first, Save button disabled, now de-select the checkbox, Save button becomes enabled...now select the checkbox again Save button is still enabled...Same for a TextBox for example. Let's say its text is "Hi"...change it to "Hi Bye" , Save is enabled...change it BACK to "Hi" as it was, Save remains enabled... Is there a model we can use to prevent these wrong enabling/disabling of save button?

Upvotes: 0

Views: 3883

Answers (4)

Shyju
Shyju

Reputation: 218722

You need to write some IF - ELSE code in the CheckedChanged event of the Checkbox. Check what is the current state by inspecting the Checked proeprty of the control (checkbox) ,If yes set the Enabled proeprty of the Button to true, else false.

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox2.Checked)
        button1.Enabled = true;
    else
        button1.Enabled = false;
}

Assuming checkBox2 is the name of the Checkbox and button1 is the name of the Save button.

You can use the same IF ELSE logic for other controls also. To Set the Value of the Textbox, Use the Text property

 TextBox1.Text="I am gonna try something now"l

EDIT : As comecme suggested, If you only want to enable/disable button based on the checbox, It can be done in one line instead of the IF else block like this

button1.Enabled=checkBox2.Checked

Upvotes: 2

Polyfun
Polyfun

Reputation: 9639

I prefer to put all my control state checking and setting into a single method:

private void UpdateControls()
{
    saveButton.Enabled = checkBox1.Checked;
    otherButton.Visible = checkBox2.Checked && textBox.Text.Length > 0;
}

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    UpdateControls();
}

private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
    UpdateControls();
}

private void textBox_TextChanged(object sender, EventArgs e)
{
    UpdateControls();
}

This means you just have one place in which to check and set the state, and makes it much easier to understand what is going on, especially when you have complex forms. I also prefer boolean expressions rather than if statements when assigning boolean variables, because it caters for both true and false without having to write a separate else statement (which may be forgotten).

Upvotes: 1

CodeCaster
CodeCaster

Reputation: 151586

I don't get where you're going with your checkbox, but I would use a Boolean variable:

private Boolean _canSave = false;
private Boolean CanSave 
{
    get { return _canSave; }
    set
    {
        _canSave = value;
        MenuSave.Enabled = value;
    }
}

public void MenuSave_Click()
{
    Save();
}

private void Save()
{
    // do your thing

    CanSave = false;
}

public void TextBox_TextChanged()
{
    CanSave = true;
}

This won't account for disabling the saving menu when you revert the text back to its original. If you want that, you'll have to store the text in the Save() method in a private variable, and compare that to the current text on every TextBox_TextChanged() to determine whether a change compared to the original (i.e. since last save) has occurred.

Upvotes: 0

You could store the last saved state, and compare the current state to it whenever it changes, to see if they're identical. If so, disable the button.

If these comparisons are expensive, you could make this more efficient, by calculating a hash value over all of the fields that need to be saved, and only doing the proper comparison if the hash of the last saved state matches the hash of the current state.

Upvotes: 1

Related Questions