Professional Documents
Culture Documents
Introduction To VB - Net 2
Introduction To VB - Net 2
Introduction To VB - Net 2
NET
COSC 226
TOPIC: Introduction to Visual Basic .NET
Required Text/Software:
What is VB.NET?
Features of VB.Net
The .NET Framework
Microsoft Visual Studio.NET environment.
VB.NET – Program Structure
What is VB.NET
Visual Basic .NET (VB.NET) is an object-oriented computer
programming language implemented on the .NET Framework.
COSC 226
TextBox1.Width = 150
Control Methods
• A method is a procedure created as a member of a class and they cause
an object to do something.
• Methods are used to access or manipulate the characteristics of an object
or a variable.
• There are mainly two categories of methods you will use in your classes:
• If you are using a control such as one of those provided by the Toolbox, you can call
any of its public methods. The requirements of such a method depend on the class
being used.
• If none of the existing methods can perform your desired task, you can add a method
to a class.
End Sub
Basic Controls
• VB.Net provides a huge variety of controls that help you to create rich user interface.
• Microsoft Visual Studio .NET controls are the graphical tools you use to
build the user interface of a VB.Net program.
• Labels are one of the most frequently used Visual Basic control.
• A Label control lets you place descriptive text , where the text does not
need to be changed by the user.
• Click Label in the Toolbox and drag it over the forms Designer and drop it
in the desired location.
Label Control
• If you want to change the display text of the Label, you have to set a new
text to the Text property of Label.
• You can load Image in Label control , if you want to load an Image in the
Label control you can code like this
Label1.Image = Image.FromFile("C:\testimage.jpg")
Label Control
• The following source code shows how to set some properties of the Label
through coding.
Button Control
• The Button control represents a standard button that reacts to a Click event.
A Button can be clicked by using the mouse, ENTER key, or SPACEBAR if
the button has focus.
Button Control
• When you want to change display text of the Button , you can change the
Text property of the button.
• Similarly if you want to load an Image to a Button control , you can code
like this.
Button1.Image = Image.FromFile("C:\testimage.jpg")
Textbox Control
• VB.Net programmers make extensive use of the TextBox control to let the
user view or enter large amount of text.
• A text box object is used to display text on a form or to get user input while
a VB.Net program is running.
• In a text box, a user can type data or paste it into the control from the
clipboard.
Textbox Control
• For displaying a text in a TextBox control , you can code like this.
• You can also collect the input value from a TextBox control to a variable
like this way.
var = TextBox1.Text
Textbox Control Properties
• The below code set a textbox width as 150 and height as 25 through source
code.
TextBox1.Width = 150
TextBox1.Height = 25
Textbox Control Properties
• Background Color and Foreground Color
• You can set Textbox background color and foreground color through
property window, also you can set it programmatically.
TextBox1.BackColor = Color.Red
TextBox1.ForeColor = Color.Gray
• TextBox BorderStyle
• You can set 3 different types of border style for textbox in vb.net, they are
None, FixedSingle and fixed3d.
TextBox1.BorderStyle = BorderStyle.FixedSingle
Textbox Control Properties
• VB.Net Textbox Maximum Length
• Maximum Length property sets the maximum number of characters or
words the user can input into the text box control. That means you can limit
the user input by this property.
TextBox1.MaxLength = 25
• How to ReadOnly Textbox
• when a program wants to prevent a user from changing the text that appears
in a text box, the program can set the controls Readonly property is to True.
TextBox1.ReadOnly = True
Textbox Control Properties
• Multiline TextBox in vb.net
• By default TextBox accept single line of characters , If you need to enter
more than one line in a TextBox control, you should change the Multiline
property is to True.
• TextBox1.Multiline = True
• Textbox passowrd character
• Sometimes you want a textbox to receive password from the user. In order
to keep the password confidential, you can set the PasswordChar property
of a textbox to a specific character.
TextBox1.PasswordChar = "*“
Textbox Control Properties
• The above code set the PasswordChar to * , so when the user enter
password then it display only * instead of typed characters.
• How to Newline in TextBox
• You can add new line in a textbox using two ways.
TextBox1.Text += "some text here" + “\r\n”
• Or
TextBox1.Text += "some text here" + Environment.NewLine
Textbox Control Properties
• How to retrieve integer values from textbox ?
• VB.Net String to Integer conversion
Dim i As Integer
i = Integer.Parse(TextBox1.Text)
• Parse method Converts the string rep conversion presentation of a number
to its integer equivalent.
• VB.Net String to Double
Dim dbl As Double
dbl = Double.Parse(TextBox1.Text)
Textbox Control Events
• TextBox Keydown event
• Keydown event occurs when a key is pressed while the control has focus.
• TextChanged Event
• TextChanged Event is raised if the Text property is changed by either
through program modification or user input.
ComboBox Control
• The ComboBox control lets the user choose one of several choices.
• The user can type a value in the text field or click the button to display a
drop down list.
• In addition to display and selection functionality, the ComboBox also
provides features that enable you to efficiently add items to the ComboBox.
ComboBox Control
• Add item to combobox
ComboBox1.Items.Add("Sunday")
ComboBox1.Items.Add("Monday")
ComboBox1.Items.Add("Tuesday")
ComboBox Control
• How to set the selected item in a comboBox
• You can set which item should shown while it displaying in the form for
first time.
comboBox1.Items.Add("test1")
comboBox1.Items.Add("test2")
comboBox1.Items.Add("test3")
comboBox1.SelectedItem = "test3"
• or
ComboBox1.SelectedItem = ComboBox1.Items(1)
• or
comboBox1.SelectedIndex = comboBox1.FindStringExact("test3")
ComboBox Selected Item
• How to retrieve value from ComboBox
• If you want to retrieve the displayed item to a string variable , you can code
like this
Dim var As String
var = ComboBox1.Text
Or
Dim item = Me.comboBox1.GetItemText(Me.comboBox1.SelectedItem)
MessageBox.Show(item)
ComboBox Selected Item
• How to remove an item from ComboBox in VB.Net
• You can remove items from a combobox in two ways. You can remove item
at a the specified index or giving a specified item by name.
ComboBox1.Items.RemoveAt(1)
• The above code will remove the second item from the combobox.
ComboBox1.Items.Remove("Friday")
• The above code will remove the item "Friday" from the combobox.
ComboBox DropDown Style
• The DropDownStyle property specifies whether the list is always displayed
or whether the list is displayed in a drop down.
• The DropDownStyle property also specifies whether the text portion can be
edited.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
ComboBox DropDown Style
• The DropDownStyle property specifies whether the list is always displayed
or whether the list is displayed in a drop down.
• The DropDownStyle property also specifies whether the text portion can be
edited.
ComboBox1.DropDownStyle = ComboBoxStyle.DropDown
The Development Process
of Wage Calculator Application
Step 1 of Developing an Application
• Start Visual Studio and create the forms and other controls identified in
Step 3
– This is the first use of Visual Studio
All of the previous steps have just been on paper
– In this step you create the application’s user interface and arrange the
controls on each from.
This is the portion of the application the user will see
Step 9 of Developing an Application
• Use the flowcharts or pseudocode from step 6 to write the actual code
– This is the second step on the computer
• In this step you develop the methods behind the click event for each
button
– Unlike the form developed on step 8, this portion of the application is
invisible to the user
Step 10 of Developing an Application
• Attempt to run the application - find syntax errors
– Correct any syntax errors found
– Syntax errors are the incorrect use of an element of the programming
language
– Repeat this step as many times as needed
– All syntax errors must be removed before the program will run
Step 11 of Developing an Application
• Run the application using test data as input
– Run the program with a variety of test data
– Check the results to be sure that they are correct
– Incorrect results are referred to as a runtime error
• Correct any runtime errors found
• Repeat this step as many times as necessary
Thank you