VB.Net Programming Using Notepad And Command Prompt (Part II) Building Windows Form Application
In this tutorial I'll show you how to create a Windows Form Application. Before reading this tutorial further, it'd better if you see my previous tutorial on http://www.dreaminco...&#entry1111651.
Creating Form
I assume that you've already installed .Net Framework on your machine and ,of course, knowing VB.Net Basic. Let's get started to have some code. Open the Notepad by clicking Start -> Run -> Type Notepad. Then notepad application will be displayed. Then type this code below :
Imports System.Windows.Forms
Partial Class MyForm : Inherits Form
Private Sub InitializeComponent()
Me.Text = "My First Example Form"
End Sub
Public Sub New()
InitializeComponent()
End Sub
Public Shared Sub Main()
Dim theForm As New MyForm
theForm.ShowDialog()
End Sub
End Class
Then save the file with ".vb" extension. I saved the file With FileName = MyForm.Designer.vb, and ensure you choose Save As Type = AllFiles. In this case I save the file in Folder D:\App.
Explanation of the code :
Partial Class MyForm : Inherits Form
...
End Class
To make a Windows Form Application, you have to inherit System.Windows.Forms.Form class. The class declared as “Partial”, because I'll create another Class MyForm as part of them. This partial class will tell you how the Form and Component displayed. And another class will handle all events occurred to the Class Component Object. So, we'll separate the “View” and “Controller” as MVC (Model View Controller) Design Pattern told us.
Private Sub InitializeComponent()
...
End Sub
In this case I create a Sub Procedure named InitializeComponent which is used to initialize the component that will be added into the form included the component settings. On the next sample I'll add some component like Label, TextBox, and Button into the Form.
Public Sub New()
...
End Sub
The constructor that will call the InitializeComponent procedure. So the Form will be displayed like we describe on InitializeComponent procedure. If you don't know what the “Constructor” is, just googling it
Public Shared Sub Main()
...
End Sub
Sub Main is the main procedure which is the starting point of your application. So, everything you've typed here would be read, compile and execute by the compiler, at startup application, before the others.
How To Compile
open the Command Prompt by clicking Start -> Run -> type cmd then press Enter.
When the Command Prompt's displayed, write down this below code to set the Work Path to be the same where .Net Framework was installed. By default it'll be installed in Folder "C:\Windows\Microsoft.Net\Framework\Vxxxx". Vxxxx is the version of your .Net Framework. In this tutorial I used .Net Framework v4.0, but it'd be running well on the other version.
path=%path%;c:\windows\microsoft.net\framework\v4.0.30319
Then change your directory into the Folder where you saved the File by type this code :
CD /D D:\App
And now it's time to compile it, just write down this code :
VBC /OUT:MyForm /T:winexe MyForm.Designer.vb
If no errors, then run your application by calling the name's of your application.
MyForm.exe
Or just :
MyForm
Adding Component Into Form
To add components into form, we could follow these steps :
1. Declare the component
2. Initialize and sets the component's property value
3. Add the component into the form
Now, let's see the codes. Open MyForm.Designer.vb file that we've created above. And update it to be like these :
Imports System.Windows.Forms
Partial Class MyForm : Inherits Form
'Component's Declaration
Friend WithEvents lblFirstName As Label
Friend WithEvents lblLastName As Label = New Label
Friend WithEvents txtFirstName As TextBox = New TextBox
Friend WithEvents txtLastName As TextBox = New TextBox
Friend WithEvents btnShow As Button = New Button
Private Sub InitializeComponent()
Me.Text = "My First Example Form"
'lblFirstName Setting
lblFirstName = New Label 'Object Initialization
lblFirstName.Text = "First Name : "
lblFirstName.AutoSize = True 'Set the label into AutoSize
lblFirstName.Location = New System.Drawing.Point(10,10) 'Set the location/position of the lblFirstName Object relative to the form System.Drawing.Point(x, y)
'lblLastName Setting
lblLastName = New Label
lblLastName.Text = "Last Name : "
lblLastName.AutoSize = True
lblLastName.Location = New System.Drawing.Point(10, 60)
'txtFirstName Setting
txtFirstName = New TextBox
txtFirstName.MaxLength = 50
txtFirstName.Size = New System.Drawing.Size(150, 40)
txtFirstName.Location = New System.Drawing.Point(100, 10)
'txtLastName Setting
txtLastName = New TextBox
txtLastName.MaxLength = 50
txtLastName.Size = New System.Drawing.Size(150, 40)
txtLastName.Location = New System.Drawing.Point(100, 60)
'btnShow Setting
btnShow = New Button
btnShow.Text = "&Show"
btnShow.Size = New System.Drawing.Size(50, 30)
btnShow.Location = New System.Drawing.Point(10, 100)
'Adding the control/component into the Form
Me.Controls.Add(lblLastName)
Me.Controls.Add(lblFirstName)
Me.Controls.Add(txtLastName)
Me.Controls.Add(txtFirstName)
Me.Controls.Add(btnShow)
Me.Size = New System.Drawing.Size(txtLastname.Right + 20, btnShow.Top + 70)
End Sub
Public Sub New()
InitializeComponent()
End Sub
Public Shared Sub Main()
Dim theForm As New MyForm
theForm.ShowDialog()
End Sub
End Class
Then save it.
Now to handle btnShow Click event, we'll create an event handler in another file. Create a new file on Notepad :
Imports System.Windows.Forms
Public Class MyForm
'This is the event handler
for btnShow.Click
Private Sub btnShow_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShow.Click
MessageBox.Show("Welcome " & txtFirstName.Text & " " & txtLastName.Text,"Welcome")
End Sub
End Class
Then save it as MyForm.vb in the same folder ( D:\App). Now let's compile it :
VBC /OUT:MyForm /T:winexe MyForm.vb MyForm.Designer.vb
If no errors, then run your application by calling the name's of your application.
MyForm
Hopefully the tutorial's clear enough for you... I'm so sorry if my english's not good enough. Next tutorial we'll see how to display the data from database (Ms.Access or SQL Server). Happy coding bro...






MultiQuote



|