Results 1 to 26 of 26

Thread: Using a var in the name of a new form..

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148

    Using a var in the name of a new form..

    When using Dim whatever as New Form1 is there any way to use a variable instead of 'whatever'.

    An example would be "duplicating" Form1 with names like newfrm1, newfrm2, etc.

    How would I do this? Im sure its really easy but as always I cant figure it out .

  2. #2
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    ké?
    -= a peet post =-

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Originally posted by peet
    ké?
    .........?

  4. #4
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    no it's not easy because it's not possible. Variable names aren't dynamic.

  5. #5
    I guess it means "What?"

    Why can't you just say "Dim NewForm1 As New Form1"?

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Originally posted by filburt1
    I guess it means "What?"

    Why can't you just say "Dim NewForm1 As New Form1"?
    Every time it calls the function it needs to add another form with a different name so I can access it later on...

    Why doesnt the form just have an Index property to make this alot easyer??

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    make a collection of forms, that might be what u want?
    -= a peet post =-

  8. #8
    hellswraith
    Guest
    I am not sure you can do that. You are early binding to an object when you do that. This means all references and such are figured out when it is compiled. Therefore, you can't change the name during runtime.

    If you use the Load function, you should be able to load new forms into memory for a "form array". You would reference them as such, form(2).Caption = "newform2"

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    I was under the impression you couldnt do a form array....

    How would I make a collection?

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    using a collection, u get control of u'r forms.
    U cant change the names though...

    VB Code:
    1. Option Explicit
    2. Private frmDet As frmDetail
    3. Private fFrmDetCol As New Collection
    4.  
    5. Private Sub Command1_Click()
    6.     Set frmDet = New frmDetail
    7.     frmDet.Caption = "Form detail no " & fFrmDetCol.Count + 1
    8.     frmDet.Tag = fFrmDetCol.Count + 1
    9.     fFrmDetCol.Add frmDet, frmDet.Tag
    10.     frmDet.Show
    11. End Sub
    12.  
    13. Private Sub Command2_Click()
    14.     Dim i As Integer
    15.     For i = 1 To fFrmDetCol.Count
    16.         MsgBox fFrmDetCol(i).Caption
    17.         fFrmDetCol(i).SetFocus
    18.     Next i
    19. End Sub
    -= a peet post =-

  11. #11
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    You asked this question before, and the last post tells you how to make an array http://www.vbforums.com/showthread.p...light=frm.Show

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Originally posted by chrisjk
    You asked this question before, and the past post tells you how to make an array http://www.vbforums.com/showthread.p...light=frm.Show
    ok well last time I checked that post it wasnt there...heh...nm i guess....

    *feels really dumb*

  13. #13
    hellswraith
    Guest
    It's not really called a form array, it's called a collection. But you can refer to the different forms in the collection with indexes. The collection contains all forms loaded in memory.

    From the MSDN library:

    Form Object, Forms Collection

    A Form object is a window or dialog box that makes up part of an application's user interface.

    A Forms collection is a collection whose elements represent each loaded form in an application. The collection includes the application's MDI form, MDI child forms, and non-MDI forms. The Forms collection has a single property, Count, that specifies the number of elements in the collection.

    Syntax

    Form

    Forms(index)

    The placeholder index represents an integer with a range from 0 to Forms.Count - 1.

    Remarks

    You can use the Forms collection to iterate through all loaded forms in an application. It identifies an intrinsic global variable named Forms. You can pass Forms(index) to a function. whose argument is specified as a Forms class.

    Forms have properties that determine aspects of their appearance, such as position, size, and color; and aspects of their behavior, such as whether or not they are resizable.

    Forms can also respond to events initiated by a user or triggered by the system. For example, you could write code in a form's Click event procedure that would enable the user to change the color of a form by clicking it.

    In addition to properties and events, you can use methods to manipulate forms using code. For example, you can use the Move method to change a form's location and size.

    A special kind of form, the MDI form, can contain other forms called MDI child forms. An MDI form is created with the MDI Form command on the Insert menu; an MDI child form is created by choosing New Form from the File menu and then setting the MDIChild property to True.

    You can create multiple instances of forms in code by using the New keyword in Dim, Set, and Static statements.

    When designing forms, set the BorderStyle property to define a form's border, and set the Caption property to put text in the title bar. In code, you can use the Hide and Show methods to make forms invisible or visible at run time.

    Note Setting BorderStyle to 0 removes the border. If you want your form to have a border without the title bar, Control-menu box, Maximize button, and Minimize button, delete any text from the form's Caption property, and set the form's ControlBox, MaxButton, and MinButton properties to False.

    Form is an Object data type. You can declare variables as type Form before setting them to an instance of a type of form that was declared at design time. Similarly, you can pass an argument to a procedure as type Form.

    Forms also can act as sources in a DDEconversation, with a Label, PictureBox, or TextBox control furnishing the data.

    You can access the collection of controls on a Form using the Controls collection. For example, to hide all the controls on an Form you can use code similar to the following:

    For Each Control in Form1.Controls
    Control.Visible = False
    Next Control

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    uuhh.....engligh translation please??

    Would there be an advantage to using peet's code instead of
    VB Code:
    1. i = UBound(MyForms) + 1
    2. ReDim Preserve MyForms(i)
    3. Set MyForms(i) = New Form1
    ?

  15. #15
    hellswraith
    Guest
    The reason to use Peets is because he is putting them in a new collection. If you rely on the standard forms collection, you get all forms that are loaded in memory. You might get forms you don't want to mess with.

    added later: Didn't see what you where doing, you left out the declaration of the array. You can do that. All it is is an array of objects...
    Sorry.

  16. #16
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    It's a lot less code...

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Type mismatch in i = UBound(myforms) + 1

  18. #18
    PowerPoster
    Join Date
    Jul 1999
    Posts
    5,923
    Is MyForms declared as a form array?

  19. #19
    hellswraith
    Guest
    Dim MyForms() As Form1

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    Originally posted by hellswraith
    Dim MyForms() As Form1
    o heh. thanks everyone for all ur help!

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148

    Another problem! (last one I hope!)

    Here is the entire function:
    VB Code:
    1. Public Function DoMsg(SenderNick, Msg)
    2. Dim MsgWindow() As frmMsg, i, User(), x
    3. i = UBound(MsgWindow) + 1 'Subscript Out Of Range?!
    4. ReDim Preserve MsgWindow(i)
    5. Set MsgWindow(i) = New frmMsg
    6. For x = 0 To UBound(MsgWindow) 'Object Variable or with block not set?!
    7. If MsgWindow(x).Caption = SenderNick Then
    8.     MsgWindow(x).txtRecieve.text = MsgWindow(x).txtRecieve.text & vbCrLf & "< & sendernick & " > " & msg"
    9.     Exit Function
    10. Else
    11.     MsgWindow(x).Show
    12.     MsgWindow(x).Caption = SenderNick
    13.     MsgWindow(x).txtRecieve.text = MsgWindow(i).txtRecieve.text & vbCrLf & "<" & SenderNick & " > " & Msg
    14.     Exit Function
    15. End If
    16. Next x
    17. End Function

    The idea is that if a form is open with the caption the same as the SenderNick it will just add the text to that form. It doesnt even get that far...


    Once again, thanks for all your help everyone. I really appreciate it!

  22. #22

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148
    anyone know where i screwed up?

  23. #23
    hellswraith
    Guest
    Use something like this to prevent your subscript out of range error. The reason it happens is because the array is empty.

    VB Code:
    1. 'If the array is unpopulated, will get an error referencing it.
    2.  'This will just initialize the array so no errors occur.
    3.             On Error Resume Next
    4.                 Err.Clear
    5.                 intCount = UBound(MsgWindow)
    6.                 If Err.Number > 0 Then
    7.                     Debug.Print Err.Description
    8.                     ReDim MsgWindow(0)
    9.                     Err.Clear
    10.                 End If
    11.             On Error GoTo 0

    This is an inline error trap, put it before you reference the Ubound of an dynamic array. It will see if the array is empty, if it is, it will put at least one element in it. If the array contains at least one element, it will continue to go through.

  24. #24
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    another thing

    shouldn't the declaration of MsgWindow() be public to the form?
    -= a peet post =-

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Sep 2001
    Posts
    148

    Re: another thing

    Originally posted by peet
    shouldn't the declaration of MsgWindow() be public to the form?
    sounds like that would make sense....right I got rid of the first error (thanks hellswraith) but now im having one more problem....

    VB Code:
    1. For x = 1 To UBound(MsgWindow) 'this isnt finding open forms!!
    2.     If MsgWindow(x).Caption = SenderNick Then
    3.         MsgWindow(i).Visible = True
    4.         MsgWindow(i).ZOrder 0
    5.         MsgWindow(i).txtRecieve.text = .txtRecieve.text & vbCrLf & Msg
    6.         Exit Sub
    7.     End If
    8.     DoEvents
    9. Next x
    sticking a Msgbox MsgWindow(x).Caption in the For doesnt even work....

    I hope this is the last problem and thanks again for everyones help!

  26. #26
    hellswraith
    Guest
    It sounds as though you need to get real intimate with the debug toolbar...lol

    Really, you are going to have to step through your code line by line in break mode, look at the variables, look how many forms are in your array. If the array is empty, well, you will never find a form with that caption.

    Can you post the full source?

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