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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Controls at runtime to SSTab 1

Status
Not open for further replies.

MZwijacz

Programmer
Jun 27, 2002
33
US
I have been experimenting with adding controls at runtime. What I'm trying to do is connect to a db not knowing the structure of the db and field list; the db is user customizable, Build controls that will display the db fields. I therefore have an xml file with the field names from the db. The customization tool creates the xlm file. It also lets the user customize the presentation of the db. By reading the xml I know how many fields how to connect to the db and where to put the text boxes.

In a Form the following will work:
_________________________________________________________

'Start Creating Controls
'Add For loop to get all controls
For i = 0 To oNodes.length - 1
Set oNode = oNodes.Item(i)
strControlType = oNode.nodeName
strControlName = oNode.Attributes.Item(0).nodeTypedValue
'Parse control type to get control type ie textbox
'
'Create control
'*** This is the line that is not working *************
Me.Controls.Add strControlType, strControlName
'*******************************************************
Set oChildNodes = oNode.childNodes
With Me(strControlName)
.Visible = True
'Find property Values
For ic = 0 To oChildNodes.length - 1
Set oChildNode = oChildNodes.nextNode
If Not Left(oChildNode.nodeName, 1) = "#" Then
strAttName = oChildNode.nodeName
strAttValue = oChildNode.Text

' set properties of control
Select Case strAttName
Case "Height"
.Height = strAttValue
Case "Left"
.Left = strAttValue
Case "Top"
.Top = strAttValue
Case "Width"
.Width = strAttValue
Case "Text"
.Text = strAttValue
End Select
'end set properties
End If
Next
End With
Next
__________________________________________________________

I am acually opening up an xml doc and reading the values for the controls.

The problem is I was trying to convert this to a sstab on an activex control. There seems to be no controls collection in the userControl or on the ssTab control. Is that correct ?

What would be a better approch to solve the problem ?

Thanks,
Michael F. Zwijacz
 
This is the code that I use to add a control to the SSTab.

Code:
Form1.Controls.Add "VB.CommandButton", "cmdTEST", SSTab1
With Form1.Controls("cmdTEST")
     .Visible = True
     .Top = 560
     .Left = 240
     .Width = 1000
     .Caption = "TEST"
End With

You need to specify the container in the third Add method argument. The default is the form. One thing to watch is that it will only add controls to the active tab. So you need to spoecify which tab is currently active before you can add controls to a specific tab.


Take Care,

zemp
 
zemp,

Thanks for responding. I used something like that when I was testing in a form. It worked, however I'm trying to move the code into an activeX control and there is no form object. The container is a UserControl and doesn't seem to have a controls collection to add to.

Any ideas ?

Thanks,
 
I placed the following code in the user control initialize event and it worked. Created a command button on the sstab control contained within a user control that was on a form.

Code:
UserControl.Controls.Add "VB.CommandButton", "cmdTEST", SSTab1
   With UserControl.Controls("cmdTEST")
        .Visible = True
        .Top = 560
        .Left = 240
        .Width = 1000
        .Caption = "TEST"
   End With


Take Care,

zemp
 
It worked ! The object browser says that the controls property is read only and when I tried to type after controls, there was no intelesence. I thought that ment there was no object or property.

Thanks !!!
 
Can you do this with VB5?

I want to dynamically add some textboxes at runtime. I am not sure if this is done via a control array or collection as you mention. I have been tinkering with your code without success.



Thanks,

Michael42
 
It can be done either way, depending on whether you want the controls to be part of an array or not.

To create a control array the first element must be placed on the form in design time and given an index of zero(0). Then you can use the load statement to add to the control array. If
Code:
Text1(0)
exists then use,

Code:
Load Text1(1)

To add to the collection you use the code posted above. Are you trying to add to a form? Post the code you have so far and we will have a look.

Take Care,

zemp
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top