dcsimg
Results 1 to 12 of 12

Thread: Loading Multiple Forms

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Loading Multiple Forms

    I have the main Form called frmMotherShip and I want to load, let's say, 40 other Forms as an array. The names of the other Forms will be frmSlaveShip(0), frmSlaveShip(1),.........frmSlaveShip(39). Each of these other Forms will have the same code and each will have a different picture in a Picturebox. So, for example frmSlaveShip(0) will have the following code:

    Code:
    Private Sub Form_Load()
     Dim WindowRegion As Long
         
     SetTopmostWindow Me.hwnd, True
      
     picShip.Picture = LoadPicture(App.Path & "\Ship(0)") 
      
     WindowRegion = MakeRegion(picShip)
     SetWindowRgn Me.hwnd, WindowRegion, True
    End Sub
    
    Private Sub picShip_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
     If Button = vbLeftButton Then
     End If
    End Sub
    
    Private Sub picShip_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
     Static lx As Single, ly As Single
      
     If Button = vbLeftButton Then
       Left = Left + ScaleX((x - lx), picShip.ScaleMode, vbTwips)
       Top = Top + ScaleY((y - ly), picShip.ScaleMode, vbTwips)
     Else
       lx = x: ly = y
     End If
    End Sub
    
    Private Sub picShip_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
     If Button = vbLeftButton Then
     End If
    End Sub
    Now the only difference between each of these Forms will be this code line:
    Code:
     picShip.Picture = LoadPicture(App.Path & "\Ship(0)")
    where the name of the bitmap to load will be Ship(1), Ship(2),......Ship(39) for each Form in the array.

    Is it possible to load these Forms using code or do I have to make each one of them, load the correct picture and make sure the LoadPicture code line is correct

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,895

    Re: Loading Multiple Forms

    You can make one form and load copies of it and you should be able to do that as an array though I haven't tried the array part.

    You have to dim your var as a form then set the var to a new copy of the form.

  3. #3
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,895

    Re: Loading Multiple Forms

    For sake of example
    Code:
    Option Explicit
    Dim frmShip(50) As Form
    Private Sub Form_Load()
    Dim x As Integer
    For x = 0 To 50
        Set frmShip(x) = New Form2
        frmShip(x).Show
    Next
    End Sub

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    18,271

    Re: Loading Multiple Forms

    @DM, just fyi. Can be done without an array. The Forms.Collection can be used to enumerate them if needed
    Code:
    Dim x As Integer, frmShip As Form
    For x = 0 to 50
        Set frmShip = New Form2
        frmShip.Show
    Next
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    20,405

    Re: Loading Multiple Forms

    Typically you don't want the base interface though. Unless you plan to mix types As Form2 would normally make more sense.

  6. #6
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    382

    Re: Loading Multiple Forms

    The easiest for you is to add an extra property, like this:

    Code:
    ' frmSlaveShip
    Option Explicit
    
    Public strShipFile As String
    
    Private Sub Form_Load()
     picShip.Picture = LoadPicture(strShipFile) 
    End Sub
    From frmMotherShip, you use it like this(Code borrowed from DataMiser post):

    Code:
    ' frmMotherShip
    Option Explicit
    Dim frmSlaveShip(50) As frmSlaveShip
    Private Sub Form_Load()
    Dim x As Integer
    For x = 0 To 50
        Set frmSlaveShip(x) = New frmSlaveShip
        frmSlaveShip(x).strShipFile = App.Path & "\Ship(" & Trim(Str(x) & ").bmp"
        frmSlaveShip(x).Show
    Next
    End Sub
    There are other ways, like adding a Public Sub to frmSlaveShip, then call it. Example:

    Code:
    ' In frmSlaveShip
    Public Sub SetPicture(sFileName As String)
     picShip.Picture = LoadPicture(sFileName) 
    End Sub
    Here is how to call it from frmMotherShip:

    Code:
    frmSlaveShip(x).SetPicture App.Path & "\Ship(" & Trim(Str(x) & ").bmp"

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Re: Loading Multiple Forms

    OK, great. Now I have a question

    Instead of controlling the MouseDown, MouseMove, and MouseUp events in each Form can I control these events in the frmMotherShip

    Also now that I have the forms all loaded how do I address them? They don't respond to indexing as I get error.

    Code:
    Private Sub picShip_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
     Dim Index As Integer
     Static lx As Single, ly As Single
     
     If Button = vbLeftButton Then
       frmSlaveShip(0).Left = frmSlaveShip(0).Left + ScaleX((x - lx), picShip.ScaleMode, vbTwips)
       frmSlaveShip(0).Top = frmSlaveShip(0).Top + ScaleY((y - ly), picShip.ScaleMode, vbTwips)
     Else
      lx = x: ly = y
     End If
    
    End Sub
    I get error on Index values if it is anything except 0. So how do I address frmSlaveShip(1) etc
    Last edited by Code Dummy; Jul 4th, 2019 at 10:31 PM.

  8. #8
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    12,895

    Re: Loading Multiple Forms

    How did you load your forms?
    If you did it using the array method like I showed then you should not have a problem accessing them by index.

  9. #9
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    382

    Re: Loading Multiple Forms

    Forms don't work like control arrays. You use the variables that you declared. One example:

    Code:
    Dim ofrmSlaveShip(50) As frmSlaveShip
    
    Private Sub Form_Load()
    Dim x As Integer
    For x = 0 To 50
        Set ofrmSlaveShip(x) = New frmSlaveShip
        ofrmSlaveShip(x).strShipFile = App.Path & "\Ship(" & Trim(Str(x) & ").bmp"
        ofrmSlaveShip(x).Show
    Next
    End Sub

  10. #10
    Hyperactive Member
    Join Date
    Feb 2019
    Posts
    382

    Re: Loading Multiple Forms

    Quote Originally Posted by Code Dummy View Post
    OK, great. Now I have a question

    Instead of controlling the MouseDown, MouseMove, and MouseUp events in each Form can I control these events in the frmMotherShip
    You could add a function like this to frmMotherShip, and call it from frmSlaveShip:

    Code:
    Public Sub ForwardMouseDown(f As frmSlaveShip, Button As Integer, Shift As Integer, x As Single, y As Single)
        Debug.Print f.strShipFile
    End Sub
    You call it like this from frmSlaveShip:

    frmMotherShip.ForwardMouseDown Me, Button, Shift, x, y

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    550

    Re: Loading Multiple Forms

    I had problems using f As frmSlaveShip so I made a slight change:

    Code:
       '
       '
     Dim x As Integer
     For x = 0 To 50
       Set frmSlaveShip(x) = New frmSlaveShip
    
       frmSlaveShip.strPieceFile = App.Path & "\" & Trim(Str(x)) & ".bmp"
       frmSlaveShip(x).MyIndex = x                '<--- Added this
       frmSlaveShip.(x).Show
        '
        '  
     Next
    In frmSlaveShip Form_Load:

    Code:
    Option Explict
    
    Public strPieceFile As String
    Public MyIndex As Integer
    
    Private Sub Form_Load()
      '
      '
     picShip.Picture = LoadPicture(strPieceFile)
      '
      '
    End Sub
      '
      '

    Then in frmSlaveShip's picShip_MouseMove...
    Code:
    Private Sub picShip_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
     frmMotherShip.frmSlaveShip_MouseMove MyIndex, Button, Shift, x, y
    End Sub
    Then in frmMotherShip I get this

    Code:
      '
      '
    Public Sub frmSlaveShip_MouseMove(Index As Integer, Button As Integer, Shift As Integer, x As Single, y As Single)
     Dim dx As Single, dy As Single
    
     If Button = vbLeftButton Then
       dx = x - lx: dy = y - ly
       
       With frmSlaveShip(Index)
         .Move .Left + dx, .Top + dy: .ZOrder 0
       End With
     End If
    End Sub
      '
      '
    Works just like it was a regular control

  12. #12
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,986

    Re: Loading Multiple Forms

    you can Control from the 'MainForm' what happens in the other Forms

    here a sample passing Text to each Textbox in the created forms, change it for you Pic's

    create Form1 and Form2
    in Form2 just add a Textbox

    Code:
    ' all in Form1
    Private mcolForms As New Collection
    Private Const con_strFormName As String = "Ship "
    
    Private Sub Command1_Click()
        ' create your Forms
        Dim frmMyForm As Form2
        Dim lngIndex As Long
        
      Form1.Caption = "Mothership"
        
    For lngIndex = 1 To 5
         
        
        lngIndex = mcolForms.Count + 1
        Set frmMyForm = New Form2
         
        frmMyForm.Caption = con_strFormName & lngIndex
        mcolForms.Add frmMyForm, frmMyForm.Caption
        mcolForms(lngIndex).Show , Me
       
    Next
        Set Frm = Nothing
    End Sub
     
    Private Sub Command2_Click()
        ' pass something from the MainForm to the Textbox in each
        'created Form
        mcolForms(con_strFormName & "1").Text1.Text = "Welcome"
        mcolForms(con_strFormName & "2").Text1.Text = "Earthling"
        mcolForms(con_strFormName & "3").Text1.Text = "to"
        mcolForms(con_strFormName & "4").Text1.Text = "the"
        mcolForms(con_strFormName & "5").Text1.Text = "Mothership"
    
    End Sub
     
    Private Sub Form_Unload(Cancel As Integer)
        Dim Frm As Form
        
        For Each Frm In Forms
            Unload Frm
            Set Frm = Nothing
        Next
    
    End Sub
    Last edited by ChrisE; Jul 6th, 2019 at 01:17 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width