Creating and Moving Controls Dynamically within .NET

Question: I understand that .NET offers inheritance and I think that that may help with a project that I am currently working on. Let’s say that I have a single control. I want to use that control across multiple tabs on a form. How would I go about that?

 

Answer:

Absolutely, the Framework offers the full breadth of object orientation. In practicality we can use this many different ways. In order to answer this question, I wanted to show three different samples.

 

Example 1:

Let’s say that I had a windows form application and wanted to add a button to the form dynamically. When adding a button dynamically, I simply create a new instance of the button class and show it. So within the form I would add the following code.

 

Dim NewBut As New Button

NewBut.Visible = True

NewBut.Text = "Added Dynamically"

Me.Controls.Add(NewBut)

 

Example 2:

Let’s say that I wanted to add the same button to a tab control and also add an event that I could write some code in.

 

Public tabbut As New Button

 

Private Sub tabform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        AddHandler tabbut.Click, AddressOf tabbut_Click

        ' create the base button that we will use

        ' place it on tab 1

        tabbut.Text = "Added to Tab Page"

        tabbut.BackColor = System.Drawing.Color.Bisque

        TabPage1.Controls.Add(tabbut)

 

    End Sub

Private Sub tabbut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        MsgBox("In Event handler")

End Sub

 

Example 3:

Let’s say that I wanted to add the same button to a tab control. But when the user changed tabs I wanted to move the button between the two tabs and display a message box that displayed the current tab.

 

Public Tabbut As New Button

 

Private Sub tabform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' add the handler in the form load

        AddHandler Tabbut.Click, AddressOf tabbut_Click

 

        ' create the base button that we will use

        ' place it on tab 1

        Tabbut.Text = "Added to Tab Page"

        Tabbut.BackColor = System.Drawing.Color.Bisque

        TabPage1.Controls.Add(Tabbut)

 

    End Sub

    Private Sub tabbut_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Select Case TabControl1.SelectedTab.TabIndex

            Case 0

                MsgBox("On tab page 1")

            Case 1

                MsgBox("On tab page 2")

        End Select

    End Sub

 

    Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.Click

       

        Select Case TabControl1.SelectedTab.TabIndex

            Case 0

                Tabbut.BackColor = System.Drawing.Color.Bisque

                TabPage1.Controls.Add(Tabbut)

            Case 1

                Tabbut.BackColor = System.Drawing.Color.Red

                TabPage2.Controls.Add(Tabbut)

        End Select

    End Sub

 

If you are interested in seeing this sample at work you can download the sample from here.