CIS 338 Dialog Boxes Dr Ralph D Westfall
CIS 338: Dialog Boxes Dr. Ralph D. Westfall April, 2011
Dialog Boxes do one of following n display a message to user w has only one button (OK) n get a decision from user w OK, Cancel, etc. n get information from user w e. g. , name, password, age, date of birth, etc.
VB Dialog Boxes You Can Use Message. Box n message + one or more buttons Msg. Box n simplified version of Message. Box User-created n n do it yourself with forms, or using form templates
Syntax Convention computer language documentation uses a syntax convention to show which items are required versus optional n n Message. Box. Show(prompt [, title][, buttons] [, icon][, default. Button][, options]) [ items in square brackets are optional ] prompt is the only required argument in the above code commas not needed for unused options, so are shown inside square brackets
Message. Box. Show(prompt [, title][, buttons] [, icon][, default. Button][, options]) n prompt: text string to display n title: message box title bar (same as Project name if not provided) n buttons: button(s) to display n icon: icon (image) n default. Button: gets initial (at 1 st) focus n options: international settings n Note: Msg. Box has an abbreviated syntax
Message Box Examples Message. Box. Show("It's crashing") 'using a string literal s. Message = "The 3 rd crash today!" Message. Box. Show(s. Message) 'using a string variable n. Reply = Message. Box. Show("Quit? ", _ Message. Box. Buttons. Yes. No) 'using Message. Box as a function 'that receives a return value 'note object. property: . Yes. No
Message. Box. Icon Values values in the Message. Box. Icon property that get specified icons. Stop (or. Error) 'severe error. Exclamation 'warning. Question 'need information. Information 'status message. None (or no value) 'no icon (default) 'more in notes
Message. Box Button Values properties control number of choices and text on buttons n n n . OK (OK button). OKCancel (2 buttons). Abort. Retry. Ignore (3 buttons). Yes. No. Cancel (3 buttons). Yes. No (2 buttons). Retry. Cancel (2 buttons)
Message. Box. Default. Button Values identify default button (gets the focus) counting from the left n n . Button 1. Button 2. Button 3 for example, . Button 2 on a Yes. No button makes No the default
Message. Box Button Values n n n vb. OK vb. Cancel vb. Yes. No vb. Ignore vb. Yes vb. No = = = 1 2 4 5 6 7 'notes n. Reply = Message. Box. Show("Choice? ", "Click", Message. Box. Buttons. Yes. No) If n. Reply = vb. Yes Then [some code]
Modal Message. Boxes are "modal" at the application level n until clicks button, user can't do anything else in the VB application w no other VB code can run until clicked VB 6: can also make modal for whole system n will not be able to use any other program in Windows until the user responds to the Message. Box (was NOT a good idea!)
Input. Box Dialog Control syntax Input. Box(prompt[, title][, default][, x][, y]) n. Input = Input. Box("Age? ") prompt is shown above text input box if provided, title shows on title bar default goes into the input box (e. g. , "guest") x (horizontal), y (vertical) position of box on screen (in pixels)
Input. Box Dialog Example Dim s. Name As String s. Name = Input. Box("Enter your name", _ " Name? ", "guest", 50) 'guest is the default value in this code
More Dialog Controls can also use Microsoft Common Dialog controls in VB. NET n n n standard Windows/Microsoft Office items Color, Folder, Font, File Open/Save items in Toolbox may need to stretch Tool. Box to see them (down toward the bottom in Dialogs category)
Using Common Dialog Boxes drag and drop onto the form, but will only see in new panel under the form n n Open. File. Dialog – files to open Save. File. Dialog – files to save Font. Dialog – fonts to use Color. Dialog – colors to use Print. Dialog is now in a separate section of Toolbox with other printing objects
Example: File. Dialog shows a standard Windows File>Open form code specifies separate lines of text in Files of type: n dropdown Combo. Box at bottom of dialog just returns name of file selected by user n still need to write code to open/read file
File. Dialog Code Private Sub Button 2_Click( … etc. Open. File. Dialog 1. Initial. Directory = "C: " Open. File. Dialog 1. Filter() = "Text files (*. txt)|*. txt|All files (*. *)|*. *" Open. File. Dialog 1. File. Name = "" Open. File. Dialog 1. Show. Dialog() Msg. Box(Open. File. Dialog 1. File. Name) End Sub 'notes
Do It Yourself Dialog Boxes customize a form n n add a form: Project>Add Windows Form set appropriate properties Form. Border. Style = Sizable. Tool. Window '(narrow, just x) (Name) = frm[Name] Text = [ whatever you want to say]
Do It Yourself Dialog Boxes - 2 can customize it to do what you want n n add labels and buttons, etc. as appropriate display modally when you need it frm[Name]. Show. Dialog can make it to be a template so you can use it again demo Grade. SQL
- Slides: 19