Visual Basic Message Box & Input Box Usage Guide
Understand how to effectively utilize MsgBox and InputBox functions in Visual Basic programming, including syntax, arguments, examples, and best practices to enhance user interactions.
Visual Basic Message Box & Input Box Usage Guide
E N D
Presentation Transcript
Algoritma & PEMROGRAMAN 2B (Visual basic) Bonang Waspadadi Ligar, S.Si, MMSI
Message box • Message Box / Kotak pesan adalah kotak dialog khusus yang digunakan untuk menampilkan informasi kepada pengguna • User tidak bisa mengetik apapun pada message box • MsgBox adalah perintah yang digunakan untuk menampilkan message box.
syntax • Syntax: • MsgBox “prompt” [, buttons, “title”] • Prompt.Required. Pesan yang ditampilkan di kotak dialog. Panjang maksimum prompt kira-kira 1024 karakter, tergantung lebar karakter yang digunakan. • Buttons.Optional. Ekspresi numerik yang menentukan jenis tombol, icon, dll. • Title.Optional. Judul dialog box, jika tidak ditentukan maka nama program yang akan ditampilkan.
The buttons argument • The buttons argument is formed by five groups of values. • The first group of values (0–5) describes the number and type of buttons displayed in the dialog box; • The second group (16, 32, 48, 64) describes the icon style; • The third group (0, 256, 512, 768) determines which button is the default;
First group Determines which buttons to display
second group Determines which icon to display
third group Determines which button is default
fourth group Special buttons
Message box • There are 2basic ways to use MsgBox: • If you do NOT need to test which button the user clicked • if you DO need to test which button the user clicked
Message box • If you do NOT need to test which button the user clicked (only an OK button), then you can use MsgBox as if you were calling a Sub. You can use the following syntax: • Msgbox arguments • -or- • Call MsgBox(arguments)
Example • The statement • MsgBox "Hello there!" • causes the following box to be displayed:
Example • The statement • MsgBox "The Last Name field must not be blank.", vbExclamation, "Last Name“ • causes the following box to be displayed:
Example • vbExclamation + vbOKOnly • making the full statement read: • MsgBox "The Last Name field must not be blank.", • vbExclamation + vbOKOnly, • "Last Name"
Example • Remember, for the buttons argument, you can add one value from each of the four groups. • An alternative (not recommended) is to use the hard-coded number for the buttons argument, as in: • MsgBox "The Last Name field must not be blank.", 48, "Last Name"
Example • MsgBox "A bad database error has occurred.", vbCritical, • "UpdateCustomerTable" • Result:
Message box • If you DO need to test which button the user clicked (i.e., message box with more than one button), then you must use MsgBox as a function, using the following syntax: • IntegerVariable = Msgbox (arguments)
Example • Dim intResponse As Integer • intResponse = MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Quit") • If intResponse = vbYes Then • End • End If • Result: • After the user clicks a button, you would test the return variable (intResponse) for a value of vbYes or vbNo (6 or 7).
Example • Note that the use of the built-in constants makes the code more readable. The statement • intResponse = MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Quit") • is more readable than • intResponse = MsgBox("Are you sure you want to quit?", 36, "Quit")
Example • Note that the use of the built-in constants makes the code more readable. The statement • If intResponse = vbYes Then • is more readable than • If intResponse = 6 Then
Example • You could use the MsgBox function directly in an if statement without using a separate variable to hold the result ("intResponse" in this case). For example, the above example could have been coded as: • If MsgBox("Are you sure you want to quit?", vbYesNo + vbQuestion, "Quit")= vbYes Then • End • End If
Example • Following is an example using the vbDefaultButton2 constant: • Dim intResponse As Integer • intResponse = MsgBox("Are you sure you want to delete all of the rows " & "in the Customer table?", vbYesNo + vbQuestion + vbDefaultButton2, "Delete") • If intResponse = vbYes Then • ' delete the rows ... • End If • The message box displayed by this example would look like this:
input box • Input Box adalah kotak dialog yang dirancang khusus yang memungkinkan pemrogram meminta sebuah nilai dari pengguna dan menggunakan nilai itu seperlunya. • Terdiri dari judul, pesan untuk menunjukkan nilai yang diminta, text box, dan 2 tombol: OK and Cancel. • InputBox adalah perintah yang digunakan untuk menampilkan message box.
syntax • Syntax: • stringvariable = InputBox (“prompt” , “title” [, default] [, xpos] [, ypos]) • Prompt.Required. Pesan yang ditampilkan di input box. Panjang maksimum prompt kira-kira 1024 karakter, tergantung lebar karakter yang digunakan. • Title.Optional. Judul input box, jika tidak ditentukan maka nama program yang akan ditampilkan. • Default.Optional. Ekspresi numerik yang akan tampil pertama kali saat kotak inputbox tampil. • xpos and ypos. Optional. koordinat untuk menentukan posisi kotak masukan (ukuran pixel)
Example • The statement • Dim strName As String • strName = InputBox("Enter your name:", "Input Test") • causes the following box to be displayed:
Example • The statement • strAcctID = InputBox("Enter account ID:", "Input Test", "ABC-123") • causes the following box to be displayed:
Example • Private Sub Commandl_Click( ) • Dim destination As String • Do destination = InputBox("Enter destination", "Vacation destinations", _ "Hawaii", 100,300) • If destination <> “” Then MsgBox "You have chosen" & destination • Loop Until destination <> “ “ • End Sub • causes the following box to be displayed: