Any shortcut to passing a form as an argument?

Any shortcut to passing a form as an argument

I was hoping that vs 2005 would ease the problem of passing a form as an argument over vs 2003.  Looks like not.  It is very cumbersome to overload a procedure for each form, so much so that there's no reason to do it.



sub XXX(frm as New Form1)

Sub XXX(frm as New Form2)

 

etc.

It's easier to repeat the code within the form.  Is there a workaround

dennist



Answer this question

Any shortcut to passing a form as an argument?

  • dnyanesh

    Thanks Joe,

    I just tried it.  There was a wiggly line under New, which said New is not valid in this context.

    dennist


  • Anonymous280z

    Adam,

    Oi, ve voi.  Thank you very much. 

    I was hoping studio 2005 would have had a shortcut.  Looks like it doesn't.

    Thanks again.

    dennist

  • Larnyjeaney

    That's because its not. Remove the New keyword, its not needed.

  • DamReev

    You will need to cast the form to a specific form object to get intellisense to show the specific form's members. Take a look at the code below



        Public Shared Sub MyMethod(ByRef myForm As Form)
            Dim tempForm As Form = Nothing
            If (myForm.GetType().Name = "Form1") Then
                MessageBox.Show("Form1")
                tempForm = New Form1()
                tempForm.ShowDialog()
            ElseIf (myForm.GetType().Name = "Form2") Then
                MessageBox.Show("Form2")
                tempForm = New Form2()
                tempForm.ShowDialog()
            ElseIf (myForm.GetType().Name = "Form3") Then
                MessageBox.Show("Form3")
                tempForm = New Form3()
                tempForm.ShowDialog()
            Else
                MessageBox.Show("Invalid Form")
            End If
        End Sub

     


    You can use this method with the following call:



    MyMethod(New Form3)

     


  • Simon Francis

    I don't want to pass it to a form.  I want to pass it to a class procedure, to process a bunch of code rather than having to process it in each form separately.

    dennist

  • Bluebell

    Nope, that's the first thing I tried.  Just tried again.  Intellisense doesn't show any of the specific form's members.  I just tried it again. If I type one in it produces an error.

    dennist


  • babusr

    Why not a single procedure with a Form argument:



    Sub XXX(frm as new Form)

     


    Joe

  • Karel Deman

    Yes that's by design. You are referencing the form by its base Form. So you will only be able to see members of System.Windows.Forms.Form.

    I'm a little confused, why to you want to pass so many Form instances to this one form

  • Any shortcut to passing a form as an argument?