|
-
Oct 14th, 2001, 11:16 AM
#1
Thread Starter
Addicted Member
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 .
-
Oct 14th, 2001, 11:20 AM
#2
-= B u g S l a y e r =-
-
Oct 14th, 2001, 11:20 AM
#3
Thread Starter
Addicted Member
Originally posted by peet
ké?
.........?
-
Oct 14th, 2001, 11:21 AM
#4
PowerPoster
no it's not easy because it's not possible. Variable names aren't dynamic.
-
Oct 14th, 2001, 11:21 AM
#5
Member
I guess it means "What?"
Why can't you just say "Dim NewForm1 As New Form1"?
-
Oct 14th, 2001, 11:23 AM
#6
Thread Starter
Addicted Member
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??
-
Oct 14th, 2001, 11:24 AM
#7
-= B u g S l a y e r =-
make a collection of forms, that might be what u want?
-
Oct 14th, 2001, 11:24 AM
#8
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"
-
Oct 14th, 2001, 11:27 AM
#9
Thread Starter
Addicted Member
I was under the impression you couldnt do a form array....
How would I make a collection?
-
Oct 14th, 2001, 11:30 AM
#10
-= B u g S l a y e r =-
using a collection, u get control of u'r forms.
U cant change the names though...
VB Code:
Option Explicit
Private frmDet As frmDetail
Private fFrmDetCol As New Collection
Private Sub Command1_Click()
Set frmDet = New frmDetail
frmDet.Caption = "Form detail no " & fFrmDetCol.Count + 1
frmDet.Tag = fFrmDetCol.Count + 1
fFrmDetCol.Add frmDet, frmDet.Tag
frmDet.Show
End Sub
Private Sub Command2_Click()
Dim i As Integer
For i = 1 To fFrmDetCol.Count
MsgBox fFrmDetCol(i).Caption
fFrmDetCol(i).SetFocus
Next i
End Sub
-
Oct 14th, 2001, 11:31 AM
#11
PowerPoster
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
-
Oct 14th, 2001, 11:32 AM
#12
Thread Starter
Addicted Member
ok well last time I checked that post it wasnt there...heh...nm i guess....
*feels really dumb*
-
Oct 14th, 2001, 11:34 AM
#13
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
-
Oct 14th, 2001, 11:38 AM
#14
Thread Starter
Addicted Member
uuhh.....engligh translation please??
Would there be an advantage to using peet's code instead of
VB Code:
i = UBound(MyForms) + 1
ReDim Preserve MyForms(i)
Set MyForms(i) = New Form1
?
-
Oct 14th, 2001, 11:43 AM
#15
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.
-
Oct 14th, 2001, 11:43 AM
#16
PowerPoster
-
Oct 14th, 2001, 11:52 AM
#17
Thread Starter
Addicted Member
Type mismatch in i = UBound(myforms) + 1
-
Oct 14th, 2001, 11:55 AM
#18
PowerPoster
Is MyForms declared as a form array?
-
Oct 14th, 2001, 12:13 PM
#19
-
Oct 14th, 2001, 01:34 PM
#20
Thread Starter
Addicted Member
Originally posted by hellswraith
Dim MyForms() As Form1
o heh. thanks everyone for all ur help!
-
Oct 14th, 2001, 02:25 PM
#21
Thread Starter
Addicted Member
Another problem! (last one I hope!)
Here is the entire function:
VB Code:
Public Function DoMsg(SenderNick, Msg)
Dim MsgWindow() As frmMsg, i, User(), x
i = UBound(MsgWindow) + 1 'Subscript Out Of Range?!
ReDim Preserve MsgWindow(i)
Set MsgWindow(i) = New frmMsg
For x = 0 To UBound(MsgWindow) 'Object Variable or with block not set?!
If MsgWindow(x).Caption = SenderNick Then
MsgWindow(x).txtRecieve.text = MsgWindow(x).txtRecieve.text & vbCrLf & "< & sendernick & " > " & msg"
Exit Function
Else
MsgWindow(x).Show
MsgWindow(x).Caption = SenderNick
MsgWindow(x).txtRecieve.text = MsgWindow(i).txtRecieve.text & vbCrLf & "<" & SenderNick & " > " & Msg
Exit Function
End If
Next x
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!
-
Oct 14th, 2001, 02:54 PM
#22
Thread Starter
Addicted Member
anyone know where i screwed up?
-
Oct 14th, 2001, 05:09 PM
#23
Use something like this to prevent your subscript out of range error. The reason it happens is because the array is empty.
VB Code:
'If the array is unpopulated, will get an error referencing it.
'This will just initialize the array so no errors occur.
On Error Resume Next
Err.Clear
intCount = UBound(MsgWindow)
If Err.Number > 0 Then
Debug.Print Err.Description
ReDim MsgWindow(0)
Err.Clear
End If
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.
-
Oct 14th, 2001, 05:15 PM
#24
-= B u g S l a y e r =-
another thing
shouldn't the declaration of MsgWindow() be public to the form?
-
Oct 14th, 2001, 09:41 PM
#25
Thread Starter
Addicted Member
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:
For x = 1 To UBound(MsgWindow) 'this isnt finding open forms!!
If MsgWindow(x).Caption = SenderNick Then
MsgWindow(i).Visible = True
MsgWindow(i).ZOrder 0
MsgWindow(i).txtRecieve.text = .txtRecieve.text & vbCrLf & Msg
Exit Sub
End If
DoEvents
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!
-
Oct 15th, 2001, 06:00 AM
#26
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|