VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
CS0004: Introduction to Programming Select Case Statements and Selection Input.
5.04 Apply Decision Making Structures
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
VB.NET User Interface Controls
.NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically. –Programming in.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
Programming Interface Controls with VB.Net. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option.
Input Validation Check the values entered into a text box before beginning any calculations Validation is a form of ‘self-protection’, rejecting bad data.
Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 4 Decisions and Conditions.
VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:
Chapter 5 new The Do…Loop Statement
I would like you to develop a mobile application that calculates for me the discount on an item I want to purchase. Input:  User enters in a number for.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Chapter 4: The Selection Structure
Tutorial 11 Using and Writing Visual Basic for Applications Code
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Multiple Forms and Standard Modules
Chapter 7 Decision Making. Class 7: Decision Making Use the Boolean data type in decision-making statements Use If statements and Select Case statements.
Programming Interface Controls with VB.Net. User Interface Controls Form MessageBox, InputBox Common Controls: –Button, TextBox, MaskedTextBox, List Box,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Five More on the Selection Structure.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Control Structures.  Need to have program statements that control execution flow  Simple statements to branch execution based on conditions (If/Then/Else)
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Decisions with Select Case and Strings Chapter 4 Part 2.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 11 So Many Paths … So Little Time.
Knowledge Base. Defining a Variable Dim statement Dim intXX As Integer Public in a Module Public dblNN As Double.
Creation of Variables with Numeric, alphanumeric, date, picture, memo data types Constant - A quantity that does not change during the execution of a program.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,
Computer Science Up Down Controls, Decisions and Random Numbers.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
VB.NET User Interface Controls. VB User Interface Objects Form InputBox, MessageBox Standard Controls: –TextBox, MaskedTextBox, List Box, Option Button,
A variable is a name for a value stored in memory.
Objective 7.04 Apply Built-in String Functions (3%)
Decision Structure - 1 ISYS 350.
CHAPTER FIVE Decision Structures.
Chapter 4: The Selection Structure
IS 350 Decision-making.
Programming Interface Controls
Programming Interface Controls
CHAPTER FIVE Decision Structures.
Decision Structure - 1 ISYS 350.
Chapter 5 The Do…Loop Statement
Part A – Doing Your Own Input Validation with Simple VB Tools
Decision Structures ISYS 350.
Decision Structure - 1 ISYS 350.
Decision Structure - 1 ISYS 350.
Programming Interface Controls
Chapter 4 Decisions and Conditions
Presentation transcript:

VB.Net Decisions

The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition: Comparison of two expressions formed with relational operators:>,, >=, <= Boolean variable –Complex condition: Formed with logical operators: ( ), Not, And, Or

Complex Condition If 12<=Age<=65 Then Fee = 20, else Fee = 5 Admission rules: Applicants will be admitted if meet one of the following conditions: –GPA>3.0 –GPA>2.5 AND SAT >700 Scholarship rules: Meet all the conditions: –GPA>3.2 –Must be Accounting or CIS Admission rules: Applicants will be admitted if meet all the following conditions: –SAT>700 Or Income > –Not GPA < 2.5

Order of Evaluation ( ), Not, And, Or Example: A=5, B=10, C=20, D=30 –A>B OR C 10) –A>B XOR C 10 OR B>A+C

Using Boolean Variables as Flags A flag is a Boolean variable that signals when some condition exists in the program. Dim goodStudent as Boolean=True If daysAbsent > 10 Then goodStudent = False End if If goodStudent Then TextBox1.text=“Good Student” Else textBox1.text=“Not good student” End If

IF … ELSEIF Statement IF condition THEN statements [ELSEIF condition-n THEN [elseifstatements] [ELSE [elsestatements]]] End If

Select Case Structure SELECT CASE testexpression [CASE expressionlist-n [Statements] [CASE ELSE [elsestatements] END SELECT

Select Case Example SELECT CASE temperature CASE <40 TextBox1.text=“cold” CASE < 60 Text Box1.text=“cool” CASE 60 to 80 TextBox1.text=“warm” CASE ELSE TextBox1.text=“Hot” End Select

The Expression list can contain multiple expressions, separated by commas. Select Case number Case 1, 3, 5, 7, 9 textBox1.text=“Odd number” Case 2, 4, 6, 8, 10 textBox1.text=“Even number” Case Else End Select

Nested If Decision tree: Example: Tuition rules based on student’s status and number of units: –Undergraduate student –Graduate student

Windows Controls Form InputBox, MessageBox Text Box, Radio Button, Check Box, GroupBox, etc.

Form Methods: –Me.show(), Me.Hide, Me.Close Events: –Load, Activated, Closing, Closed Modeless form: Other forms can receive input focus while this form remains active. Modal form: No other form can receive focus while this form remains active. –Formname.ShowDialog()

Multiple Forms Two forms: Form1, Form2 To Open Form2 from Form1: Dim f2 As New Form2() f2.Show() Open Form2 as a Modal form: f2.ShowDialog() Note: Form is defined as a class. Must create an instance of the form class by using the keyword New to access the form. Demo: Problem with the Show method.

SharingVariables Among Forms Define these variables with class-level scope using the Public keyword. –Must use formName to qualify variables. – –Dim f2 As New Form2() –TextBox1.Text = f2.g2 *** g2 is declared in form2 Better way: Define these variables with project- level scope in a module using the Public keyword: –Module Module1 – Public testVar As Integer –End Module –Note: Use Project/Add Windows form to add a module.

Modules A file contains code such as: –Variable declarations –Procedures and functions Variables and procedures used by more than one form should store in a module. Global variables: Public

Starting Application with Sub Main The Sub Main procedure must reside in a module. Choose Sub Main as startup object. The code in Main will execute. –Write code in Main to open forms.

Sub Main Example Public Sub main() Dim myDay As Date myDay = Today If myDay.DayOfWeek = DayOfWeek.Saturday Or myDay.DayOfWeek = DayOfWeek.Sunday Then Dim frmWeekEnd As New Form2() frmWeekEnd.ShowDialog() Else Dim frmWeekDay As New Form1() frmWeekDay.ShowDialog() End If End Sub

MsgBox MsgBox(prompt, other arguments) MsgBox can return a value representing the user’s choice of buttons displayed by the box. The return value has MsgBoxResult data type: –Dim RETurnVal As MsgBoxResult –RETuranVal = MsgBox("customer click cancel", MsgBoxStyle.AbortRetryIgnore) If RETurnVal = MsgBoxResult.Abort Then – MsgBox("YOU CLICK ABORT") – End If

MessageBox MessageBox.Show(message) MessageBox.Show(message, Caption) MessageBox.Show(message, Caption, Buttons) Note: 1. In each format, arguments are positional and required. 2. This object returns a DialogResult data type. To test the return value: Dim ReturnVal as DialogResult ReturnVal=MessageBox(“hello”, …..) If ReturnVal=DialogResult.OK…

Form Closing Event Example Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing If MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) = DialogResult.Yes Then e.Cancel = False Else e.Cancel = True End If End Sub

InputBox InputBox(Prompt [,Title] [, Default] [, Xpos] [, Ypos]) Xpos is the distance from the left edge of the screen, and Ypos is the distance from the top of the screen. Both are measured in twips (1/1440 th of an inch). Note: The arguments are positional and optional. Enter a comma to skip an argument. cityName = InputBox("Please enter city name:“,, “SF”) If cityName = vbNullString Then MessageBox.Show ("customer click cancel") Else Text1.Text = cityName End If Note: vbNullString is a VB keyword.

Text Box Useful properties –Locked: read only –Password Character –Multiline –ScrollBar –Text Useful events –TextChanged: default event –Validating

Input Validation Numbers are checked to ensure they are: –Within a range of possible values –Reasonableness –Not causing problems such as division by 0. –Containing only digits IsNumeric Textbox: –Set CauseValidation property to true. –Use the Validating event: Triggered just before the focus shifts to other control.

TextBox Validating Event Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating If Not IsNumeric(TextBox1.Text) Then e.Cancel = True MsgBox("enter digits only") Else MsgBox("good") End If End Sub Note: Why not use theTextChanged event?

Group Box and Panel Controls in a Group Box should move with the boxs. A panel control can display scrollbars by setting the AutoScroll property to true.

Radio Button Radio buttons must be grouped together inside a container such as a GroupBox or a form. When the user selects an option all other options in the same group are deselected. Checked property value: True/False. Default button: Set the Checked property to true at the design time.

RadioButton Example 1 Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged If RadioButton1.Checked = True Then MsgBox("Check RadioButton1") Else MsgBox("uncheck RadioButton1") End If End Sub

RadioButton Example 2 If radioButton1.Checked=true then textbox1.text=“You select radio button 1” ElseIf radioButton2.Checked=true then textbox1.text=“You select radio button 2” Else textbox1.text=“You select radio button 3” End If

Check Box Check boxes do not belong to a group even when they are grouped in a Group Box. Checked property and checkedChangedevent

Check Box Example 1 Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged If CheckBox1.Checked = True Then MsgBox(“check chk1") Else MsgBox("uncheck chk1") End If End Sub

Check Box Example 2 Dim msg as String Msg=“You choose “ If checkBox1.checked=true then msg=msg & “check box 1” End If If checkBox2.checked=true then msg=msg & “check box 2” End If If checkBox3.checked=true then msg=msg & “check box 3” End If

Working with Strings String comparison is case-sensitive: –Blank –Digits in numerical order –Uppercase letters in alphabetical order –Lowercase letters in alphabetical order “A” <> “a” “a” > “Z” “alan”, “Chao”, “Smith”

String Methods ToUpper, ToLower Length – Number of characters TrimStart, TrimEnd, Trim Substring(Start), Substring(Start, length) IndexOf(SearchString), IndexOf(SearchString, Start) –0 based index –Case-sensitive eName=“David” Position=eName.IndexOf(“d”) –Return –1 if the searchString is not found. Note: Text property of a Textbox has all the string methods. –Ex. TextBox1.Text.Substring(0,2)

Example: Extract the firstname and the lastname from a fullname Dim indexSpace As Integer Dim firstName, lastName As String indexSpace = TextBox1.Text.IndexOf(" ") firstName = TextBox1.Text.Substring(0, indexSpace) lastName = TextBox1.Text.Substring(indexSpace + 1) MessageBox.Show(firstName) MessageBox.Show(lastName)

Example: Validate SSN Format Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating Dim correct As Boolean = True If Not IsNumeric(TextBox1.Text.Substring(0, 3)) Or _ Not IsNumeric(TextBox1.Text.Substring(4, 2)) Or _ Not IsNumeric(TextBox1.Text.Substring(7, 4)) Then correct = False End If If TextBox1.Text.Substring(3, 1) <> "-" Or TextBox1.Text.Substring(6, 1) <> "-" Then correct = False End If If correct Then MsgBox("perfect format") Else e.Cancel = True MsgBox("not correct format") End If End Sub