Download presentation
Presentation is loading. Please wait.
1
VB.Net Decisions
2
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
3
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 > 40000 –Not GPA < 2.5
4
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
5
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
6
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
7
IF … ELSEIF Statement IF condition THEN statements [ELSEIF condition-n THEN [elseifstatements] [ELSE [elsestatements]]] End If
8
Select Case Structure SELECT CASE testexpression [CASE expressionlist-n [Statements] [CASE ELSE [elsestatements] END SELECT
9
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
10
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
11
Nested If Decision tree: Example: Tuition rules based on student’s status and number of units: –Undergraduate student –Graduate student
12
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…
13
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
14
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.
15
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.
16
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
17
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
18
Check Box Check boxes do not belong to a group even when they are grouped in a Group Box. Checked property and checkedChangedevent
19
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
20
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
21
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.
22
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
23
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”
24
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.
25
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.