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

vb.net add componet to tabcontrol at runtime

Posted on 2004-08-20
13
620 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
  • 8
  • 5
13 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 35 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 85

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 85

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 85

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 85

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 94
Removing line numbers from left column in config file 7 61
some keys in my laptop is not working. any suggestion 6 39
youtube blocking politics 4 55
This is about my first experience with programming Arduino.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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 …

839 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