Solved

Detect Field Changes

Posted on 2006-11-12
11
202 Views
Last Modified: 2012-05-05
Sorry about the points, my last ones.. Will add more points soon.

Hi!

I need something that detects field changes in a form, so I know when to save the data I have in my fields.

Is there a better way to use,
SelectedIndexChanged
TextChanged
 
0
Comment
Question by:NissePPP
  • 4
  • 4
  • 3
11 Comments
 
LVL 10

Expert Comment

by:Kinger247
ID: 17925156
I would do the same as you ... I'd set a flag in each textbox, combo etc .. on the change event.
So when closing you can check for un-saved amendents and warn the user.

I've never seen it done in one final statement without setting a flag for each control.
Then again I might be wrong.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17925168
Nope, I agree.  If you don't care WHICH fields were changed (i.e. you just want to know whether or not there have been changes since opening the form), you can get away with a single event handler (for all the .TextChanged, .SelectedIndexChanged (et cetera) events) and a single Private _dirty As Boolean variable, but you're still ultimately handling every event, you're just combining the code a little that way.
0
 
LVL 1

Author Comment

by:NissePPP
ID: 17925182
ok.. sound good, But can you give me an example on this.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 10

Expert Comment

by:Kinger247
ID: 17925201
Something like :

Public Class Form1
    Private blnDataChanged As Boolean = False

    Private Sub DataChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged, TextBox1.TextChanged
        blnDataChanged = True
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If blnDataChanged Then
            MessageBox.Show("Data Changed")
            e.Cancel = True
        End If
    End Sub
End Class
0
 
LVL 10

Expert Comment

by:Kinger247
ID: 17925209
But of course you would then get a response from the dialog to save the data, on 'yes' you would then save and close (in the example).
0
 
LVL 1

Author Comment

by:NissePPP
ID: 17925215
Thx, but how about a single event handler for all controls.
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17925225
That's exactly what that is.  The DataChanged event handler takes care of both ComboBox1.SelectedIndexChanged and TextBox1.TextChanged -- expand that to as many events as you like.
0
 
LVL 10

Accepted Solution

by:
Kinger247 earned 35 total points
ID: 17925229
You could so something like this :

Public Class Form1
    Private blnDataChanged As Boolean = False

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If blnDataChanged Then
            MessageBox.Show("Data Changed")
            e.Cancel = True
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For Each control As Control In Me.Controls
            AddHandler control.TextChanged, AddressOf Datachanged
        Next
    End Sub

    Private Sub Datachanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        blnDataChanged = True
    End Sub
End Class

NOTE: But it won't work for ALL control types.
0
 
LVL 1

Author Comment

by:NissePPP
ID: 17925233
But is there a way to skip writning all the control names..
0
 
LVL 1

Author Comment

by:NissePPP
ID: 17925238
oki.. thx... Great!!!
0
 
LVL 19

Expert Comment

by:VoteyDisciple
ID: 17925291
Yeah, that has the disadvantage of only handling TextChanged events.  You might want to try something like:

If TypeOf control Is TextBox
    AddHandler CType(control, TextBox).TextChanged, AddressOf DataChanged
ElseIf TypeOf control Is ComboBox
    AddHandler CType(control, ComboBox).SelectedIndexChanged, AddressOf DataChanged
' ... et cetera
EndIf
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

785 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