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
Solved

Detect Field Changes

Posted on 2006-11-12
11
203 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

840 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