Solved

Dynamically Adding an object to a tab

Posted on 2004-03-27
19
954 Views
Last Modified: 2008-05-30
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
Comment
Question by:kinton
  • 8
  • 5
  • 4
  • +2
19 Comments
 
LVL 17

Expert Comment

by:mokule
Comment Utility
I don't know VB but try
txtlabel.Parent = sstab
txtlabel.Owner = sstab
0
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
ameba:
And if you want it to appear in Tab(2)?

Dabas
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
 
LVL 27

Expert Comment

by:Dabas
Comment Utility
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
 
LVL 15

Accepted Solution

by:
ameba earned 500 total points
Comment Utility
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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 27

Expert Comment

by:Dabas
Comment Utility
ameba:
Cool! Well done!
Hope it works for Kinton too!

Dabas
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
 
LVL 2

Author Comment

by:kinton
Comment Utility
superb answer ameba, thanks
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
:-) Thanks!
0
 

Expert Comment

by:akrishmohan
Comment Utility
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
 
LVL 15

Expert Comment

by:ameba
Comment Utility
   Me.Controls("txt3").Text = "Hello!!"
0
 

Expert Comment

by:akrishmohan
Comment Utility
hey thanks
0
 

Expert Comment

by:akrishmohan
Comment Utility
CAn u tell me how to pass arrays using get and let methods for a class
0
 
LVL 15

Expert Comment

by:ameba
Comment Utility
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
 

Expert Comment

by:akrishmohan
Comment Utility
thanks ameba
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now