Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > Creating new objects at runtime


Reply
 
Thread Tools Display Modes
  #1  
Old 11-15-2009, 04:07 PM
james2k2's Avatar
james2k2 james2k2 is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 349
Default 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
Reply With Quote
  #2  
Old 11-16-2009, 09:47 AM
emartinho emartinho is offline
Centurion
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 194
Default

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
Reply With Quote
  #3  
Old 11-16-2009, 10:24 AM
james2k2's Avatar
james2k2 james2k2 is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 349
Default

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
Reply With Quote
  #4  
Old 11-16-2009, 12:20 PM
emartinho emartinho is offline
Centurion
 
Join Date: Jan 2009
Location: Toronto, Ontario
Posts: 194
Default

You should put separate issues in separate threads, that way you get the most exposure.

Good luck.
-EM
Reply With Quote
  #5  
Old 11-16-2009, 12:47 PM
Flyguy's Avatar
Flyguy Flyguy is offline
Lost Soul

Super Moderator
* Guru *
 
Join Date: May 2001
Location: Vorlon
Posts: 18,884
Default

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.
Reply With Quote
  #6  
Old 11-17-2009, 01:45 AM
james2k2's Avatar
james2k2 james2k2 is offline
Junior Contributor
 
Join Date: Oct 2002
Posts: 349
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->