Windows Develop Bookmark and Share   
 index > Windows Forms General > checkbox set values not getting persisted
 

checkbox set values not getting persisted

hi all,

i have a situation where my form uses a binding source and i put in a default value in a textbox when a checkbox is clicked. when the checkbox is clicked and the default value is populated through the cb clicked event, the value is not being persisted..however, if the textbox gets focus and is typed in, the value is persisted. what am i missing(focus or something) that the checkbox event needs to persist the default value? thanks - j
jp
jpdev4  Wednesday, July 15, 2009 10:01 AM

Hi jpdev4,

We need to call the EndEdit method of the BindingSource after we change the text of the TextBox programmatically. This is the modified code:

private void cbDefaultEmail_CheckedChanged(object sender, EventArgs e)

{

if (cbDefaultEmail.Checked)

{

emailTextBox.Text = "defaultEmail@address.net";

}

else

{

emailTextBox.Text = string.Empty;

}

//Notify the binding source that it needs to be updated

attorneyBindingSource.EndEdit();

}

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 17, 2009 3:48 AM
can you show the code?
Asgar
_asgar  Wednesday, July 15, 2009 10:22 AM
Try BindingSource.ResetBindings() after the clicked event is triggered.
  • Proposed As Answer byKalaz.Net Wednesday, July 15, 2009 10:45 AM
  •  
Kalaz.Net  Wednesday, July 15, 2009 10:45 AM
here is some code:

this is the handler for the checkbox being checked
private void cbDefaultEmail_CheckedChanged(object sender, EventArgs e)
        {
            if (cbDefaultEmail.Checked)
                emailTextBox.Text = "defaultEmail@address.net";
            else
                emailTextBox.Text = string.Empty;
        }

i tried the reset bindings here after setting the textbox value and that caused it to reset the email back to the currently saved value..

also here is the code where i save the value:

 private void Save()
        {
            ForceFocus();
/*i notice when calling the binding source current, it has set the email properly when the textbox has been entered via typing a value, but then checkbox event sets the value call current on the binding source does not shows the old email value*/
            Attorney a = (Attorney)attorneyBindingSource.Current;

            a.LastUpdatedBy = Program.user.UserName;
            a.RowVersion++;
            if (a.AttorneyId == "")
                a.AttorneyId = ServerProxy.SaveAttorney(a).ToString();
            else
                ServerProxy.SaveAttorney(a);

            attorneyBindingSource.ResetCurrentItem();
        }

jp
jpdev4  Thursday, July 16, 2009 9:53 AM
it seems as though it has something to do with focus perhaps? since giving the textbox focus when selecting and entering text works but the checkbox checked event adding the text does not?
jp
jpdev4  Thursday, July 16, 2009 10:02 AM

Hi jpdev4,

We need to call the EndEdit method of the BindingSource after we change the text of the TextBox programmatically. This is the modified code:

private void cbDefaultEmail_CheckedChanged(object sender, EventArgs e)

{

if (cbDefaultEmail.Checked)

{

emailTextBox.Text = "defaultEmail@address.net";

}

else

{

emailTextBox.Text = string.Empty;

}

//Notify the binding source that it needs to be updated

attorneyBindingSource.EndEdit();

}

Let me know if this helps.
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Friday, July 17, 2009 3:48 AM

Hi jpdev4,

Please let me know if my solution works for you. If not, I would find another solution for you. Thanks.

Regards,
Aland Li


Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
Aland Li  Tuesday, July 21, 2009 7:15 AM

You can use google to search for other answers

Custom Search

More Threads

• changing language font
• How to determine if node exists
• A good way to load a bunch of hardcored variables
• Issue w/ Property Grid control and unhandled exceptions
• Problem with TreeView
• How to Disable DoubleClick and Plus keys
• datime picker control
• How to make controls appearance independed from windows theme
• how make composition from controls ?
• Database across internet question