• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 637
  • Last Modified:

vb.net add componet to tabcontrol at runtime

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
hogan9111
Asked:
hogan9111
  • 8
  • 5
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hogan9111Author Commented:
That is perfect. Thank you so much
0
 
hogan9111Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hogan9111Author Commented:
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
 
hogan9111Author Commented:
what do u think???
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> 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
 
hogan9111Author Commented:
>>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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hogan9111Author Commented:
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
 
hogan9111Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
hogan9111Author Commented:
ok, thank u
I also did msgbox(Me.ActiveControl.Text) and it seemed to work

thanks
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now