Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Detect Field Changes

Posted on 2006-11-12
11
Medium Priority
?
209 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 140 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
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…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

636 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