Solved

vb.net add componet to tabcontrol at runtime

Posted on 2004-08-20
13
606 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
Comment Utility
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
Comment Utility
That is perfect. Thank you so much
0
 

Author Comment

by:hogan9111
Comment Utility
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
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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
Comment Utility
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
Comment Utility
what do u think???
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
>> 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
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
ok, thank u
I also did msgbox(Me.ActiveControl.Text) and it seemed to work

thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

RIA (Rich Internet Application) tools are interactive internet applications which have many of the characteristics of desktop applications. The RIA tools typically deliver output either by the way of a site-specific browser or via browser plug-in. T…
This is an explanation of a simple data model to help parse a JSON feed
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now