Solved

Dynamically Adding an object to a tab

Posted on 2004-03-27
19
966 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
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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

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