CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Nov 2000
    Location
    Calgary
    Posts
    2

    Form Loading Via Variable?? - HELP!!

    Am writing an application that uses a table to store all MENU Items in it, with descriptions, and a field with either a form name in it or a function name.

    The menuing program reads the data from the table and populates the menu form with buttons etc...

    The idea is simple. Once the button is selected the app will draw either the FORM NAME to load or a FUNCTION to execute.

    THE QUESTION......(s)

    1) How do I load a form with the information from the table. Have tried numerous things, but seem to have come up with a brain cramp. Tried this but it didn't work.

    DIM newform as form
    set newform = .fields(Procedurecall) <--- the var

    another e.g.
    dim newformstr as string
    newformstr = "LOADTHIS"
    dim aform as form
    set aform = ?????(newformstr)




    2) How do I execute a function in the same manner.

    e.g. Table says function to be called is 'CALLME'
    How do I execute this function.

    Any help would be appreciated




  2. #2
    Join Date
    May 1999
    Location
    Germany
    Posts
    106

    Re: Form Loading Via Variable?? - HELP!!

    Hi,

    loading a form via its name, i can't help you. Sorry.

    But i know how to call a function by its name. Please refer to the documentation of CallByName-function.

    Hope this helps
    Peter


  3. #3
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: Form Loading Via Variable?? - HELP!!

    Maybe VB.Net (VB7 ) will allow you to specify a variable containing the name of the form you want to load, but current VB (< 7) does not. You need to specify the name of the form directly like thus


    private Sub Command1_Click()

    Dim F as Form
    ' Create a copy of this form
    set F = new Form1
    F.Show
    ' or to show a second form
    Form2.Show

    End Sub





    John G

  4. #4
    Join Date
    Aug 2000
    Location
    KY
    Posts
    766

    Re: Form Loading Via Variable?? - HELP!!

    You can do this, but you have to think differently. A form is Nothing but an instance of a class. Add a property to your form class to store the formName make this a default property of the form. You then need to have a collection to store the forms. Add the FormName as the Key and the form as as item. You can work with the forms through your own collection.




  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Form Loading Via Variable?? - HELP!!

    VB doesn't offer any way to dynamically create forms by name, but you can do some clever stuff with class modules to replicate this functionality.

    Say you have some forms in your program (form1, form2, form3) which you'd like to create dynamically from a string. I'd recommend that you create a class module which is responsible for creating these forms, eg:


    '
    ' A class module called CFormFactory
    '
    option Explicit
    '
    public Function CreateForm(byval sFormName as string) as Form
    Dim fFrm as Form
    '
    Select Case UCase$(sFormName)
    Case "FORM1"
    set fFrm = new Form1
    '
    Case "FORM2"
    set fFrm = new Form2
    '
    Case "FORM3"
    set fFrm = new Form3
    '
    End Select
    '
    Load fFrm
    set CreateForm = fFrm
    '
    End Function




    You could then use this class from whereever you like :

    eg:


    Dim oFactory as CFormFactory
    '
    set oFactory = new CFormFactory
    '
    oFactory.CreateForm("form1").Show
    '






    Chris Eastwood

    CodeGuru - the website for developers
    http://codeguru.developer.com/vb

  6. #6
    Join Date
    Sep 1999
    Posts
    202

    Re: Form Loading Via Variable?? - HELP!!

    Use Add method of Forms collection:
    Forms.Add("Form1").Show

    option Explicit

    private Sub Form1_Click()
    Dim x as Form, strName as string
    strName = "Form1"

    set x = Forms.Add(strName)
    x.Show
    End Sub





  7. #7
    Join Date
    Sep 1999
    Posts
    202

    Re: Form Loading Via Variable?? - HELP!!

    I forgot to say:
    You'll need VB6 SP3 for Forms.Add to work. Without SP3 it won't work when compiled.


  8. #8
    Join Date
    Sep 1999
    Posts
    202

    Re: Form Loading Via Variable?? - HELP!!

    Here is a sample with mentioned CallByName function:
    option Explicit

    private Sub Form_Click()
    Dim x as Form

    ' set x = Forms.Add("form1")
    set x = CallByName(Forms, "Add", VbMethod, "Form1")

    ' x.Show
    CallByName x, "Show", VbMethod

    ' x.Left = me.Left + 100
    CallByName x, "Left", VbLet, me.Left + 100
    End Sub





  9. #9
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Wink This is something worth to remember...

    Have nice days, you BigGuru
    Cesare I.

    Ps: a note:
    Code:
    option Explicit
    
    Private Sub Form1_Click()
      Dim x as Form, strName as string
      strName = "Form1"
      'Before adding this form, be sure it is not already added
       For Each x in Forms
            If x.Name =strName Then
                 x.Show 'same form found, show it and exit sub
                 Exit Sub
            End If
       Next 
      set x = Forms.Add(strName) 'form not found, add it and show
      x.Show
    End Sub
    Last edited by Cimperiali; October 20th, 2003 at 02:54 AM.
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

  10. #10
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured