Vb.Net WinForms. How to save value entered in current field if form closed

Using vb.net in Visual Studio 2008.

I have a form with bound fields.  The datasource is a datatable (Releases) within a dataset (DS).

All is fine, but if I enter text into a field and then close the form without leaving the field, I lose what I've entered in that field.

I trap the form closing event and make the following tests:

CType(Me.BindingContext(DS.Releases), CurrencyManager).EndCurrentEdit()

But then if I test for changes, DS.Releases.Rows(0).RowState is Unchanged and DS.HasChanges is false.

The form is used for displaying a single record at a time and so the row being used is always row zero.  The same happens if the use selects the Save option from the menu.

Is there any way I can save the current contents of a field if the user does not move out of it before closing the form?

Who is Participating?
CodeCruiserConnect With a Mentor Commented:
What happens if you programmatically move focus to another field?
WestcountryBusinessAuthor Commented:
Thanks! I've been dopey this morning.  I've actually used that solution myself in other forms.  

For completeness, here's the solution I used (as sometimes I need to remain in the form and so can't lose focus from the current control)

Private Sub SaveChanges()

Dim ActiveControl As String = Me.ActiveControl.Name
' Move focus to any other field on the form. This one is a read only field so no chance the latest edit is in it

Code to perform save here ...

'Set focus back to original control
Me.Controls.Find(ActiveControl, True)(0).Focus()

end sub
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.