Click here to Skip to main content
14,882,311 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I'm trying to use the TextChanged event to initialize a second textbox. The second textbox also depends on the value of a third textbox value. These values change when changing the BindingSourcePosition.

Ex.
Private Sub StudentScheduleBindingSource_Position(ByVal sender As System.Object, _ 
            ByVal e As System.EventArgs) _
            Handles StudentScheduleBindingSource.PositionChanged
        
            'Some code.
End Sub

'The above event changes the textbox data and triggers the TextChanged event below

Private Sub TalkTextBox_TextChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) _
            Handles T1TextBox.TextChanged, T2TextBox.TextChanged, _
                    T3TextBox.TextChanged, T1aTextBox.TextChanged, _
                    T2aTextBox.TextChanged, T3aTextBox.TextChanged, _
                    T3bTextBox.TextChanged   
  Dim tb as TextBox = sender  'For this example assume T1TextBox is sender
  If tb.Text <> "" then
     id = FindInArray(studentNames, tb.Text) 'Find the student in the array
     cnsl = AllStudents(id, byCounsel) ' Get the counsel point they are working on
  End If

  ' This is where the problem starts
  ' tmsReview textbox stil has the old value in it and hasn't updated yet.
  ' schDateTextBox also has to old value (last week) and hasn't updated yet.
  ' Therefore T1Counsel.Text will not be correct.
  If tmsReview.Text = "Review" then 
       T1Counsel.Text = "R"
  Else
       T1Counsel.Text = cnsl
  End if

  'This code doesn't work either. Because it too is a week off.
  'Assume schDateTextBox is sender and has been added to Handles list
  If schDateTextBox.Text = reviewWeek then 
       T1Counsel.Text = "R"
  Else
       T1Counsel.Text = cnsl
  End if

End Sub

'Another attempt is to capture the Date change and compare it with the Review data
'This doesn't work because the tmsReviewTextBox still may not have updated yet _
'  and the T1CounselTextBox will still be wrong. When the T1TextBox.TextChanged _
'  is triggered it may overwrite the T1CounselTextBox
Private Sub SchDateTextBox_TextChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles SchDateTextBox.TextChanged
  If tmsReview.Text = "Review" then 
       T1Counsel.Text = "R"
  Else
       T1Counsel.Text = cnsl
  End if
 


'This is the only way I can get it to work.
'Create an array containing all ReviewDates and search for them _
'  each time the StudentScheduleBindingSource_Position event is triggered
Private Sub SchDateTextBox_TextChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles SchDateTextBox.TextChanged
    If FindInArray(tmsReviewDates, CDate(SchDateTextBox.Text)) >= 0 Then
       T1CounselTextBox.Text = "R"
    End If
End Sub


This is a little irritating to me because I can't rely on the order of updating fields on the form. Even using the TabOrder doesn't seem to make a difference on update order for the form. Or am I missing something.
Posted
Updated 1-Oct-12 15:38pm
v2

VB
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
       If TextBox1.Text = "Review" Then
           TextBox2.Text = ""
       Else
           TextBox3.Text = "Value"
       End If

   End Sub


This working fine, what is whole problem ?

Maybe you will try this ?

VB
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
       If TextBox1.Text = "Review" Then
           TextBox2.Text = ""
           TextBox3.Text = "Value"
       Else
           TextBox3.Text = ""
       End If

   End Sub
   
v2
Comments
Andrew Alix 30-Sep-12 19:36pm
   
I believe you misunderstood my problem. This code works fine in a form. But when the BindingNavigator changes position, it also triggers a .TextChanged event when the form updates to the new row. The problems is that, of course, not all textboxes update at the same time. And even the bindingSource.positionChanged event isn't triggered before other textboxes update.
This means that I cannot rely on the value of a textbox when changing another textbox.

For example, Textbox1 has a name. Textbox2 has a number (counsel point) which depends on Textbox1 having the correct name. TextBox1 and Textbox3 are updated when the BindingNavigator Position button is clicked on. Unforetunately Textbox 3 doesn't get updated before TextBox1 change event is triggered so it has "old" data in it and can't be relied on. If I use the TextChanged event on Textbox3, the data in Textbox1 may not have updated yet and is still "old".

Is this any clearer?
Do you try ?

Use the Clear method of the controls property. Call the method just before you start loading the new data.

.Clear

or

.Refresh - Forces the control to invalidate its client area and immediately redraw itself and any child controls.
   
Comments
Andrew Alix 13-Nov-12 20:50pm
   
Don't you just love how easy it is?
Do you may put here whole piece of code ?
   
Comments
Andrew Alix 1-Oct-12 21:38pm
   
Please see updated question with code example.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900