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

Posted on 2011-10-21
Last Modified: 2012-08-14
Using 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?

Question by:WestcountryBusiness
    LVL 83

    Accepted Solution

    What happens if you programmatically move focus to another field?
    LVL 1

    Author Closing Comment

    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now