190 likes | 411 Views
CIS 338: Dialog Boxes. Dr. Ralph D. Westfall April, 2011. Dialog Boxes. do one of following display a message to user has only one button (OK) get a decision from user OK, Cancel, etc. get information from user e.g., name, password, age, date of birth, etc.
E N D
CIS 338: Dialog Boxes Dr. Ralph D. Westfall April, 2011
Dialog Boxes • do one of following • display a message to user • has only one button (OK) • get a decision from user • OK, Cancel, etc. • get information from user • e.g., name, password, age, date of birth, etc.
VB Dialog Boxes You Can Use • MessageBox • message + one or more buttons • MsgBox • simplified version of MessageBox • User-created • 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 • MessageBox.Show(prompt[, title][, buttons] [, icon][, defaultButton][, 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
MessageBox • MessageBox.Show(prompt [, title][, buttons] [, icon][, defaultButton][, options]) • prompt: text string to display • title: message box title bar (same as Project name if not provided) • buttons: button(s) to display • icon: icon (image) • defaultButton: gets initial (at 1st) focus • options: international settings • Note: MsgBox has an abbreviated syntax
Message Box Examples MessageBox.Show("It's crashing") 'using a string literal sMessage = "The 3rd crash today!" MessageBox.Show(sMessage) 'using a string variable nReply = MessageBox.Show("Quit?",_ MessageBoxButtons.YesNo) 'using MessageBox as a function 'that receives a return value 'note object.property: .YesNo
MessageBoxIcon Values • values in the MessageBoxIcon 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
MessageBox Button Values • properties control number of choices and text on buttons • .OK (OK button) • .OKCancel (2 buttons) • .AbortRetryIgnore (3 buttons) • .YesNoCancel (3 buttons) • .YesNo (2 buttons) • .RetryCancel (2 buttons)
MessageBoxDefaultButton Values • identify default button (gets the focus) counting from the left • .Button1 • .Button2 • .Button3 • for example, .Button2 on a YesNo button makes No the default
MessageBox Button Values • vbOK = 1 • vbCancel = 2 • vbYesNo = 4 • vbIgnore = 5 • vbYes = 6 • vbNo = 7 'notes nReply = MessageBox.Show("Choice?", "Click", MessageBoxButtons.YesNo) If nReply = vbYes Then [some code]
Modal MessageBoxes • MessageBoxes are "modal" at the application level • until clicks button, user can't do anything else in the VB application • no other VB code can run until clicked • VB6: can also make modal for whole system • will not be able to use any other program in Windows until the user responds to the MessageBox (was NOT a good idea!)
InputBox Dialog Control • syntax InputBox(prompt[, title][,default][,x][,y]) nInput = InputBox("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)
InputBox Dialog Example Dim sName As String sName = InputBox("Enter your name", _ " Name?", "guest", 50, 50) 'guest is the default value in this code
More Dialog Controls • can also use Microsoft Common Dialog controls in VB.NET • standard Windows/Microsoft Office items • Color, Folder, Font, File Open/Save items in Toolbox • may need to stretch ToolBox 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 • OpenFileDialog – files to open • SaveFileDialog – files to save • FontDialog – fonts to use • ColorDialog – colors to use • PrintDialog is now in a separate section of Toolbox with other printing objects
Example: FileDialog • shows a standard Windows File>Open form • code specifies separate lines of text in Files of type: • dropdown ComboBox at bottom of dialog • just returns name of file selected by user • still need to write code to open/read file
FileDialog Code Private Sub Button2_Click( … etc. OpenFileDialog1.InitialDirectory = "C:\" OpenFileDialog1.Filter() = "Text files (*.txt)|*.txt|All files (*.*)|*.*" OpenFileDialog1.FileName = "" OpenFileDialog1.ShowDialog() MsgBox(OpenFileDialog1.FileName) End Sub 'notes
Do It Yourself Dialog Boxes • customize a form • add a form: Project>Add Windows Form • set appropriate properties FormBorderStyle = SizableToolWindow '(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 • add labels and buttons, etc. as appropriate • display modally when you need it frm[Name].ShowDialog • can make it to be a template so you can use it again • demo GradeSQL