Gui Unit 1
Gui Unit 1
3. Languages
The types of applications that can be built in the .Net framework is classified broadly into
the following categories.
WinForms – This is used for developing Forms-based applications, which would
run on an end user machine. Notepad is an example of a client-based application.
ASP.Net – This is used for developing web-based applications, which are made to
run on any browser such as Internet Explorer, Chrome or Firefox.
o The Web application would be processed on a server, which would have
Internet Information Services Installed.
o Internet Information Services or IIS is a Microsoft component which is used
to execute an Asp.Net application.
o The result of the execution is then sent to the client machines, and the
output is shown in the browser.
ADO.Net – This technology is used to develop applications to interact with
Databases such as Oracle or Microsoft SQL Server.
The Common Language Specification (CLS) was introduced by Microsoft to make it easier
for language developers to adapt their languages to be compatible with .NET. This means
that C, C++, C# and Java can be easily made to be compatible with .NET environment.
MSIL
Microsoft Intermediate Language (MSIL) is a language used as the output of a number of
compilers (C#, VB, .NET, and so forth). It is a set of indeoendent instructions that are
generated by language compiler when the project is compiled. MSIL code is not executable
but can be processed by CLR before it becomes executable. The advantages of MSIL are as
follows:
1. MSIL provides language interoperability, as code in any .NET language is compiled on
MSIL.
2. Same performance in all .NET languages.
3. It support different runtime environments.
4. JIT compiler in CLR converts MSIL code into native machine code which is executed by
OS.
EVENT-DRIVEN PROGRAMMING
Event-driven programming is a programming paradigm in which the flow of program
execution is determined by events - for example a user action such as a mouse click, key
press, or a message from the operating system or another program. An event-driven
application is designed to detect events as they occur, and then deal with them using an
appropriate event-handling procedure.
A visual programming IDE such as VB.Net provides much of the code for detecting events
automatically when a new application is created. The programmer can therefore
concentrate on issues such as interface design, which involves adding controls such as
command buttons, text boxes, and labels to standard forms (a form represents an
application's workspace or window). Once the user interface is substantially complete, the
programmer can add event-handling code to each control as required.
Many visual programming environments will even provide code templates for event-
handlers, so the programmer only needs to provide the code that defines the action the
program should take when the event occurs. Each event-handler is usually bound to a
specific object or control on a form. Any additional subroutines, methods, or function
procedures required are usually placed in a separate code module, and can be called from
other parts of the program as and when needed.
The most common event are Click, DoubleClick, KeyPress, KeyDown, MouseMove,
MouseDrag etc.
2.The Toolbars
Many toolbars are available within the IDE, including Formatting, Image Editor, and Text
Editor, which you can add to and remove from the IDE through the View ➪ Toolbars menu
option. Just beneath the Menu Editor is a toolbar. The toolbar contains frequently accessed
functionality that is a subset of what is available via menu. Each one provides quick access
The toolbar is segmented into groups of related options, which are separated by vertical
bars:
➤ The first six icons provide access to the commonly used project and file manipulation
options available through the File and Project menus, such as opening and saving files.
➤ The next group of icons is for editing (Cut, Copy, and Paste). The next icon is for finding
and replacing items in your code.
➤ The third group of icons is used for commenting out and un-commenting sections of
code. This can be useful in debugging when you want to comment out a section of code to
determine what results the program might produce by not executing those lines of code.
➤ The fourth group of icons is for undoing and redoing edits and for navigating through
your code.
➤ The fifth group of icons provides the ability to start (via the green triangle), pause, and
stop your application. You can also use the last three icons in this group to step into your
code line by line, step over entire sections of code, and step out of a procedure. These
icons will be covered in depth in Chapter 10.
➤ The final group of icons provides quick links to the Solution Explorer, Properties
window, Object Browser, Toolbox, Error List, ExtensionManager, and the Immediate
window. If any of these windows is closed, clicking the appropriate icon will bring it back
into view.
3.THE TOOLBOX
The Toolbox is a palette of developer objects, or
controls, that are placed on forms or web pages, and then code
is added to allow the user to interact with them. An example
would be TextBox, Button and ListBox controls. With these
three controls added to a Windows Form object the developer
could write code that would take text, input by the application
user, and added to the ListBox after the button was clicked.
The toolbox is accessed through the view -> Toolbox
menu option, by clicking the Toolbox icon on the standard
menu bar,or by pressing Ctrl+Alt+X. Alternatively, the toolbox
tab is displayed on the left of the IDE; hovering your mouse
over this tab will cause the Toolbox window to fly out ,
partially covering your form.
You can also add controls by clicking on the space
available on the toolbox and then select component menu.A
dialog box will appear list all the available component that is
installed in your system. Click on the box which is available
before the component name,then select OK. A new component will appear on the toolbox.
5. PROPERTIES WINDOW
The properties windows shows all the control (like textbox)
properties to be change at design time. Most of this
properties can be change at run time with some code, but
basically most of this properties change the way the control
is display on your application. Every control has their own
properties, so according to the control you can change its
properties. Some of the common properties of a control are
name, visible, enable, font, forecolor, backcolor etc.
4. Instance Variables − Each object has its unique set of instance variables. An object's
state is created by the values assigned to these instance variables.
Imports System
Module Program
'It will display a string on the console
Sub Main(args As String())
Console.WriteLine("Welcome to VB.NET Tutorial.")
End Sub
End Module
VARIABLES
In visual basic, as in any other programming language, variables store values
during a program’s language execution. A variable has a name and a value. The variable’s
username, for example, might have the value Joe, and the variable Discount might have the
value 0.35.UserName and Discount are variable Names, and Joe and 0.35 and their values .
Joe is a string (that is,text), and 0.35 is a numeric value . When a variable’s value is a
string, it must be enclosed in double quotes.
DECLARING VARIABLES
When programming in VB 2010, you should declare your variables because this is
the default mode. If you attempt to use undeclared variables in your code, VB 2010 will
throw an exception. It is possible to change the default behavior and use undeclared
variables. To declare variables use the Dim statement and the As keyword,
Dim meters As integer
Dim greetings As string
The first variable, meters, will store integers, such as 3 or 1,002; the second
variable, greetings will store text. You can declare multiple variables of the same or
different type in the same line, as follows:
Dim Qty As Integer , Amount As Decimal, Volume As String
Dim Length,Width, Height As Integer, Volume, Area As Double
VARIABLE INITIALIZATION
Visual basic allows you to initialize variables in the same lines that declare them.
The following statement declares an integer variable and immediately places in the value
3,045 in it:
Dim distance As integer = 3045
This statement is equivalent to the following two:
Dim Distance As integer
Distance = 3045
IDENTIFIERS
As the name defines, an identifier is used to identify the name of variable, function, class,
or any other user-defined elements in the program. An identifier should be the
combination of letter, digit, and underscore. Still, the first character of the identifier or
variable name should start with alphabet letter or underscore (_) of any length.
There are various rules for identifier in VB.NET, as follows:
KEYWORDS
A keyword is a reserved word with special meanings in the compiler, whose meaning
cannot be changed. Therefore, these keywords cannot be used as an identifier in VB.NET
programming such as class name, variable, function, module, etc.
Numeric Variables
All programming languages provide a variety of numeric data types, including the
following:
◆ Integer (there are several Integer data types)
◆ Decimal
◆ Single (floating-point numbers with limited precision)
◆ Double (floating-point numbers with extreme precision)
Decimal, Single, and Double are the three basic data types for storing floating-point
numbers (numbers with a fractional part).
Integer Variables
There are three types of variables for storing integers, and they differ only in the range of
numbers each can represent. As you understand, the more bytes a type takes, the larger
values it can hold. The type of Integer variable you’ll use depends on the task at hand. You
should choose the type that can represent the largest values you anticipate will come up in
your calculations. You can go for the Long type, to be safe, but Long variables take up four
times as much space as Short variables and it takes the computer longer to process them.
Single- and Double-Precision Numbers
Boolean Variables
The Boolean data type stores True/False values. Boolean variables are, in essence,
integers that take the value –1 (for True) and 0 (for False). Actually, any nonzero value is
considered True. Boolean variables are declared as
Dim failure As Boolean
String Variables
The String data type stores only text, and string variables are declared as follows:
Dim someText As String
Character Variables
Character variables store a single Unicode character in two bytes. To declare a Character
variable, use the Char data type:
Date Variables
Date variables store date values that may include a time part (or not), and they are
declared with the Date data type:
Dim expiration As Date
Type Characters
In addition to specifying a data type in a declaration statement, you can force the data type
of some programming elements with a type character. The type character must
immediately follow the element, with no intervening characters of any kind.
Visual Basic supplies a set of identifier type characters that you can use in a declaration to
specify the data type of a variable or constant. The following table shows the available
identifier type characters with examples of usage.
______________________________________________________________________________________________________
Type Character Description Example
CONSTANTS
The constants refer to fixed values that the program may not alter during its execution.
These fixed values are also called literals. Constants can be of any of the basic data types
like an integer constant, a floating constant, a character constant, or a string literal. There
are also enumeration constants as well.
The constants are treated just like regular variables except that their values cannot be
modified after their definition.
Declaring Constants
In VB.Net, constants are declared using the Const statement. The Const statement is used
at module, class, structure, procedure, or block level for use in place of literal values.
The syntax for the Const statement is:
Const constantname [ As datatype ] = initializer
Const : keyword for const
constantname: specifies the name of the constant
datatype: specifies the data type of the constant
initializer: specifies the value assigned to the constant
For example,
'The following statements declare constants.'
Const maxval As Long = 4999
Public Const message As String = "HELLO"
Private Const piValue As Double = 3.1415
OPERATORS IN VB.NET
Arithmetic Operators
Following table shows all the arithmetic operators supported by VB.Net. Assume
variable A holds 2 and variable B holds 7, then:
Show Examples
Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands A + B will give 9
- Subtracts second operand from the first A - B will give -5
* Multiplies both operands A * B will give 14
/ Divides one operand by another and returns a floating B / A will give 3.5
point result
\ Divides one operand by another and returns an B \ A will give 3
integer result
MOD Modulus Operator and remainder of after an integer B MOD A will give 1
division
Comparison Operators
Following table shows all the comparison operators supported by VB.Net. Assume
variable A holds 10 and variable B holds 20, then:
Operator Description Example
= Checks if the values of two operands are equal or not; (A = B) is not
if yes, then condition becomes true. true.
<> Checks if the values of two operands are equal or not; (A <> B) is true.
if values are not equal, then condition becomes true.
> Checks if the value of left operand is greater than the (A > B) is not
value of right operand; if yes, then condition becomes true.
true.
< Checks if the value of left operand is less than the (A < B) is true.
value of right operand; if yes, then condition becomes
true.
>= Checks if the value of left operand is greater than or (A >= B) is not
equal to the value of right operand; if yes, then true.
condition becomes true.
<= Checks if the value of left operand is less than or equal (A <= B) is true.
to the value of right operand; if yes, then condition
becomes true.
Logical operators
BCA405-GUI Programming: Govt. Zirtiri Residential Science College
Page 15
Following table shows all the logical operators supported by VB.Net. Assume variable A
holds Boolean value True and variable B holds Boolean value False, then:
Operator Description Example
And It is the logical as well as bitwise AND operator. If both the (A And B) is
operands are true, then condition becomes true. This operator False.
does not perform short-circuiting, i.e., it evaluates both the
expressions.
Or It is the logical as well as bitwise OR operator. If any of the two (A Or B) is
operands is true, then condition becomes true. This operator True.
does not perform short-circuiting, i.e., it evaluates both the
expressions.
Not It is the logical as well as bitwise NOT operator. Use to reverses Not(A And B)
the logical state of its operand. If a condition is true, then Logical is True.
NOT operator will make false.
Xor It is the logical as well as bitwise Logical Exclusive OR operator. A Xor B is
It returns True if both expressions are True or both expressions True.
are False; otherwise it returns False. This operator does not
perform short-circuiting, it always evaluates both expressions
and there is no short-circuiting counterpart of this operator.
AndAlso It is the logical AND operator. It works only on Boolean data. It (A AndAlso
performs short-circuiting. B) is False.
OrElse It is the logical OR operator. It works only on Boolean data. It (A OrElse B)
performs short-circuiting. is True.
IsFalse It determines whether an expression is False.
IsTrue It determines whether an expression is True.
Bitwise Operators Assume that the variable A holds 60 and variable B holds 13, then:
Operator Description Example
And Bitwise AND Operator copies a bit to the result if it (A AND B) will give 12,
exists in both operands. which is 0000 1100
Or Binary OR Operator copies a bit if it exists in either (A Or B) will give 61, which
operand. is 0011 1101
Xor Binary XOR Operator copies the bit if it is set in (A Xor B) will give 49,
one operand but not both. which is 0011 0001
Not Binary Ones Complement Operator is unary and (Not A ) will give -61,
has the effect of 'flipping' bits. which is 1100 0011 in 2's
complement form
<< Binary Left Shift Operator. The left operands value A << 2 will give 240, which
is moved left by the number of bits specified by the is 1111 0000
right operand.
>> Binary Right Shift Operator. The left operands A >> 2 will give 15, which is
value is moved right by the number of bits 0000 1111
specified by the right operand.
Assignment Operators
There are following assignment operators supported by VB.Net:
Operator Description Example
= Simple assignment operator, Assigns values from right C = A + B will assign
BCA405-GUI Programming: Govt. Zirtiri Residential Science College
Page 16
side operands to left side operand value of A + B into C
+= Add AND assignment operator, It adds right operand to C += A is equivalent
the left operand and assigns the result to left operand to C = C + A
-= Subtract AND assignment operator, It subtracts right C -= A is equivalent
operand from the left operand and assigns the result to to C = C - A
left operand
*= Multiply AND assignment operator, It multiplies right C *= A is equivalent
operand with the left operand and assigns the result to to C = C * A
left operand
/= Divide AND assignment operator, It divides left operand C /= A is equivalent
with the right operand and assigns the result to left to C = C / A
operand (floating point division)
\= Divide AND assignment operator, It divides left operand C \= A is equivalent
with the right operand and assigns the result to left to C = C \A
operand (Integer division)
^= Exponentiation and assignment operator. It raises the C^=A is equivalent
left operand to the power of the right operand and to C = C ^ A
assigns the result to left operand.
<<= Left shift AND assignment operator C <<= 2 is same as C
= C << 2
>>= Right shift AND assignment operator C >>= 2 is same as C
= C >> 2
&= Concatenates a String expression to a String variable or Str1 &= Str2 is
property and assigns the result to the variable or same as
property. Str1 = Str1 & Str2
Miscellaneous Operators
There are few other important operators supported by VB.Net.
Operator Description Example
AddressOf Returns the address of a AddHandler Button1.Click,
procedure. AddressOf Button1_Click
Await It is applied to an operand in
an asynchronous method or Dim result As res
lambda expression to = Await
suspend execution of the AsyncMethodThatReturnsResult()
method until the awaited task Await AsyncMethod()
completes.
GetType It returns a Type object for MsgBox(GetType(Integer).ToString())
the specified type. The Type
object provides information
about the type such as its
properties, methods, and
events.
Function It declares the parameters Dim add5 = Function(num As
Expression and code that define a Integer) num + 5
function lambda expression. 'prints 10
Console.WriteLine(add5(5))
Operator Precedence
Await Highest
Exponentiation (^)
Unary identity and negation (+, -)
Multiplication and floating-point division (*, /)
Integer division (\)
Modulus arithmetic (Mod)
Addition and subtraction (+, -)
Arithmetic bit shift (<<, >>)
All comparison operators (=, <>, <, <=, >, >=, Is, IsNot, Like, TypeOf...Is)
Negation (Not)
Conjunction (And, AndAlso)
Inclusive disjunction (Or, OrElse)
Exclusive disjunction (Xor) Lowest
STATEMENT IN VB.NET
A statement is a complete instruction in Visual Basic programs. It may contain keywords,
operators, variables, literal values, constants and expressions.
Statements could be categorized as:
Declaration statements - these are the statements where you name a variable,
constant, or procedure, and can also specify a data type.
Executable statements - these are the statements, which initiate actions. These
statements can call a method or function, loop or branch through blocks of code or
assign values or expression to a variable or constant. In the last case, it is called an
Assignment statement.
Declaration Statements
The declaration statements are used to name and define procedures, variables,
properties, arrays, and constants. When you declare a programming element, you can
also define its data type, access level, and scope.
Executable Statements
An executable statement performs an action. Statements calling a procedure, branching
to another place in the code, looping through several statements, or evaluating an
expression are executable statements. An assignment statement is a special case of an
executable statement.
Example
The following example demonstrates a decision making statement:
Module decisions
Sub Main()
'local variable definition '
Dim a As Integer = 10
' check the boolean condition using if statement '
If (a < 20) Then
' if condition is true then print the following '
Console.WriteLine("a is less than 20")
End If
Console.WriteLine("value of a is : {0}", a)
Console.ReadLine()
End Sub
BCA405-GUI Programming: Govt. Zirtiri Residential Science College
Page 20
End Module
When the above code is compiled and executed, it produces the following result:
a is less than 20;
value of a is : 10
Lifetime of a Variable : It is the period for which they retain their value. Variables
declared as Public exist for the lifetime of the application. Local variables, declared within
procedures with the Dim or Private statement, live as long as the procedure.
You can force a local variable to preserve its value between procedure calls with the Static
keyword. The advantage of using static variables is that they help you minimize the
number of total variables in the application.
Variables declared in a Form outside any procedure take effect when the Form is loaded
and cease to exist when the Form is unloaded. If the Form is loaded again, its variables are
initialized, as if it’s being loaded for the first time.
One of the tasks performed by the application is to track the average of the numeric
values. Instead of adding all the values each time the user adds a new value and dividing
by the count, you can keep a running total with the function RunningAvg(), which is shown
below.