Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

vb.net add componet to tabcontrol at runtime

Posted on 2004-08-20
13
Medium Priority
?
626 Views
Last Modified: 2010-08-05
I have a tab control that the tabs are created at runtime. I also have a code that automactically creates a textbox on the form at runtime. What I want to know is how can I get the textbox to bind to a certain tab

example

my code creates 2 tabs on the tabcontrol

tab1             tab2


when you click on tab1 I want the user to see the textbox that the code made and when the user hits tab2 doesn't see it. Pretty much the same way it acts if you actually drag a textbox to the tabs. Except I cannot drag an actual textbox or create tabs, my program has to actually create the items through code. ANy ideas


0
Comment
Question by:hogan9111
[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
13 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 140 total points
ID: 11855282
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 286)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' make a tabcontrol with two tabs
        Dim tc As New TabControl
        tc.TabPages.Add(New TabPage("Tab1"))
        tc.TabPages.Add(New TabPage("Tab2"))
        tc.Location = New Point(0, 0)
        tc.Dock = DockStyle.Fill

        ' add a textbox to the first tab
        Dim tb As New TextBox
        tb.Text = "This is a dynamically created textbox"
        tb.Location = New Point(15, 15)
        tb.Width = tc.Width - 30
        tb.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(0).Controls.Add(tb)

        ' add a label to the second tab
        Dim lbl As New Label
        lbl.Text = "This is a dynamically created label"
        lbl.Location = New Point(15, 15)
        lbl.Width = tc.Width - 30
        lbl.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(1).Controls.Add(lbl)

        Me.Controls.Add(tc)
    End Sub

End Class
0
 

Author Comment

by:hogan9111
ID: 11855441
That is perfect. Thank you so much
0
 

Author Comment

by:hogan9111
ID: 11855558
I am sorry I forgot to ask, how do u create events for that.

Meaning lets say i want something to happen when the user starts typing in the textbox that was created at runtime. Howdo u do that????
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!

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11855636
You have to use the AddHandler() function for dynamic controls like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 286)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' make a tabcontrol with two tabs
        Dim tc As New TabControl
        tc.TabPages.Add(New TabPage("Tab1"))
        tc.TabPages.Add(New TabPage("Tab2"))
        tc.Location = New Point(0, 0)
        tc.Dock = DockStyle.Fill

        ' add a textbox to the first tab
        Dim tb As New TextBox
        tb.Text = "This is a dynamically created textbox"
        tb.Location = New Point(15, 15)
        tb.Width = tc.Width - 30
        tb.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(0).Controls.Add(tb)
        AddHandler tb.TextChanged, AddressOf Me.tb_TextChanged   ' <----------------------- AddHandler() call

        ' add a label to the second tab
        Dim lbl As New Label
        lbl.Text = "This is a dynamically created label"
        lbl.Location = New Point(15, 15)
        lbl.Width = tc.Width - 30
        lbl.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(1).Controls.Add(lbl)

        Me.Controls.Add(tc)
    End Sub

    ' Sub to handle dynamic textbox TextChanged event
    Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender.text.Equals("007") Then
            MsgBox("Welcome back Mr. Bond...")
        End If
    End Sub

End Class
0
 

Author Comment

by:hogan9111
ID: 11870772
I hate to bother u again, but I am wanting to create these tabs by an xml file. So how could you have the labels be created automactially by the array.


Example

I have an array that has loaded 4 items in it.

I want the code to look at array 1 and create the label to that name,

then look at array 2 and create the label to that name, etc....
From ur example i was lost of what to do since u are hardcoding the lbl in already, I would almost need something like

dim lbl(i) as new label
 
       i.Text = "This is a dynamically created label"
        i.Location = New Point(15, 15)
        i.Width = tc.Width - 30

i would be the array, I know this doesn't work, but tryin to explain to u





'what I am look at

      Dim lbl As New Label
        lbl.Text = "This is a dynamically created label"
        lbl.Location = New Point(15, 15)
        lbl.Width = tc.Width - 30
        lbl.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(1).Controls.Add(lbl)


0
 

Author Comment

by:hogan9111
ID: 11871157
what do u think???
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11871222
>> I have an array that has loaded 4 items in it.

Are the items in the array the text for each label?

Do you want the four labels on the first tab?
<or>
Does each item in the array become a label on a corresponding tab?

Idle_Mind
0
 

Author Comment

by:hogan9111
ID: 11872478
>>Are the items in the array the text for each label?

yes

I want the labels to be create by the arrays, so in this example I have 4 so 4 labels would be created. Now if the array had 8 then 8 labels would be created, etc.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11872688
It would look something like this:

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 286)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' make a tabcontrol with two tabs
        Dim tc As New TabControl
        tc.TabPages.Add(New TabPage("Tab1"))
        tc.TabPages.Add(New TabPage("Tab2"))
        tc.Location = New Point(0, 0)
        tc.Dock = DockStyle.Fill

        ' add a textbox to the first tab
        Dim tb As New TextBox
        tb.Text = "This is a dynamically created textbox"
        tb.Location = New Point(15, 15)
        tb.Width = tc.Width - 30
        tb.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(0).Controls.Add(tb)
        AddHandler tb.TextChanged, AddressOf Me.tb_TextChanged

        ' add a label(s) from Array to the second tab
        Dim labelText() As String = {"Dog", "Cat", "Fish", "Hamster"} ' this would come from your XML file somehow
        Dim strLabel As String
        Dim lbl As Label
        Dim i As Integer
        For i = labelText.GetLowerBound(0) To labelText.GetUpperBound(0)
            lbl = New Label
            lbl.Text = labelText(i)
            lbl.Location = New Point(15, 15 + (lbl.Height * i))
            lbl.Width = tc.Width - 30
            lbl.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
            tc.TabPages(1).Controls.Add(lbl)
        Next i

        Me.Controls.Add(tc)
    End Sub

    Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender.text.Equals("007") Then
            MsgBox("Welcome back Mr. Bond...")
        End If
    End Sub

End Class
0
 

Author Comment

by:hogan9111
ID: 11873275
ok I did that and it seemed to work except for

 AddHandler tb.TextChanged, AddressOf Me.tb_TextChanged


I was wanting to use this for the labels I created. I used the linklabel instead of label.

I created 1 sub statement like this


Private Sub lbl_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                MsgBox(lbl.text)
          End Sub


but I need to have the correct label click name show up.


so if there is 4 items

{"Dog", "Cat", "Fish", "Hamster"}

and they click on cat

the code would be like this

Private Sub lbl_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
                   MsgBox(lbl.text)
 End Sub


and the msgbox would show the word Cat, Or if the user hits Dog the msgbox would say Dog, etc.....

I really appreciate the help. I can open a question to reward points if u wish...

thanks I really need this asap


0
 

Author Comment

by:hogan9111
ID: 11873284
The reason why I have 1 sub statement is so that I didn't have to either hardcode them or have the form autogenerate them. just do whatever I need to do by the label the user hits
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 11873457
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(488, 286)
        Me.Name = "Form1"
        Me.Text = "Form1"

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' make a tabcontrol with two tabs
        Dim tc As New TabControl
        tc.TabPages.Add(New TabPage("Tab1"))
        tc.TabPages.Add(New TabPage("Tab2"))
        tc.Location = New Point(0, 0)
        tc.Dock = DockStyle.Fill

        ' add a textbox to the first tab
        Dim tb As New TextBox
        tb.Text = "This is a dynamically created textbox"
        tb.Location = New Point(15, 15)
        tb.Width = tc.Width - 30
        tb.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
        tc.TabPages(0).Controls.Add(tb)
        AddHandler tb.TextChanged, AddressOf Me.tb_TextChanged

        ' add a label(s) from Array to the second tab
        Dim labelText() As String = {"Dog", "Cat", "Fish", "Hamster"} ' this would come from your XML file somehow
        Dim strLabel As String
        Dim linkLbl As LinkLabel
        Dim i As Integer
        For i = labelText.GetLowerBound(0) To labelText.GetUpperBound(0)
            linkLbl = New LinkLabel
            linkLbl.Text = labelText(i)
            linkLbl.Links(0).LinkData = labelText(i)
            linkLbl.Location = New Point(15, 15 + (linkLbl.Height * i))
            linkLbl.Width = tc.Width - 30
            linkLbl.Anchor = AnchorStyles.Top Or AnchorStyles.Left Or AnchorStyles.Right
            tc.TabPages(1).Controls.Add(linkLbl)
            AddHandler linkLbl.LinkClicked, AddressOf linklbl_LinkClicked
        Next i

        Me.Controls.Add(tc)
    End Sub

    Private Sub tb_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        If sender.text.Equals("007") Then
            MsgBox("Welcome back Mr. Bond...")
        End If
    End Sub

    Private Sub linklbl_LinkClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs)
        MsgBox(e.Link.LinkData)
    End Sub

End Class
0
 

Author Comment

by:hogan9111
ID: 11873539
ok, thank u
I also did msgbox(Me.ActiveControl.Text) and it seemed to work

thanks
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Six Sigma Control Plans

730 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