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
Solved

Dynamically Adding an object to a tab

Posted on 2004-03-27
19
962 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
Last week, our Skyport webinar on “How to secure your Active Directory” (https://www.experts-exchange.com/videos/5810/Webinar-Is-Your-Active-Directory-as-Secure-as-You-Think.html?cid=Gene_Skyport) provided 218 attendees with a step-by-step guide for…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

808 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