Control event handling question

B

Brian

Hello -

I have created a handler as follows that, when fired, will set a
boolean value to True/False:

AddHandler DirectCast(ctl, TextBox).TextChanged, AddressOf
ControlIsDirty

Private Sub ControlIsDirty(ByVal sender As System.Object, ByVal e As
System.EventArgs)
bolDirtyRecord = True

End Sub

I would like bolDirtryRecord to be set to True only when the user
changes the text in the text box. However, other changes to the text
box, such as moving to another record, are triggering this event. Is
there a way to check whether the the event was triggered by user input
in the text box?

Thanks.
Brian
 
C

Cor

Hi Brian,
I would like bolDirtryRecord to be set to True only when the user
changes the text in the text box. However, other changes to the text
box, such as moving to another record, are triggering this event. Is
there a way to check whether the the event was triggered by user input
in the text box?

Do you mean only when the textbox has focus?

Cor
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Brian) scripsit:
I have created a handler as follows that, when fired, will set a
boolean value to True/False:

AddHandler DirectCast(ctl, TextBox).TextChanged, AddressOf
ControlIsDirty

Private Sub ControlIsDirty(ByVal sender As System.Object, ByVal e As
System.EventArgs)
bolDirtyRecord = True

End Sub

I would like bolDirtryRecord to be set to True only when the user
changes the text in the text box. However, other changes to the text
box, such as moving to another record, are triggering this event. Is
there a way to check whether the the event was triggered by user input
in the text box?

You can add a 2nd flag that tells the 'ControlIsDirty' method that you
are setting the value through code.
 
F

Fergus Cooney

Hi Herfried,

There are a couple of problems with what I think you're suggesting.

Because it's an event handler (TextChanged).
No additional parameters are allowed.
It's not called directly from the code - it's a side effect of other
actions (as events usually are).

But a flag <is> the way to go, I believe - in conjunction with Cor's
suggestion.

Regards,
Fergus
 
F

Fergus Cooney

Hi Brian, Cor,

Cor's right.

If the User can only edit text when the TextBox has focus, then you have
the ideal 'frame' provided by TextBox.GotFocus and LostFocus. Set a flag to
True in the first, False in the latter. Then your ControlIsDirty handler can
use that flag.

Private tTheUserIsEditing As Boolean

GotFocus
tTheUserIsEditing = True

ControlIsDirty
bolDirtyRecord = tTheUserIsEditing

LostFocus
tTheUserIsEditing = False

Depending on what I want to do, my tendency is to detect <actual> changes
rather than potential changes. I save the contents before and then compare the
two at the end. Thus, if the User has made a change and then undone it, the
program doesn't erroneously think that a change has been made.

Private sOriginalText As String

GotFocus
sOriginalText = TextBox1.Text

ControlIsDirty
'No longer required.

LostFocus
bolDirtyRecord = (TextBox1.Text <> sOriginalText)
sOriginalText = ""

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
There are a couple of problems with what I think you're suggesting.

Because it's an event handler (TextChanged).
No additional parameters are allowed.

You can use a private variable.
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
And point two?

You can set the flag when changing the text programmatically and reset
it in the 'TextChanged' event handler if it is set.
 
F

Fergus Cooney

Hi Herfried,

Hmm, that means bracketing all instances where the text may change <other>
than by user intervention. Prone to error, misleading and overly complex I
would have thought. I'd say it's a bigger set than bracketing where the user
<does> change the text.

Regards,
Fergus
 
H

Herfried K. Wagner [MVP]

* "Fergus Cooney said:
Hmm, that means bracketing all instances where the text may change <other>
than by user intervention. Prone to error, misleading and overly complex I
would have thought. I'd say it's a bigger set than bracketing where the user
<does> change the text.

I think this depends on the situation. If there are, for example, only
3 lines of code changing the text, it's no problem to use this approach.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top