Solved

Dynamically Adding an object to a tab

Posted on 2004-03-27
19
967 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
  • 4
  • +2
19 Comments
 
LVL 17

Expert Comment

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

Expert Comment

by:Dabas
ID: 10696279
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
ID: 10696313
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 15

Expert Comment

by:ameba
ID: 10696564
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
ID: 10697120
ameba:
And if you want it to appear in Tab(2)?

Dabas
0
 
LVL 15

Expert Comment

by:ameba
ID: 10697181
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
ID: 10697206
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
ID: 10697240
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
ID: 10697257
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
 
LVL 27

Expert Comment

by:Dabas
ID: 10697275
ameba:
Cool! Well done!
Hope it works for Kinton too!

Dabas
0
 
LVL 15

Expert Comment

by:ameba
ID: 10697299
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
ID: 10699000
superb answer ameba, thanks
0
 
LVL 15

Expert Comment

by:ameba
ID: 10699131
:-) Thanks!
0
 

Expert Comment

by:akrishmohan
ID: 10778709
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
ID: 10778771
   Me.Controls("txt3").Text = "Hello!!"
0
 

Expert Comment

by:akrishmohan
ID: 10778860
hey thanks
0
 

Expert Comment

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

Expert Comment

by:ameba
ID: 10779115
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
ID: 10779133
thanks ameba
0

Featured Post

Free NetCrunch network monitor licenses!

Only on Experts-Exchange: Sign-up for a free-trial and we'll send you your permanent license!

Here is what you get: 30 Nodes | Unlimited Sensors | No Time Restrictions | Absolutely FREE!

Act now. This offer ends July 14, 2017.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Uncontrolled local administrators groups within any organization pose a huge security risk. Because these groups are locally managed it becomes difficult to audit and maintain them.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

726 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