By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,553 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

"dirty" flag on a form

P: n/a
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

Nov 20 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
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
Nov 20 '05 #2

P: n/a
* "aussie rules" <au***@home.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #3

P: n/a
I think maybe this is a good example of where an extender control might come
into play...
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:eZ**************@TK2MSFTNGP12.phx.gbl...
* "aussie rules" <au***@home.com> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #4

P: n/a
Hi Ray,

No an example how to do simple things in a difficult way.

Cor
\\\
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
///

Nov 20 '05 #5

P: n/a
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
1.
any other ideas

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ex*************@TK2MSFTNGP11.phx.gbl... Hi Ray,

No an example how to do simple things in a difficult way.

Cor
\\\
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
///


Nov 20 '05 #6

P: n/a
* "aussie rules" <au***@home.com> scripsit:
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
1.


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".

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #7

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
No an example how to do simple things in a difficult way.


What's difficult?!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #8

P: n/a
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.

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
* "Cor Ligthert" <no**********@planet.nl> scripsit:
No an example how to do simple things in a difficult way.


What's difficult?!

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #9

P: n/a
>
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
///
Nov 20 '05 #10

P: n/a
* "Ray Cassick \(Home\)" <rc************@enterprocity.com> scripsit:
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.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #11

P: n/a
* "Cor Ligthert" <no**********@planet.nl> scripsit:
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.


I like this solution, but it's too general for me. What if there is one
textbox I don't want to be checked ;-)?

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #12

P: n/a
Hi Herfried,

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 ;-)?


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
Nov 20 '05 #13

P: n/a
Hi Cor,

* "Cor Ligthert" <no**********@planet.nl> scripsit:
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 ;-)?


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

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #14

P: n/a
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.

--
http://www.standards.com/; See Howard Kaikow's web site.
"aussie rules" <au***@home.com> wrote in message
news:Oh**************@TK2MSFTNGP09.phx.gbl...
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
1.


any other ideas

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ex*************@TK2MSFTNGP11.phx.gbl...
Hi Ray,

No an example how to do simple things in a difficult way.

Cor
> \\\
> 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
> ///
>



Nov 20 '05 #15

P: n/a
"Howard Kaikow" <ka****@standards.com> wrote in news:O50HYPMQEHA.2348
@TK2MSFTNGP10.phx.gbl:
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/
Nov 20 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.