Brief Summary
User controls are almost Forms except that you can create custom controls or just a form you want to attach to a form, which I will tell you how. This adds simplicity, professionalism, and neatness in any application.
Using User Control
In this tutorial, I will teach you how to use user controls to attach to your form using a simple control, the Panel. The panel will be used as a place holder for the User Control. The most efficient way of using this is to use a User Control instead of a form that you would have pop up which can clutter and complicate things.• Using a Windows Form, which will be the main window, is where everything going to be attached to. It is kinda like adding extensions, but already programmed into your application.
○ Create a User Control
- Customize the User Control as you want
○ In Form1 or your main form (Windows Forms) is what going to bring everything together. To add your User Control into Form1, add this code. (Can be executed via Button, Label, ect)
Code:
Panel1.Controls.Add(New UserControl1)
With that simple code, you have now placed a User Control inside of Panel1
For better effects, add a Timer inside your User Control.• Switch to your code and add this code
○ In your timer properties, enable the timer to True
Code:
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Width = Panel1.Width
Height = Panel1.Height
End Sub
Adding User Controls into New Tabs in a TabControl
• This is the same exact process of adding into a panel, as described above. Only difference is adding User Control into a New TabPage instead.
○ In Form1, add this code instead
Code:
Dim tab As New TabPage
tab.Text = ""
TabControl1.TabPages.Add(tab)
tab.Controls.Add(New UserControl1)
• You can also add this if you want the new tab to be focused
Code:
Dim c As Integer = TabControl1.TabPages.Count - 1
TabControl1.SelectedTab = TabControl1.TabPages(c)
• You can also add this if you only want it to open and not open another if one is already opened (I think I explained that right... You can only open it if its not already open. Else if there is one already opened, then the already opened one will be focused on)
Code:
Dim ThisTab As Boolean = False 'Determines if the tab is already opened or not
Dim ThisTabID As Integer ' This will be the place holder for this tab index
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ThisTab = False Then
Dim tab As New TabPage
tab.Text = ""
TabControl1.TabPages.Add(tab)
tab.Controls.Add(New UserControl1)
Dim c As Integer = tc.TabPages.Count - 1
TabControl1.SelectedTab = tc.TabPages(c)
ThisTab = True ' Sets to True, saying that this tab is now opened
ThisTabID = c 'This sets the TabPage index
Else ' Else this tab is already opened
TabControl1.SelectedTab = TabControl1.TabPages(ThisTabID) ' Since its already opened, focuses on the tab. You can also delete this line of code so if its already opened, nothing will happen.
End If
End Sub
• and if you have tabs that can be closed
○ On the User Control that is being closed, make sure you include this code.
Code:
ThisTab = False
ThisTabID=nothing
○ Create a Module, and add this code there
Code:
Public ThisTab As Boolean = False
Public ThisTabID As Integer
○ Now add this code to Form1
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If ThisTab = False Then
Dim tab As New TabPage
tab.Text = ""
TabControl1.TabPages.Add(tab)
tab.Controls.Add(New UserControl1)
Dim c As Integer = tc.TabPages.Count - 1
TabControl1.SelectedTab = tc.TabPages(c)
ThisTab = True ' Sets to True, saying that this tab is now opened
ThisTabID = c 'This sets the TabPage index
Else ' Else this tab is already opened
TabControl1.SelectedTab = TabControl1.TabPages(ThisTabID) ' Since its already opened, focuses on the tab. You can also delete this line of code so if its already opened, nothing will happen.
End If
End Sub
That is all