Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 310
  • Last Modified:

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?

1 Solution
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now