Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 978
  • Last Modified:

Dynamically Adding an object to a tab

Hi,

I am using the following code to add a label to my form

Set txtlabel = Controls.Add("VB.label", "txtlabel" & y)
txtlabel.Left = 300
txtlabel.top = ltop
txtlabel.Height = 200
txtlabel.Width = 500
txtlabel.Visible = True
txtlabel.Caption = y

This sits on the form fine. Can anyone tell me the code I need to add to get this to sit on a sstab?

thanks
0
kinton
Asked:
kinton
  • 8
  • 5
  • 4
  • +2
1 Solution
 
mokuleCommented:
I don't know VB but try
txtlabel.Parent = sstab
txtlabel.Owner = sstab
0
 
DabasCommented:
Hi mokule:
Good try, but does not help
After creating an SSTab and placing a label on its first Tab, I tried
Debug.Print Label1.Parent.Name

and it gave me Form1.
Also, by setting its parent, we still would not know onto which one of the tabs to put it on!

VB6 does not have an Owner property for the label control

Dabas
0
 
DabasCommented:
kinton:

No luck here, probably need API calls to do this.

This is how a form with an SSTab with a Label in Tab(0) looks like when loaded into NotePad

Unfortunately properties like ControlCount do not seem to be available at runtime

VERSION 5.00
Object = "{BDC217C8-ED16-11CD-956C-0000C04E4C0A}#1.1#0"; "tabctl32.ocx"
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin TabDlg.SSTab SSTab1
      Height          =   2130
      Left            =   300
      TabIndex        =   0
      Top             =   810
      Width           =   3390
      _ExtentX        =   5980
      _ExtentY        =   3757
      _Version        =   393216
      TabHeight       =   520
      TabCaption(0)   =   "Tab 0"
      TabPicture(0)   =   "Form1.frx":0000
      Tab(0).ControlEnabled=   -1  'True
      Tab(0).Control(0)=   "Label1"
      Tab(0).Control(0).Enabled=   0   'False
      Tab(0).ControlCount=   1
      TabCaption(1)   =   "Tab 1"
      TabPicture(1)   =   "Form1.frx":001C
      Tab(1).ControlEnabled=   0   'False
      Tab(1).ControlCount=   0
      TabCaption(2)   =   "Tab 2"
      Tab(2).ControlEnabled=   0   'False
      Tab(2).ControlCount=   0
      Begin VB.Label Label1
         Caption         =   "Label1"
         Height          =   690
         Left            =   675
         TabIndex        =   1
         Top             =   780
         Width           =   795
      End
   End
End
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
amebaCommented:
Hi,
Use third argument when adding control, or specify Container property:

Private Sub Form_Load()
    With Controls.Add("VB.Label", "txtlabel1", Me.SSTab1)
        .Move 300, 600, 1900, 900
        .Visible = True
        .Caption = "Hello1"
    End With
    With Controls.Add("VB.Label", "txtlabel2")
        Set .Container = Me.SSTab1
        .Move 1300, 1990, 1900, 900
        .Visible = True
        .Caption = "Hello2"
    End With
End Sub
0
 
DabasCommented:
ameba:
And if you want it to appear in Tab(2)?

Dabas
0
 
amebaCommented:
Hi Dabas,
That was general code for adding control inside some container.

For tab(2), well, it seems SSTab control might have some problems/bugs (http:Q_20268245.html).  I don't use it, I use TabStrip control + control array of pictureboxes or frames.
0
 
DabasCommented:
ameba:
Choosing between the two controls is like electing one of two awful candidates for president or prime minister!
I have used both. I agree TabStrip is less buggy. But then SSTab is easier to use at design time.

In this particular case, TabStrip might be the way to go.

In the experiment I did (posted earlier on), if I check the Label1's Container, it will point to the form itself (Not the SSTab, although it is obvious that it is contained by the latter). Very strange.

Dabas
0
 
amebaCommented:
There is a bug when code is in Form Load, but it works after form is loaded, e.g. by Clicking the button.
Instead of Form Load, this can be used:

Private Sub Form_Activate()
    Static done As Boolean
   
    SSTab1.Tab = 2

    If done Then Exit Sub ' do this only once
    done = True
   
    With Controls.Add("VB.Label", "txtlabel1", SSTab1)
        .Move 300, 600, 1900, 300
        .Visible = True
        .Caption = "Hello1"
    End With
    With Controls.Add("VB.Label", "txtlabel2")
        Set .Container = Me.SSTab1
        .Move 1300, 1990, 1900, 300
        .Visible = True
        .Caption = "Hello2"
    End With
End Sub
0
 
amebaCommented:
For all controls on the form, Parent will be a form.  Container can be a form if control is sitting directly on the form, or another control.
0
 
DabasCommented:
ameba:
Cool! Well done!
Hope it works for Kinton too!

Dabas
0
 
amebaCommented:
I hope, too. :-)
To be complete, here is the third method
API equivalent of Container is SetParent.  Label doesn't have hwnd, so we'll add textbox:

    With Controls.Add("VB.Textbox", "txt3")
        .Move 1700, 2590, 1900, 300
        .Visible = True
    End With
    SetParent Controls("txt3").hWnd, SSTab1.hWnd

in declarations section:
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
0
 
kintonAuthor Commented:
superb answer ameba, thanks
0
 
amebaCommented:
:-) Thanks!
0
 
akrishmohanCommented:
Hi I have a question for you. Once I have created the text boxes at run time how do I access the same out side the current procedure??
0
 
amebaCommented:
   Me.Controls("txt3").Text = "Hello!!"
0
 
akrishmohanCommented:
hey thanks
0
 
akrishmohanCommented:
CAn u tell me how to pass arrays using get and let methods for a class
0
 
amebaCommented:
Hi akrishmohan, I see you are new here.

You should ask your own question in VB topic area:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/askQuestion.jsp
It's easy question, I suggest 50 pts.
Also make sure you give A grade for correct answer. :-)
0
 
akrishmohanCommented:
thanks ameba
0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 8
  • 5
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now