Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating usercontrol in runtime 1

Status
Not open for further replies.

bascy

Programmer
Mar 6, 2001
53
NL
I have created an OCX with a usercontrol called PrcDatagrid. It essentially consists of a grid and a toolbar with a few buttons.

In my main form one such control is added at design time.
I want to be able to create more instances of the usercontrol PrcDatagrid at runtime. I want to use those to display child-data from the selected parent-record.

I'v tried:
Code:
   Dim pdgDetail as PrcDatagrid.PrcDatagridControl
   set pdgDetail = new Prcdatagrid.PrcDatagridControl
This gives me the error "Invalid use of new keyword"

I'v tried:
Code:
   Dim pdgDetail as Object
   Set pdgDetail = CreateObject("PrcDatagrid.PrcDatagridControl")
   PdgDetail.visible = true
This gives me the error "Object doesnt support this property or method" on the last line.

Can anyone help me with this?????
Thanks
 
You can not use the New command on controls since the control interface is cosidered public but not creatable (you can declare a pointer to it, but you can not directly instantiate it). That does not, however, mean that you can not load new controls at run time. You have two options that you can use. One is a simple control array and the other is to use "control constructor".
The option uses a control array. To do this, simply set the index property of your control that you loaded at design time and then load new controls at runtime (i.e. Load MyControl(newIndexNumber)). With this methos, you will be able to respond to all of the events fired off of the original control as well as new ones that are added to the forms at runtime.
The other option is to use the add method on the form's control collection. This method will accept the ProgID of the control that you want to add as well as the name that you want to give you control. After it is created, you will need to position, set any other properties, and then make it visible. See the following example . . .


Dim objTextbox As VB.TextBox


'** Ask the form's control collection to instantiate the
'** control for us. When it is done, it will return a
'** reference to the control.

Set objTextbox = Form1.Controls.Add("VB.TEXTBOX", "txtTextBox")


'** Set the control properties and make it visible.

With objTextbox
.Top = 100
.Left = 100
.Visible = True
End With




Of course there is a catch to this method. Using the forms control collection to create new forms, you do not have any direct access to the events that can be fired by the control (try to imagine a adding button with no Click event). But, there is a way around that as well. You can create Class Wrappers that hold an instance of your control object declared WithEvents. After you use the form's control collection to create the control, set your reference int the class wrapper and you will have full access to all of the fired events.
This really isn't as bad as it sounds. If you need some more help on this, let me know and I will try to post a small example. - Jeff Marler B-)
 
Thanks for the advice!
I'v got it working with the Controlarray technique.

Bascy
 
Great! Glad to hear that it worked for you. Just remember however, that for the control array technique to work, you will always have to have at least 1 control (of the same type that you want to add at runttime) added at design time. If you are OK with that, then this should work fine for you. - Jeff Marler B-)
 
Hello jmarler,

If you are still around, could you please give an example of the class wrapper you reference above. I have done mostly VBA but have programmed classes and used with events so I think I have an idea about what you are talking about.

Thanks in advance for any help you can give me!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top