Mdi Form Validating

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • easoftware@gmail.com

    #1

    Mdi Form Validating

    I am using VS .Net 2003 and VB. I have an app with one parent and two
    Mdi child forms. I need to validate data in the Mdi form. The
    Form.Validating event works when I try to close a Mdi form, but not
    when I try to switch form one Mdi form to the other. I tried to add
    code to MdiForm1's Deactivate event:

    Private Sub MidForm1_Deacti vate(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles MyBase.Deactiva te

    Dim TempE As System.Componen tModel.CancelEv entArgs
    TempE.Cancel = False
    MdiForm1_Valida ting(sender, TempE)
    If TempE.Cancel Then
    MdiForm1_Activa ted(sender, e)
    End If
    End Sub

    but when MdiForm2 tries to load, the program crashes:

    An unhandled exception of type 'System.OutOfMe moryException' occurred
    in system.windows. forms.dll

    Additional information: Error creating window handle.

    How can I keep the focus in MdiForm1 when the user wants to change
    forms and the data in MdiForm1 is not valid?

    Thanks
    Eric

  • tomb

    #2
    Re: Mdi Form Validating

    I am guessing you mean when you switch from one child form to the
    other. Why don't you try validating in the OnLostFocus override event?
    Then you can regain focus.

    T

    easoftware@gmai l.com wrote:
    >I am using VS .Net 2003 and VB. I have an app with one parent and two
    >Mdi child forms. I need to validate data in the Mdi form. The
    >Form.Validatin g event works when I try to close a Mdi form, but not
    >when I try to switch form one Mdi form to the other. I tried to add
    >code to MdiForm1's Deactivate event:
    >
    Private Sub MidForm1_Deacti vate(ByVal sender As Object, ByVal e As
    >System.EventAr gs) Handles MyBase.Deactiva te
    >
    Dim TempE As System.Componen tModel.CancelEv entArgs
    TempE.Cancel = False
    MdiForm1_Valida ting(sender, TempE)
    If TempE.Cancel Then
    MdiForm1_Activa ted(sender, e)
    End If
    End Sub
    >
    >but when MdiForm2 tries to load, the program crashes:
    >
    >An unhandled exception of type 'System.OutOfMe moryException' occurred
    >in system.windows. forms.dll
    >
    >Additional information: Error creating window handle.
    >
    >How can I keep the focus in MdiForm1 when the user wants to change
    >forms and the data in MdiForm1 is not valid?
    >
    >Thanks
    >Eric
    >
    >
    >

    Comment

    • easoftware@gmail.com

      #3
      Re: Mdi Form Validating

      I would like the validating to test before the other MDI form gets
      focus. Per VB documentation, the event order is:

      1) Enter (Activated for Forms)
      2) Got Focus
      3) Leave (Deactivated for Forms)
      4) Validating
      5) Validated
      6) Lost Focus

      so, if the validating is in Lost Focus, the other form gets displayed
      before the not valid error message can be shown. I would like to keep
      the user in the form that is being validated.

      tomb wrote:
      I am guessing you mean when you switch from one child form to the
      other. Why don't you try validating in the OnLostFocus override event?
      Then you can regain focus.
      >
      T
      >
      easoftware@gmai l.com wrote:
      >
      I am using VS .Net 2003 and VB. I have an app with one parent and two
      Mdi child forms. I need to validate data in the Mdi form. The
      Form.Validating event works when I try to close a Mdi form, but not
      when I try to switch form one Mdi form to the other. I tried to add
      code to MdiForm1's Deactivate event:

      Private Sub MidForm1_Deacti vate(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles MyBase.Deactiva te

      Dim TempE As System.Componen tModel.CancelEv entArgs
      TempE.Cancel = False
      MdiForm1_Valida ting(sender, TempE)
      If TempE.Cancel Then
      MdiForm1_Activa ted(sender, e)
      End If
      End Sub

      but when MdiForm2 tries to load, the program crashes:

      An unhandled exception of type 'System.OutOfMe moryException' occurred
      in system.windows. forms.dll

      Additional information: Error creating window handle.

      How can I keep the focus in MdiForm1 when the user wants to change
      forms and the data in MdiForm1 is not valid?

      Thanks
      Eric

      Comment

      • Barry

        #4
        Re: Mdi Form Validating

        Hi,

        Maybe you could check that ChildForm1 is valid in the ChildForm2
        GotFocus. If it is valid then no problems - if it isn't switch back to
        ChildForm1?

        I haven't looked in to this properly - just an idea....

        HTH

        Barry

        Comment

        • easoftware@gmail.com

          #5
          Re: Mdi Form Validating

          Thanks for the help, but the problem with this approach is the number
          of forms. In my example, there are only two, but my app will have
          more. SO... I would have to check if Form#x is open, and it it is, is
          the user editing, and if editing then validate. I would need to have
          this code for every MDI form that could be open and editing data. I
          would also have to do this in each form's GotFocus event. It would be
          much cleaner to do the validation in the form with the data being
          edited.

          Barry wrote:
          Hi,
          >
          Maybe you could check that ChildForm1 is valid in the ChildForm2
          GotFocus. If it is valid then no problems - if it isn't switch back to
          ChildForm1?
          >
          I haven't looked in to this properly - just an idea....
          >
          HTH
          >
          Barry

          Comment

          Working...