Insert a form without form designer in Visual Basic
You need to know how to create a form in Visual Basic without the Visual Basic Express forms designer. Yes is possible, you just need to check a few things before continuing:
- Make sure the reference is added in your project
- Declare your form with dim, public, private, protected �
- Create the object using the new function
- Add controls in your form
- Show your form
Add the reference for the form
You absolutely need to make sure the reference System.Windows.Forms is part of you project. You could add it directly from Visual Basic Express or Visual Studio. You could add it dynamically but is just a little bit more complicate.
With the library, you will have access to all the stuff useful to create your windows forms.
You have to choose your startup object, is not really important what type of application you choose since this is a sample.
I made a video for a previous post. It might give you an idea but is not that complete than this one: How create a form from scratch in Visual Basic
Create the form object
I created a little project. That project is a Windows Application named CreateFromWithoutFormDesigner.
Form1 is the object started and is what you are used to. The next explanation will show you how to create a form without the form designer.
If you double click the form1, you will get the empty load form:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load End Sub End Class |
I decided to fill the Label1 and the Button1 with basic information:
Public Class Form1 Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load With Label1 .Text = "this form will start the new one" End With With Button1 .Text = "open new form" End With With Me .Text = Me.Name End With End Sub End Class |
Ok, now there are three lines you have to add. A first line is to declare the form object. The second line is to create the form object. The last line is to show the form. Here are the 3 lines:
Dim oForm As Form oForm = New Form oForm.Show () |
You could put those 3 lines in the click button1 event:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oForm As Form oForm = New Form oForm.Show () End Sub |
When you run the program, the form1 will show. You click on the button to display the new form called oForm. oForm is not created or designer from the form designer.
Add Control in your form dynamically
The form designer is very fun to use and useful to create form and usercontrol. Each time you add a control in your form, you have to think about the position of each control in that form. So is a pain in the butt.
I�ll make a little sample on adding control dynamically in a form. Because I�m too lazy to make simple complex, I�ll make a simple table.
'adding controls to make a table Dim oTextbox As TextBox For index1 As Integer = 0 To 3 Step 1 For index2 As Integer = 0 To 3 Step 1 oTextbox = New TextBox() With oTextbox .Name = "TextBox" & index1.ToString & index2.ToString .Text = index1.ToString & index2.ToString .Width = Me.Width / 4 .Left = index1 * (Me.Width / 4) '.Height .Top = index2 * .Height End With oForm.Controls.Add(oTextbox) Next index2 Next index1 |
If you use the show function to display your dynamically created form, you could simply put it before you call the show function or after. If you use ShowDialog, your control will not be added in your form because your form is modal.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim oForm As Form oForm = New Form oForm.Show() 'adding controls to make a table Dim oTextbox As TextBox For index1 As Integer = 0 To 3 Step 1 For index2 As Integer = 0 To 3 Step 1 oTextbox = New TextBox() With oTextbox .Name = "TextBox" & index1.ToString & index2.ToString .Text = index1.ToString & index2.ToString .Width = Me.Width / 4 .Left = index1 * (Me.Width / 4) '.Height .Top = index2 * .Height End With oForm.Controls.Add(oTextbox) Next index2 Next index1 End Sub |
When you run the program, you new form will look like this:
I don�t want to make big post. That is the reason I�ll stop here. There are many things we could do here.
If you like this post, leave a comment or share it.
Reference
Download the sample project: CreateFromWithoutFormDesigner.zip
Visit my web site: Check Technologies Official Web Site
Update Contact :
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email : Fajarudinsidik@gmail.com
No Wa/Telepon (puat) : 085267792168
No Wa/Telepon (fajar) : 085369237896
Email: Fajarudinsidik@gmail.com
atau Kirimkan Private messanger melalui email dengan klik tombol order dibawah ini :