Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open form after textbox data changed 2

Status
Not open for further replies.

Christineeve

Programmer
Feb 12, 2005
104
US
I've struggled with my textbox code. I am trying to only launch a form if the textbox data has actually changed.

Code:
  Private Sub txtLastName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLastName.TextChanged
        Try

            'If the last name has changed, collect the reason.
            frmReasonChange.ShowDialog()

        Catch ex As Exception

        End Try


    End Sub
This doesn't work because it launches the change form even when the box is first populated by the code initated when the form loads.

Other events such as lost or gotfocus trigger for the wrong reason.

Somehow I need to capture the original value, compare it with the new value and if different, launch the form.

Would anyone have a suggestion on how to do this?
 

To get around the code running when the textbox is first populated when the form is loaded, you can use a boolean that is global to the form. At the top of the Form_Load event set the boolean to True, then set it back to false at the end of the Form_Load event.

Code:
    'in the General/Declaration section of the code
    Dim bFormLoading As Boolean = False

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
        bFormLoading = True

        'code here

        bFormLoading = False
    End Sub

Then in the textbox's TextChanged event handler:

Code:
If Not bFormLoading Then
    'code here
End If


As to keeping the original value to compare with the changed value, you could put the original value in the textbox's Tag property. Then in the TextChanged event you can compare the current text to the original text (in the Tag) and take appropriate action. Just remember to set the Tag to the new value once the change has been made.



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you again for your help. I will give this code a whirl. It looks like it makes perfect sense, of course. :)
 

You just have to remember that [tt]TextChanged[/tt] Event fires every time well... the test changes.

So if you type: Christineeve, [tt]TextChanged[/tt] fires at every (let's say: *) - [tt] C*h*r*i*s*t*i*n*e*e*v*e [/tt] So you need to know when you are done typing to compare your new text with old value, like in LostFocus event of your text box.

Have fun.

---- Andy
 
Yep, you're right. I was realizing that as I was studying the code. But, I think lostfocus might work?
 
Depending on your usuage, you may also was to consider an empty text box. You may or may not want the form to fire up if there was no text - for example if the first letter entered was wrong and the backspace was pressed.

Code:
If txtLastName.trim <> String.Empty then
   frmReasonChange.ShowDialog()
endif

If at first you don't succeed, then sky diving wasn't meant for you!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top