Solved

Detect Field Changes

Posted on 2006-11-12
11
200 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
Comment Utility
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
Comment Utility
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
Comment Utility
ok.. sound good, But can you give me an example on this.
0
 
LVL 10

Expert Comment

by:Kinger247
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Author Comment

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

Expert Comment

by:VoteyDisciple
Comment Utility
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
Comment Utility
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
Comment Utility
But is there a way to skip writning all the control names..
0
 
LVL 1

Author Comment

by:NissePPP
Comment Utility
oki.. thx... Great!!!
0
 
LVL 19

Expert Comment

by:VoteyDisciple
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

18 Experts available now in Live!

Get 1:1 Help Now