Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-form applications and dialogs

Similar presentations


Presentation on theme: "Multi-form applications and dialogs"— Presentation transcript:

1 Multi-form applications and dialogs
Secondary Forms Multiform Applications November 17, 2018

2 Multiform applications
Nearly all desktop GUI applications use more than a single form to do their jobs Visual Studio has the editor with many fly-out panels, dialogs to open or create projects, classes, resources, and so forth, save them, close them, debug them, and so forth Word processors have open/save/save as dialogs, select font or color dialogs, about boxes, and so forth Even browsers may have multiple sites open in different tabs, pop-up security messages or ads, and so forth Many applications have splash screens and about boxes at a minimum Multiform Applications November 17, 2018

3 Common Dialogs Some dialogs are so commonly needed among applications, that .NET supplies prewritten classes for you to use in such cases Tasks such as Opening or Saving a File, Selecting a Font, Choosing a Color, Displaying a Message in a MessageBox, and so forth are good candidates for such dialogs Among the common dialogs provided by the .NET class library are OpenFileDialog, SaveFileDialog, ColorDialog, FontDialog, PrintDialog, MessageBox, and PageSetupDialog These may be used as they are or tailored to suit the needs of the developer Multiform Applications November 17, 2018

4 Example Create new instance of ColorDialog
If user did not dismiss by canceling the operation Display dialog Get selected color Multiform Applications November 17, 2018

5 Select color and click OK
Example Color Dialog Result Select color and click OK Multiform Applications November 17, 2018

6 OpenFileDialog Multiform Applications November 17, 2018

7 OpenFileDialog Example
Create dialog instance Caption for the dialog Filter: limits which files show in the dialog If the user Canceled the dialog, simply return; otherwise process the selected file When dialog first opens, initial directory is folder where app’s .exe is found Filter contains sequences of “description|*.ext” where “description” is a phrase that will be shown describing what is being shown and “*.ext” specifies which file extensions are shown Multiform Applications November 17, 2018

8 OpenFileDialog Example
Caption Initial Directory Dropdown filter ComboBox Expanded Only .txt files shown Filter Multiform Applications November 17, 2018

9 SaveFileDialog Multiform Applications November 17, 2018

10 SaveFileDialog Example
Create instance Set initial directory, caption, and filters Display dialog, and, if not cancelled by user, process the selected file Multiform Applications November 17, 2018

11 SaveFileDialog Example
Caption Initial Directory Only *.txt files shown Filter Multiform Applications November 17, 2018

12 FontDialog Multiform Applications November 17, 2018

13 FontDialog Demo Create instance
Set minimum and maximum allowable font sizes Initial font set to current font If user selected a font and did not cancel, make the label use that font Multiform Applications November 17, 2018

14 FontDialog Example Click Button Multiform Applications
November 17, 2018

15 Right click on project, choose Add, Windows Form…
Secondary Forms Right click on project, choose Add, Windows Form… The developer may also create new secondary forms From the Solution Explorer, add a new Form to the project Add new form Multiform Applications November 17, 2018

16 Pre-built, modifiable About Box
Secondary Form Ordinary Form Pre-built, modifiable About Box Give it a suitable name Multiform Applications November 17, 2018

17 Secondary Form The secondary form looks just like the main form initially Developer may add controls, write event handlers, and so forth in the same ways as with the primary form Multiform Applications November 17, 2018

18 Communicating Between Forms
Primary creates an object of Secondary and displays it The primary and secondary forms are just objects of classes and they interact just like ordinary objects. Do not let the fact that they are form objects make them mysterious Passes Parameters to methods of Form2 Primary Form Secondary Form Return values and exposes public properties Primary knows about, creates, and uses secondary. Secondary knows NOTHING about primary. Multiform Applications November 17, 2018

19 Example: Communicating Between Forms
Primary form sets some properties of secondary form Primary form instantiates an object of secondary form Primary form displays the secondary form . . . There is no difference in dealing with a developer-created form and a built-in dialog Multiform Applications November 17, 2018

20 Using Secondary Forms Can use Show or ShowDialog from one form to display an instance of another Modal dialog must be dismissed before continuing with the parent form Show yields a modeless dialog, while ShowDialog yields a modal dialog Modeless dialog does not “block” the parent and user may switch back and forth between them Multiform Applications November 17, 2018

21 Setting the Result of a Dialog Box
The way a dialog box is closed, or its "result," can be set at either design time or run time: At design time, you can set the DialogResult property for the Button controls on a dialog box Setting the DialogResult property at run time allows you to dynamically handle user responses To set the DialogResult property for a control at design time: Click on the Button control whose property you want to set Select the DialogResult property in the Properties window and open the list of available property settings Select the appropriate DialogResult value Lecture 9 - CSCI 3800: Forms and Dialogs 11/17/2018

22 Setting Results of a Dialog Programmatically
You can set the dialog box result for user-performed actions other than clicking a Button control. If your dialog box does not contain buttons to close the dialog box, you can set the result of the dialog box at run time. To set the DialogResult property for a control or form programmatically Navigate to the event handler or method in which you want to set the DialogResult property. Type code similar to the following: this.DialogResult = DialogResult.Yes; // OR – for a button btnNo.DialogResult = DialogResult.No; Although setting the DialogResult property will cause your dialog box to close automatically, you can still finish handling the control's Click event, and the dialog box will close once the event handler's code is completed. Lecture 9 - CSCI 3800: Forms and Dialogs 11/17/2018

23 Using a Secondary Form Typically, the logic of an application may dictate that one form may “own” another form in the sense that the first form decides when to create, show, and use the second form The first form often uses second form as result of some user action such as Selecting a choice (such as Open, Save, Print, Select Font, About, Help, and so forth) from a main Menu or a context Menu Clicking a button Selecting something from a ListBox or ComboBox The second form may also use secondary forms of its own In the event handler for the event that results in the display of the second form, an instance of the second form is created, and it is displayed through a call to a method such as Show or ShowDialog … Multiform Applications November 17, 2018

24 Creating and Using a Secondary Form
From within an Event Handler in one Form, the code below may be used to create and display a second Form Create instance of the second form Display the second form Use results of work done by second form here in the first form Multiform Applications November 17, 2018

25 Using a Secondary Form In some cases, however, there may be a second form that is independent of all other forms No other form decides when to display it No other form creates an instance of it or uses its methods and properties A splash screen may be such a form In such a case, the secondary form may be “owned” by the application rather than by another form In this case, it may be opened by some method such as Main in the driver program Multiform Applications November 17, 2018

26 Independent Secondary Form
Create and show splash screen form . . . … then, create and show the primary form for the application Note that these are sequential operations. The primary form will not be created and shown until the SplashForm has been closed Multiform Applications November 17, 2018

27 Replaceable picture in PictureBox
Using the AboutBox VS provides a skeleton of an AboutBox form that is usable as it is, but it may also be tailored to suit the application These items are filled-in at run-time from AssemblyInfo.cs by the code that is provided Replaceable picture in PictureBox Multiform Applications November 17, 2018

28 Generated/Modified Code
Description modified Default picture is replaced Multiform Applications November 17, 2018

29 AboutBox Example Results
Multiform Applications November 17, 2018

30 Splash Screen One may remove the border and caption via the FormBorderStyle property if desired Ordinary form with a Split Container control docked to fill the form One Panel of the Split Container has a Picture Box Transparency may be achieved by setting the TransparencyKey property The other panel has a Label for the Program Name … … and another for the version number Uses a timer to dismiss itself Multiform Applications November 17, 2018

31 Transparent Borderless Form
Remove the border from around the form Everything that is white in the design will be transparent when the form is displayed at run time Multiform Applications November 17, 2018

32 Borderless, Transparent Form
Multiform Applications November 17, 2018

33 The Code in the Splash Screen Class
Timer interval is set to 3.5 seconds in this case Multiform Applications November 17, 2018

34 Code in the Driver Primary form is not displayed until after the Splash Screen form closes itself Multiform Applications November 17, 2018


Download ppt "Multi-form applications and dialogs"

Similar presentations


Ads by Google