"dirty" flag on a form

  • Thread starter Thread starter aussie rules
  • Start date Start date
A

aussie rules

Hi,

I have some really, really large forms with dozens of text boxs.

What I want to do is somehow know that the user has edited a text box, so
that if they close the form, i can ask them if they would like to save there
changes.

Is there a simple way to do this, or do I have to have code in each text
boxes keypress(?) event ?

Thanks
 
Hi Aussie,

Why not just make a loop through all your texboxes before closing to see it
there is entered something.

Very easy to do in my opinion.

Or do I not understand you?

Cor
 
* "aussie rules said:
What I want to do is somehow know that the user has edited a text box, so
that if they close the form, i can ask them if they would like to save there
changes.

\\\
Private m_Dirty As Boolean
..
..
..
Public Sub TextBox_TextChanged( _
ByVal sender As Object, _
... _
) _
Handles Me.TextBox1.TextChanged, Me.TextBox2.TextChanged, ...
If DirectCast(sender, TextBox).TextLength > 0 Then
m_Dirty = True
End If
End Sub
..
..
..
If m_Dirty Then
...
Else
...
End If
///
 
I think maybe this is a good example of where an extender control might come
into play...
 
Hi all,

Thanks for the feedback.

I failed to mention that the text boxs may have been pre-populated when the
form loaded with current data. The length of therefore of a textbox will be

any other ideas
 
* "aussie rules said:
I failed to mention that the text boxs may have been pre-populated when the
form loaded with current data. The length of therefore of a textbox will be

You could store the standard text of each textbox in its 'Tag' property
too and then compare its content to the value of this property in order
to find out if the control is "dirty".
 
I am kind of curious about what he felt was more difficult about writing an
extender control as well.

Write the code once that attaches to all controls on a form and allows you
to check with one property if there was any changes made to any of the
controls on that form.

Very easy task? No, but you do all the hard work up front once and reap the
rewards of code reuse for all your latter projects.
 
What's difficult?!

See my first answer, in code that is a simple routine as this bellow for all
textboxes, and than you start telling to use the handlers as a reply on
that.

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
ctr.Tag = ctr.Text
End If
Next
End Sub

Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
If ctr.Tag.ToString <> ctr.Text Then
MessageBox.Show("do you want to save the items")
End If
End If
Next
End Sub
///
 
* "Cor Ligthert said:
See my first answer, in code that is a simple routine as this bellow for all
textboxes, and than you start telling to use the handlers as a reply on
that.

I like this solution, but it's too general for me. What if there is one
textbox I don't want to be checked ;-)?
 
* "Ray Cassick \(Home\) said:
I am kind of curious about what he felt was more difficult about writing an
extender control as well.

Write the code once that attaches to all controls on a form and allows you
to check with one property if there was any changes made to any of the
controls on that form.

Very easy task? No, but you do all the hard work up front once and reap the
rewards of code reuse for all your latter projects.

I think that's a good solution because it will clearly separate the
implementation of the extender from the form's controls.
 
Hi Herfried,
I like this solution, but it's too general for me. What if there is one
textbox I don't want to be checked ;-)?

When I real can help you to make that I will do that, however I think you
are smart enough for that to do it yourself. (You can however also use that
enum method I once showed you for this).
This was really the fastest.

Cor
 
Hi Cor,

* "Cor Ligthert said:
When I real can help you to make that I will do that, however I think you
are smart enough for that to do it yourself.

LOL!!!
 
If the goal is to learn whether any control has been changed from its
initial value, then:

1. Before you close the form, you could cycle thru all the controls that
could have changed and compare with the initially loaded values.
2. Or, you could set a Boolean dirty flag that gets set to True as soon as
anything is changed, Or changed from the initial value.

Which method is best depends on the specifics of the app.
 
Howard Kaikow said:
If the goal is to learn whether any control has been changed from its
initial value, then:

How about serializing the form on start, then after doing the same and
diffing them? You'd have to filter the 1/2 properties but otherwise should
work.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Make your ASP.NET applications run faster
http://www.atozed.com/IntraWeb/
 
Back
Top