 |
 |

11-15-2009, 04:07 PM
|
 |
Junior Contributor
|
|
Join Date: Oct 2002
Posts: 349
|
|
Creating new objects at runtime
|
Hi All,
Firstly, apologies if this is in the wrong section.
I have written the code below:
Code:
Private Function NewObject(ObjType As String, ObjName As String)
Dim NewObj As Control
If ObjType = "List" Then
NewObj = ObjName
Set NewObj = Me.Controls.Add("VB.TextBox", "Text1", Me)
NewObj.Visible = True
End If
End Function
And basically, it won't work. I am trying to make an application that can take commands from an ini file to do things such as dynamically add controls and manipulate these after creation. For this reason, i need a user to be able to specify a controls name in the ini file so that my application can keep track of this control. I fear the problem lies with me not being able to use a variable as an identifier. My only other thought currently is to create an initial control for all the controls i'm allowing on the form and set an index as 0 and then creating new controls from this. Then to track them, i use say the 'Tag' property. The problem i face with this method is lots of potential memory usage for no reason and i need this app to be as quick as possible with VB6.
Many thanks for reading.
|
__________________
I don't have a signature
Last edited by james2k2; 11-15-2009 at 04:08 PM.
Reason: Missed the thanks
|

11-16-2009, 09:47 AM
|
|
Centurion
|
|
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 194
|
|
Hi,
I slightly modified your code and it worked fine.
Code:
Private Sub Command1_Click()
NewObject "List", "Abc123"
End Sub
Private Sub NewObject(ObjType As String, ObjName As String)
Dim NewObj As Control
If ObjType = "List" Then
' NewObj = ObjName <-- Not necessary??
Set NewObj = Me.Controls.Add("VB.TextBox", ObjName) '<-- No need to specify a container since you want the
'control on Me. If you wanted it on another form, then you'd need to pass that form's object in the 3rd parameter.
NewObj.Visible = True
msgbox NewObj.Name
End If
End Sub
This created a textbox in the top left of my form whose name is "Abc123".
Hope this helps.
-EM
|
|

11-16-2009, 10:24 AM
|
 |
Junior Contributor
|
|
Join Date: Oct 2002
Posts: 349
|
|
|
Hello,
Thanks for your reply. My next issue is the following: Say i want to modify the object later (i.e. move it, resize it etc...), how would i go about that without directly specifying the objects name, i.e. I can have another function called ModifyObject(ObjName as String, ObjCommand as String, CommandValue as String). And then from this be able to perform any function I require.
Kind regards.
|
__________________
I don't have a signature
|

11-16-2009, 12:20 PM
|
|
Centurion
|
|
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 194
|
|
|
You should put separate issues in separate threads, that way you get the most exposure.
Good luck.
-EM
|
|

11-16-2009, 12:47 PM
|
 |
Lost Soul
Super Moderator * Guru *
|
|
Join Date: May 2001
Location: Vorlon
Posts: 18,884
|
|
|
I would go for control arrays.
All events are there and you can easily access the loaded controls and all the events are raised in "normal" event code.
|
|

11-17-2009, 01:45 AM
|
 |
Junior Contributor
|
|
Join Date: Oct 2002
Posts: 349
|
|
|
Yeah, from a code point of view, using control arrays is probably the way forward. I suppose i can just use the 'Tag' property to store the controls name for reference.
Many thanks to both of you.
|
__________________
I don't have a signature
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|