Professional Documents
Culture Documents
Public Class: "We Have A Winner"
Public Class: "We Have A Winner"
Common Properties
-
Name
Font
Enabled
Size :- Sets or returns the controls size. The size property is Size object, which
exposes two properties, the Height and Width properties.
Tag : Holds some data you want to associate with a specific control. For example, you
can set the
Tag property to the controls default value, so that you can restore the controls
default value if the
user supplies invalid data.
Text : The text (a string) that appears on the control.
TabStop : The TabStop property determines whether the control belongs to the socalled tab order. If True (which is the default value), you can move the focus to the
con-trol with the Tab key. If False, the control will be skipped in the tab order.
TabIndex : A numeric value that determines the position of the control in the Tab
order.
Visible
Click
DoubleClick
Enter Fired when the control received the focus.
Leave Fired when the control loses the focus.
MouseEnter Fired when the mouse pointer enters the area of the control.
MouseLeave
XXXChanged These events include BackColorChanged, FontChanged,
VisibleChanged, and many more.
Even a num-ber is an object and exposes a few properties and methods of its own.
STRING
Length
ToUpper
ToLower
Console.WriteLine("Visual Basic".Length)
Console.WriteLine("Visual Basic".ToUpper)
Console.WriteLine("Visual Basic".ToLower)
Focus. This method moves the focus to the control to which the method applies.
TextBox1.Focus()
Private Sub TextBox1_Leave(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox1.Leave
If Not IsNumeric(TextBox1.Text) Then TextBox1.Focus()
End Sub
Clear
Hide/Show. The two methods are equivalent to setting the Visible property to True and
False respectively.
PerformClick Its rather common to invoke the Click event of a button from within our code.
Scale The following statement scales the TextBox1 control down to 75 percent of its
current size:
TextBox1.Scale(0.75)
Module Module1
Sub Main()
End Sub
End Module
Pmt() is a built-in function that uses the five values in the parentheses to calculate the
monthly payment. The last two arguments of the Pmt() function are optional.
Select the check box with the mouse (if its not already selected), and in the Properties window, locate the CheckAlign property youll see that this property has many different settings
and each setting is shown as a square. Select the square in the middle row, the right
column. The string MiddleRightwill appear in the propertys box when you click the
appropriate button.
Single : Single precision floating point number.
Financial functions in VB
IsNumeric() is another built-in function that accepts avariable and returns True if the variable
is a number, False otherwise.
Private Sub Digit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles bttn1.Click, bttn2.Click, _
bttn3.Click, bttn4.Click, bttn5.Click, bttn6.Click, _
bttn7.Click, bttn8.Click, bttn9.Click, bttn0.Click
TextBox).Text
sender.Text directly
End Sub
dont
use
DateDiff() function, which contains the difference between two dates. The simplest syntax of
this function is
DateDiff(interval, date1, date2)
Console.WriteLine(DateDiff(DateInterval.Day, #1/1/2000#, #1/2/2000#))
The value printed on the Output window is 1
If lblDisplay.Text < 0 Then
MsgBox(Cant calculate the square root of a negative number)
Else
lblDisplay.Text = Math.Sqrt(Val(lblDisplay.Text))
End If
All math functions are part of the Math class; thats why theyre prefixed by the name of the
class. You can also import the Math class to the project with the following statement and
thus avoid pre-fixing the math functions:
Imports System.Math
The program will attempt to perform the calculations, which are coded in the statements
block. If it succeeds, it continues with the clean-up statements. These statements are mostly
clean-up code, and the Finally section of the statement is optional. If missing, the program
execution continues with the statement following the End Try statement. If an error occurs in the
first block of statements, then the Catch Exception section is activated and the statements in the
handler block are executed.
MsgBox(exc.Message)
lblDisplay.Text= ERROR
Finally
clearDisplay = True
End Try
End Sub
Drawback???
Solution
Public Class CalculatorsForm Inherits System.Windows.Forms.Form
Dim calcForm As New CalculatorForm()
Dim loanForm As New loanForm()
End Sub
The following statement, for instance, will create three Integer variables:
Dim width, depth, height As Integer
The following statement will create three Integer and two Double variables:
Dim width, depth, height As Integer, area, volume As Double
Not only can you declare a variable with the Dim statement, you can also initialize it by
assigning a value of the proper type to it:
Dim width As Integer = 9
Dim distance As Integer = 100, time As Single = 9.09
To declare a variable, use the Dim statement followed by the variables name, the As
keyword, and its type, as follows:
Dim meters As Integer
VARIABLE-NAMING CONVENTIONS
A variables name:
The simplest method of creating a new Pen is to specify its color. The fol-lowing statement
declares a Pen object that will draw in a blue-green color:
Dim myPen As Pen = New Pen(Color.AquaMarine)
You can also create variables of type Color. The following statement creates two variables
that rep-resent a different color each (one will be used as the background and the other as
the drawing color):
Dim bgColor As Color = Color.LightYellow, fgColor As Color = Color.Blue
TYPES OF VARIABLES
Visual Basic recognizes the following five categories of variables:
Numeric
String
Boolean
Date
Object
NUMERIC
Decimal, Single, and Double are the three basic data types for storing floating-point
numbers. The Double type can represent these numbers more accurately than the Single
type, and its used almost exclusively in scientific calculations. The integer data types store
whole numbers.
o
Short(Int16) :2 bytes
o
o
o
o
o
Integer(Int32): 4 bytes
Long(Int64): 8 bytes
Single: 4 bytes
Double: 8 bytes
Decimal: 16 bytes
Each numeric data type exposes the MinValue and MaxValue properties, which return the
minimum and maximum values that can be represented by the corresponding data type.
Console.WriteLine(Integer.MaxValue)
Console.WriteLine(Int32.MinValue)
DECIMAL
If you write financial applications, use the Decimal data type and round the amounts to two
decimal digits at the end.
VB.NET can represent two very special values, which may not be numeric values themselves
but are produced by numeric calculations: NaN (not a number) and Infinity.
12/0
infinity
0/0
NaN
To find out whether the result of an operation is a NaN or Infinity, use the IsNaN and IsInfinity
methods of the Single and Double data type. The Integer data type doesnt support these
methods, even though its possible to generate Infinity and NaN results with Integers. If the
IsInfinity method returns True, you can further examine the sign of the Infinity value with the
IsNegativeInfinity and IsPositiveInfinity methods.
Dim var1, var2 As Double
Dim result As Double
var1 = 0
var2 = 0
result = var1 / var2
If result.IsInfinity(result) Then
If result.IsPositiveInfinity(result) Then
MsgBox("Encountered a very large number. Cant continue")
Else
MsgBox("Encountered a very small number. Cant continue")
End If
Else
If result.IsNaN(result) Then
MsgBox("Unexpected error in calculations")
Else
MsgBox("The result is " & result.ToString)
End If
End If
IsInfinity method is a member of the System.Doubleclass.
Console.WriteLine((CInt(A) + CInt(B)))
The CInt() function converts its argument to an Integer value.
BOOLEAN VARIABLES
Boolean variables are, in essence, integers that take the value 1 (for True) and 0 (for False).
Actually, any non-zero value is considered True.
STRING
You can store nearly 2 GB of text in a string vari-able
The difference between these two variables
Dim aNumber As Integer = 25000
Dim aString As String = 25,000
is that they hold different values. The aStringvariable holds the characters 2, 5, ,, 0,
0, and 0, and aNumberholds a single numeric value.
CHARACTER VARIABLES
Character variables store a single Unicode character in two bytes. In effect, characters are
unsigned short integers (UInt16); you can use the CChar() function to convert integers to
characters, and the CInt() function to convert characters to their equivalent integer values.
You can initialize a character variable by assigning either a character or a string to it. In the
latter case, only the first character of the string is assigned to the variable.
Dim char1 As Char = a, char2 As Char = ABC
Console.WriteLine(char1)
Console.WriteLine(char2)
System.Char.IsLetterOrDigit(ch)
System.Char.Chars(i).IsLetterOrDigit(password.Chars(i))
DATE VARIABLES
A variable declared as Date can store both date and time values with a state-ment like the
following:
Dim expiration As Date
expiration = #01/01/2004#
expiration = #8/27/2001 6:29:11 PM#
expiration = July 2, 2002
expiration = Now()
If the Strict option is on, you cant specify dates using the long date format (as in the third
statement of this example).
To change the default behavior, you must insert the following statement at the beginning of
the
file, above the Imports statements:
Option Explicit Off
The Option Explicitstatement must appear at the very beginning of the file. This setting
affects
the code in the current module, not in all files of your project or solution.
By setting the Explicit option to Off, youre telling VB that you intend to use variables
without
declaring them. As a consequence, VB cant make any assumption as to the variables type,
so it uses
a generic type of variable that can hold any type of information. These variables are called
Object
variables, and theyre equivalent to the old variants.
var1 = Thank you for using Fabulous Software
var2 = 49.99
The following statements print the types shown below
each statement, in bold:
Console.WriteLine(Variable var1 is & var1.GetType().ToString)
Variable var1 is System.String
Console.WriteLine(Variable var2 is & var2.GetType().ToString)
Variable var2 is System.Double
var1 = 49.99
var2 = Thank you for using Fabulous Software
If you execute the previous Print statements again, youll see that the types of the variables
have
changed.
With the Strict option set to Off, you can use a string
variable that holds a number in a numeric calculation:
Dim a As String = 25000
Console.WriteLine(a / 2)
The last statement will print the value 12500 on the Output window. Likewise, you can use
numeric variables in string calculations:
Dim a As Double = 31.03
a = a + 1
When the Strict option is set to On, the compiler doesnt disallow allimplicit conversions
between data types.
Moreover, with Option Strict On, you cant late-bind an expression. Late bindingmeans to call
a
method or a property of an object, but not be able to resolve this call at design time.
Dim myPen As Pen
myPen = New Pen(Color.Red)
myPen.Width = 2
This is early binding.
Dim objPen As Object
objPen = New Pen(Color.Red)
objPen.Width = 2
This is called late binding, and it will work only if the Strict option is turned off.