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.
5.04 Apply Decision Making Structures
1.
Practical Programming COMP153-08S Lecture: Repetition Continued.
VB.NET User Interface Controls
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.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
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
Starting Out with Visual Basic.NET 2 nd Edition Chapter 4 Making Decisions and Working With Strings.
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.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
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)
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 Four The Selection Structure Programming with Microsoft Visual Basic th Edition.
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.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
© 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.
110 F-1 Decisions and Conditions Chapter 4: We can design a form We can calculate To make our programs more powerful, we need to be able to make choices.
Decisions with Select Case and Strings Chapter 4 Part 2.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
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.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
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,
5.04 Apply Decision Making Structures
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 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
String Variable, Methods and Properties
Decision Structures ISYS 350.
String Variable, Methods and Properties
Microsoft Visual Basic 2005: Reloaded Second Edition
Decision Structure - 1 ISYS 350.
String Variable, Methods and Properties
Decision Structure - 1 ISYS 350.
String Variable, Methods and Properties
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, Xor

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

Xor Truth table –T, T, F –T, F, T –F, T, T –F, F, F Example: Degree = “MBA”, Experience>20 years –Under qualified –Overqualified: Meet both conditions

Order of Evaluation ( ), Not, And, Or, Xor 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 40 to 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

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…

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

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

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

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.

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