inputbox( ) function - university of babylon · 1 visual basic 6 programming: lecture 6...

10
1 Visual Basic 6 Programming: Lecture 6 ﺍﻟﻣﺣﺎﺿﺭﺓ ﺍﻟ ﺎﺩﺳﺔ: 6 ﺍﻟﺑﺭﻣﺟﺔ ﺑﻠﻐﺔ ﻓﻳﺟﻭﻝ ﺑﻳﺳﻙInputBox( ) Function An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is A = InputBox (“Question or Phrase”, “Window Title”, “ ”) Example1: Dim a As string Command1: Private Sub Command1_Click() a = InputBox("What is your full name ?", "Input Box Example", " ") End Sub Integer: if you input numbers in the InputBox. String: if you input Text in the InputBox.

Upload: truongnhu

Post on 13-Oct-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

1

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

InputBox( ) Function An InputBox( ) function will display an input Box window where the user can enter a value or a text. The format is

A = InputBox (“Question or Phrase”, “Window Title”, “ ”)

Example1:

Dim a As string

Command1: Private Sub Command1_Click()

a = InputBox("What is your full name ?", "Input Box Example", " ") End Sub

Integer: if you input numbers in the InputBox. String: if you input Text in the InputBox.

Page 2: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

2

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Notes:

1. If we Dim a variable as a string or integer, then the variable = InputBox( PUT ALL STATEMENT INSIDE BRACKETS).

2. If we use InputBox directly without Dim, then InputBox”NO NEED FOR BRACKETS”.

Example2: Using inputBox method, changing the background color of the form.

Solution:

Code:

Dim a As String

Command1: Private Sub Command1_Click()

a = InputBox("What is your name ?", "Background Color", "")

If a = "Your Name" Then

Form1.BackColor = vbRed

End If

End Sub

Page 3: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

3

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Example3: Using inputBox method, showing your name in the TextBox if the InputBox Number = 10.

Solution:

Code:

Dim a As Integer

Command1: Private Sub Command1_Click() a = InputBox("What is the Number ?", "Input Example"," ") If a = "10" Then Text1.Text = "Your Name" End If End Sub

Page 4: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

4

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

MsgBox(””) Function

The objective of MsgBox is to produce a pop-up message box and prompt the user to click on a command button before to confirm specific condition. This message box format is as follows:

MsgBox “Answer or Phrase”, vbInformation + vbYesNo, “Window Title”

Notes:

3. If we Dim a variable as a string or integer, then the variable = MsgBox( PUT ALL STATEMENT INSIDE BRACKETS). See example5

4. If we use MsgBox directly without Dim, then MsgBox”NO NEED FOR BRACKETS”.

5. In MsgBox method doesn’t matter if we Dim as an Integer or as a String.

Page 5: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

5

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Icons:

Constant Value Description

vbCritical 16 Display Critical message icon

vbQuestion 32 Display Warning Query icon

vbExclamation 48 Display Warning message icon

vbInformation 64 Display information icon

Buttons:

Constant Value Description

vbOkOnly 0 Display OK button only

vbOkCancel 1 Display OK and Cancel buttons

vbAbortRetryIgnore 2 Display Abort, Retry and Ignore buttons

vbYesNoCancel 3 Display Yes, No and Cancel buttons

vbYesNo 4 Display Yes and No buttons

vbRetryCancel 5 Display Retry and Cancel buttons

Page 6: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

6

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Example4: Using inputBox method and MsgBox, showing your name in the Text box if the InputBox Number = 10 then (Correct Number), if not then (Wrong Number). Use icons with MsgBox.

Solution:

Code:

Dim a As Integer

Command1:

Private Sub Command1_Click() a = InputBox("What is the Number ?", "Input Example"," ") If a = "10" Then MsgBox "Correct Number", vbInformation + vbYesNo, "Check Window" Text1. text=" your name" Else MsgBox "Wrong Number", vbCritical, "Check Window" T e x t 1 . t e x t = " " End If

Page 7: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

7

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

End Sub

Example5: Using MsgBox method, showing that when click Yes, the form background color changes to Red, if not changes to Black. Use icons with MsgBox.

Solution:

Code:

Dim a As Integer

Command1: Private Sub Command1_Click() a = MsgBox("Red or Black", vbInformation + vbYesNo, "Check Window") If a = vbYes Then Form1.BackColor = vbRed Else Form1.BackColor = vbBlack End If End Sub

Page 8: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

8

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Example6: Design a Visual Basic Program to create a User Name and Password form to log in into another form (backcolor Blue), using command button, textboxes, MsgBoxes for error and exit.

Solution:

Adding new Form method 1

Select Form

Page 9: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

9

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Code:

Dim a As String

Command1: Private Sub Command1_Click() If Text1.Text = "Any Name" And Text2.Text = "Any Thing" Then Form1.Hide Form2.Show Else MsgBox "Wrong User Name or Password", vbCritical + vbOKOnly, "Warning" End If End Sub

Adding new Form method 2

Page 10: InputBox( ) Function - University of Babylon · 1 Visual Basic 6 Programming: Lecture 6 ﺔﺳﺩﺎﺳﻟﺍ ﺓﺭﺿﺎﺣﻣﻟﺍ: 6 ﻙﺳﻳﺑ ﻝﻭﺟﻳﻓ ﺔﻐﻠﺑ

10

Visual Basic 6 Programming: Lecture 6 البرمجة بلغة فيجول بيسك 6 : ادسة سالمحاضرة ال

Choosing Unload helping in Cancelling Code

Form1: Private Sub Form_Unload(Cancel As Integer) a = MsgBox("Are you sure you want to exit", vbQuestion + vbYesNo, "Warning") If a = vbYes Then End Else Cancel = 1 End If End Sub Means

not end

Questions: 1. Using InputBox method, change the color of your name in the TextBox.

2. Using MsgBox method, make Command1 (invisible) and Command2 (visible) when click on Command1, and vice versa when click on Command2.

3. Using InputBox and MsgBox method, comparing between Even and Odd numbers.