Implement "form.dirty" feature on a VB .NET Form

  • Thread starter Thread starter Alain Filiatrault
  • Start date Start date
A

Alain Filiatrault

Hi,

I need to implement a "form.dirty" feature on a VB .NET form not
related to a database. I need to know if any of the items (Textboxes,
combo, listboxes, ...) was changed. Is there a quick and easy way to
do this?

Thanks in advance.

Alain
 
Alain,
I would add a handler for each controls "Changed" event to a single event
Handler that set a Dirty field.

Something like
Public Class MainForm

Private WithEvents TextBox1 As TextBox
Private WithEvents TextBox2 As TextBox
Private WithEvents ComboBox1 As ComboBox
Private WithEvents ListBox1 As ListBox

Private m_dirty As Boolean

Private Sub Form_Changed(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, _
ComboBox1.SelectedValueChanged, ComboBox1.TextChanged _
ListBox1.SelectValueChanged
m_dirty = True
End Sub

Private Sub TextBox1_Changed(ByVal sender As Object, _
ByVal e As EventArgs) Handles TextBox1.TextChanged
End Sub

End Class

Remember that a single event handler can handle multiple events, plus a
single event can be handled by multiple handlers.

An alternative method to the above is to set the Dirty flag in each of the
individual handlers for each control individually.

Hope this helps
Jay
 
Alain,
I would add a handler for each controls "Changed" event to a single event
Handler that set a Dirty field.

Something like
Public Class MainForm

Private WithEvents TextBox1 As TextBox
Private WithEvents TextBox2 As TextBox
Private WithEvents ComboBox1 As ComboBox
Private WithEvents ListBox1 As ListBox

Private m_dirty As Boolean

Private Sub Form_Changed(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, _
ComboBox1.SelectedValueChanged, ComboBox1.TextChanged _
ListBox1.SelectValueChanged
m_dirty = True
End Sub

Private Sub TextBox1_Changed(ByVal sender As Object, _
ByVal e As EventArgs) Handles TextBox1.TextChanged
End Sub

End Class

Remember that a single event handler can handle multiple events, plus a
single event can be handled by multiple handlers.

An alternative method to the above is to set the Dirty flag in each of the
individual handlers for each control individually.

Hope this helps
Jay


Jay, what is the order of the event handlers? Will the TextBox1_Changed
occur first? Or will the Form_Changed occur first? Correct me if I'm
wrong, but if you use AddHandler to wire up the events, the order the
handlers are called will be the order in which they were wired up (FIFO).
Is that correct? But what is the order when using the handles clause?

My follow up question is: If the value in the text box after any text is
typed is the same as the previous value, it may not be necessary to mark
the form as "Dirty". How can you cancel or prevent the Form_Changed
handler from being called? Or is it even possible?
 
Chris,
what is the order of the event handlers? Will the TextBox1_Changed
occur first? Or will the Form_Changed occur first? Correct me if I'm
Does it really matter what the order is? I really don't know what the order
is, nor do I really care.

If the order is important, I would not use WithEvents, I would use
AddHandler itself.
My follow up question is: If the value in the text box after any text is
typed is the same as the previous value, it may not be necessary to mark
the form as "Dirty". How can you cancel or prevent the Form_Changed
handler from being called? Or is it even possible?
Check the definition TextBox.TextChanged event, it is called for each
character typed, if you start with "Help", then type "Help" you will see the
changed event 4 times!

Remember the Changed event suggests that it already occurred, so you cannot
prevent it, you would need to stop the Changing event, however I do not see
that a cancelable TextBox.TextChanging event (you know like the Form.Closing
& Form.Closed events). You could possible do something with the keyboard
events...

Hope this helps
Jay


Chris Dunaway said:
Alain,
I would add a handler for each controls "Changed" event to a single event
Handler that set a Dirty field.

Something like
Public Class MainForm

Private WithEvents TextBox1 As TextBox
Private WithEvents TextBox2 As TextBox
Private WithEvents ComboBox1 As ComboBox
Private WithEvents ListBox1 As ListBox

Private m_dirty As Boolean

Private Sub Form_Changed(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles TextBox1.TextChanged, TextBox2.TextChanged, _
ComboBox1.SelectedValueChanged, ComboBox1.TextChanged _
ListBox1.SelectValueChanged
m_dirty = True
End Sub

Private Sub TextBox1_Changed(ByVal sender As Object, _
ByVal e As EventArgs) Handles TextBox1.TextChanged
End Sub

End Class

Remember that a single event handler can handle multiple events, plus a
single event can be handled by multiple handlers.

An alternative method to the above is to set the Dirty flag in each of the
individual handlers for each control individually.

Hope this helps
Jay


Jay, what is the order of the event handlers? Will the TextBox1_Changed
occur first? Or will the Form_Changed occur first? Correct me if I'm
wrong, but if you use AddHandler to wire up the events, the order the
handlers are called will be the order in which they were wired up (FIFO).
Is that correct? But what is the order when using the handles clause?

My follow up question is: If the value in the text box after any text is
typed is the same as the previous value, it may not be necessary to mark
the form as "Dirty". How can you cancel or prevent the Form_Changed
handler from being called? Or is it even possible?


--
Chris

To send me an E-mail, remove the "[", "]", underscores ,lunchmeat, and
replace certain words in my E-Mail address.
 
Back
Top