Download presentation
Presentation is loading. Please wait.
Published byNeal Parker Modified over 8 years ago
1
Dialog Boxes Getting information from user
2
Types of Windows Five types we’ll be using 1. Main Application Window 2. Child Windows 3. Dialog Boxes 4. MessageBox objects 5. Controls on Forms (already discussed) First three are essentially constructed in the same way Differ in how you bring them up
3
Main Window Created when you create a Windows Form Project Associated with application object in Main() when called as an argument to the Run() function, e.g.,: Application.Run(new MainWindow()); When it is closed, all other child windows in the application are closed
4
Child Windows Created within an application, but don’t demand attention Created in two steps: construct & show, e.g., WindowDemo wnd=new WindowDemo(); wnd.Show(); Multiple child windows can be open inside application Some care required to ensure they keep each other updated MDI (multi-document interface) applications have some code built in
5
Dialog Boxes Created within an application and lock application until dismissed Created in two steps: construct & show, e.g., WindowDemo wnd=new WindowDemo(); wnd.ShowDialog(); Buttons in a dialog may return DialogResult enum values Close dialog when pressed Button value is returned by ShowDialog()
6
Example Dialog Code Comments: MultipleChoice is a form that holds a multiple choice test (q is the question) that is presented in dialog form If user presses the button associated with DialogResult.OK (the “Submit” button on the form, we score the question If the user exits some other way (e.g., cancelling) we return false. Other results that can be attached to buttons include: Abort, Cancel, Retry, Ignore, OK, No, None, Yes.
7
DialogResult Property Comments: OK property is attached to Submit button Cancel property is attached to Cancel button (not shown)
8
Designing Dialog Boxes Ensure properties are defined so your code can access information in the box, e.g., if (dlg.Correct) Score = Score+PointsPerQuestion; Work with temporary variables in your form, so user can cancel if necessary Provide buttons (with DialogResult values) so user doesn’t have to cancel the window Remember that the choice to display a child window or dialog is just a matter of what show function you use--Show() or ShowDialog()
9
Standard Dialog Boxes Comments: Pre-created dialogs to save the user the need to design dialogs for frequently required tasks Adding them to a form Dragging them on to a form creates member variable and initialization code Dialog can then be launched without new step Can also be created and used just like user- developed dialog, without being placed on the form
10
Message Boxes A simple class designed to send a message to a user or prompt for a simple result (e.g., Yes, No) Invoked with a static function in the MessageBox class, i.e., MessageBox.Show(“This is a message”); There are many overloads. The MessageBoxButtons enumeration defines combinations of buttons that can be displayed The return value of the Show() function holds the result
11
Example Comments: Prompt is “Click a button” Title bar is “Message box test” Yes, No and Cancel buttons specified (MessageBoxButtons.YesNoCancel)
12
Parting words… Dialog boxes are convenient means of getting information from the user If it’s just a simple (e.g., yes/no) question, create a message box A number of predefined standard dialog boxes, that we’ll be using in Modules 3 & 4, exist File dialog Color dialog Font dialog
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.