Windows Forms Problem

Am I going mad

I have a form with a bindingNavigator bar and a bunch of text box controls. It appears that Click events on the bindingNavigator buttons fire before the control that loses focus fires the Leave and LostFocus events

Is this behaviour by accident or design or am I doing something wrong



Answer this question

Windows Forms Problem

  • susqu

    Unlikely....Code sample please!

  • joebrown

    Here are the relevent code fragments.  The scenario is that if the user amends the text in the LocationAddress1 text box I want "IsDirty" to be set to True.  However, if the user clicks on the bindingNavigatorMoveNextItem button then the bindingNavigatorMoveNextItem.Click event fires before the LocationAddress1TextBox.Leave event.  The behaviour occurs for all controls on the form.

    From the form:

    Private Sub bindingNavigatorMoveNextItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bindingNavigatorMoveNextItem.Click

       If IsDirtyRecordOK(Me.ClacksonsLocationsBindingSource, IsNewRecord, IsDirty) Then
          
    Exit Sub
       Else
          Me.ClacksonsLocationsBindingSource.MoveNext()
          Me
    .ClacksonsLocationsBindingSource.ResetBindings(False)
       End If
    End Sub

    Private Sub LocationAddress1TextBox_Leave(ByVal sender As Object, ByVal e As System.EventArgs) Handles LocationAddress1TextBox.Leave
       FormatProperString(LocationAddress1TextBox)
       
    If Me.LocationAddress1TextBox.Modified Then
          
    IsDirty = True
       
    End If
    End Sub

    From a separate module:


    Public
    Function IsDirtyRecordOK(ByVal InputBindingSource As Windows.Forms.BindingSource, ByVal IsNewRecord As Boolean, ByRef IsDirty As Boolean) As Boolean

       'This function checks what the user wants to do with a dirty record and returns true if
       'no further action is required (ie cancel changes)

       Dim MsgBoxReply As MsgBoxResult
       If Not IsDirty Then Return True

       MsgBoxReply = MsgBox(
    "There are outstanding changes to the record" & vbCrLf & "Do you want the opportunity to save the record ", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Record Changes Pending")
       If MsgBoxReply = MsgBoxResult.Yes Then
          Return False
       Else
          InputBindingSource.CancelEdit()
          IsDirty =
    False
          Return True
       End If
    End Function

    Thanks for your interest in my problem



  • CKdp

    The order of events posted is fro entering and leaving the same control...

    I would suggest moving your code to the validating event.



  • MSDevPartners

    FormatProperString is:

    Public Function FormatProperString(ByRef StringBox As TextBox) As Boolean
    'This function examines a text string and formats the string into '"proper" format if the string is all upper case or lower case. Mixed
    'case strings remain unchanged. The function returns true if the text
    'string has been reformated otherwise it returns false

       Dim InString As String
       Dim ix As Integer
       
    Dim UpperCase, LowerCase As Boolean

       InString = Trim(StringBox.Text)
       UpperCase =
    False
       LowerCase = False
       For ix = 0 To Len(InString) - 1
          If Char.IsLetter(InString, ix) Then
             LowerCase = LowerCase And Char.IsLower(InString, ix)
             UpperCase = UpperCase And
    Char.IsUpper(InString, ix)
          End If
          'Bale out if we have a mixed case string
          If UpperCase And LowerCase Then Return False
       Next
       StringBox.Text = StrConv(InString, VbStrConv.ProperCase)
       Return True
    End Function

    For the avoidance of doubt, can you can confirm that the event order is more explicitly:

    1. Enter (New Control)
    2. GotFocus (New Control)
    3. LostFocus (Old Control)
    4. Leave (Old Control)
    5. Validating (Old Control)
    6. Validated (Old Control)

    If I am right, then I would have to go back and programatically check to see if the data in the control had changed unless there is a simpler way to achieve the same aim

    Do all these event occur in the same thread or is there a way of identifying when the Leave event has been completed



  • Aaron West

    1.  What does FormatProperString Do...Is the leave event putting the focus back on the textbox using that method

    2. take a look here:ms-help://MS.VSCC.2003/MS.MSDNQTR.2005JAN.1033/cpref/html/frlrfSystemWindowsFormsControlClassLeaveTopic.htm

    Remarks

    When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl.ActiveControl property to the current form, focus events occur in the following order:

    1. Enter
    2. GotFocus
    3. Leave
    4. Validating
    5. Validated
    6. LostFocus

    When you change the focus by using the mouse or by calling the Focus method, focus events occur in the following order:

    1. Enter
    2. GotFocus
    3. LostFocus
    4. Leave
    5. Validating
    6. Validated

    If the CausesValidation property is set to false, the Validating and Validated events are suppressed.

    Note   The Enter and Leave events are suppressed by the Form class. The equivalent events in the Form class are the Activated and Deactivate events. The Enter and Leave events are hierarchical and will cascade up and down the parent chain until the appropriate control is reached. For example, assume you have a Form with two GroupBox controls, and each GroupBox control has one TextBox control. When the caret is moved from one TextBox to the other, the Leave event is raised for the TextBox and GroupBox, and the Enter event is raised for the other GroupBox and TextBox.

    For more information about handling events, see < XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" />Consuming Events.



  • Windows Forms Problem