ActiveForm question

Ok, I know that the ActiveForm object returns the, well, active form. But what about the active form's controls I can't access those because they don't appear as one of the ActiveForm's objects, and when I try to compile like that it says that the property doesn't exist.

So how can I access the controls of the active form

Please help!




Answer this question

ActiveForm question

  • madkurt

    But are you testing the type of the ActiveForm before you cast it You can only cast the ActiveForm as an RTFDoc if it is an RTFDoc. The whole point of the code I provided was to show that you can test the type of the ActiveForm to ensure that you are casting as the appropriate type. My code will only cast as Form1 if the ActiveForm is an instance of Form1. Same goes for Form2 and Form3.
  • nlitsme

    VBRocksLikeMad wrote:

    Oh!

    I was declaring it in general declarations, of course I didn't test!

    Dumb me.

    But does that mean I will have to check EVERY SINGLE TIME I USE IT !

    If you want to keep a reference to a specific form then you should do so when it is opened in the first place. If you're using the ActiveForm property then its type can be any type of form that you might open, so of course you have to test it every time you want to get a reference to the current ActiveForm. Once you have a reference to that form it doesn't matter whether the ActiveForm changes after that. You still have a reference to the same form you did originally.

  • oimtiaz

    Oh!

    I was declaring it in general declarations, of course I didn't test!

    Dumb me.

    But does that mean I will have to check EVERY SINGLE TIME I USE IT !



  • PK_Sean

    Thank you! I feel much better now!!



  • Yknev05

    Okay, I'm using the variable as a Public variable, and I'm only using one "type" of child form.

    Public activeRTF As RTFDoc = DirectCast(Me.ActiveForm, RTFDoc)



  • Stefan Keir Gordon

    VBRocksLikeMad wrote:
    Actually, no. When I use that method, it comes up with the "InvalidCastException" error. Little help here
    I don't see how that's possible if you used the code properly. Each case tests that the ActiveForm is a particular type and, if it is, declares a variable of that type and casts the ActiveForm as that type. My guess is that you've mixed up your types somewhere. If you can't identify the issue I suggest you post the code you're using.

  • VFI Support

    The ActiveForm property returns a Form reference because it can be any type of form.  You need to cast that reference to the appropriate type to be able to access the members of that type.  If the ActiveForm may be any of several types then you're going to need to test what type it is before casting, e.g.:

    Select Case True

        Case TypeOf Form.ActiveForm Is Form1

            Dim myForm1 As Form1 = DirectCast(Form.ActiveForm, Form1)

            'Use myForm1 here to access members of Form1 class.

        Case TypeOf Form.ActiveForm Is Form2

            Dim myForm2As Form2 = DirectCast(Form.ActiveForm, Form2)

            'Use myForm2 here to access members of Form2 class.

        Case TypeOf Form.ActiveForm Is Form3

            Dim myForm3 As Form3 = DirectCast(Form.ActiveForm, Form3)

            'Use myForm3 here to access members of Form3 class.

    End Select


  • latas

    Actually, no. When I use that method, it comes up with the "InvalidCastException" error. Little help here

  • ActiveForm question