Solved

Detect Field Changes

Posted on 2006-11-12
11
204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

726 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