Error on Value Changed event.

  • Thread starter Thread starter lbsaltzman
  • Start date Start date
L

lbsaltzman

Guest
Here is a problem I ran into in vb .net in relation to some validation called from a value changed event. On a Windows form I have a NumericUpDown control and a Date time Picker control. The Date time picker is used to select the beginning date of an agreement and the NumericUpDown control to set the number of days the control is in force. I have code to recalculate the end date based on the values of these to fields. I want to call that code from value_changed and leave events of these two controls. The code works with the leave event and crashes badly with the value_changed event. The code also works with a variety of keypress and mouse events as well.

Here is the code for the problem. I left out the Try, Catch end try in this sample, but wheither that is present or not I am getting a stackoverflow error message and the error triggers when the form is loading. As I said this same validation works perfectly placed in almost any event.

Private Sub dtpStartDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpStartDate.ValueChanged

Call ChangeAgrmntEndDate()

End Sub

Private Sub ChangeAgrmntEndDate()
Dim DateReturn As Date
Dim StartDate As Date
Dim dblAgrmntLngth As Double
Dim strDateReturn As String

StartDate = Me.dtpStartDate.Value
dblAgrmntLngth = Me.updAgrmntLngth.Value
DateReturn = CalculateAgrmntDate(StartDate, dblAgrmntLngth)
strDateReturn = CStr(DateReturn)
Me.txtEndDate.Text = strDateReturn
End Sub

If anyone has had any experience yet with the vagaries errors during events that can explain this I will greatly appreciate some help.
 
Your code is run when something in the textbox changes, and the code changes the contents of the textbox. If you think about it, your code is going to recurse on itself until it runs out of stack space.

The usual way to fix this is with a variable that you can temporarily set to mark the code not to execute, when you change the contents programmatically.
 
Back
Top