26
DateTime, Code Regions, and Multiple Form Applications Part13dbg

DateTime, Code Regions, and Multiple Form Applications

  • Upload
    jacoba

  • View
    33

  • Download
    1

Embed Size (px)

DESCRIPTION

DateTime, Code Regions, and Multiple Form Applications. Part13dbg. DateTime. DateTime is used for storing and converting time and date values. The Now property of the DateTime object delivers the present time and date from the system clock. DateTime currentDateTime = DateTime.Now ; - PowerPoint PPT Presentation

Citation preview

Page 1: DateTime, Code Regions, and Multiple Form Applications

DateTime, Code Regions, and Multiple Form Applications

Part13dbg

Page 2: DateTime, Code Regions, and Multiple Form Applications

2

DateTime

• DateTime is used for storing and converting time and date values.

• The Now property of the DateTime object delivers the present time and date from the system clock.DateTime currentDateTime = DateTime.Now;

• The Today property of DateTime object is the date portion only.DateTime currentDate = DateTime.Today;

Page 3: DateTime, Code Regions, and Multiple Form Applications

3

Working With DateTime

• Declare a DateTime object:DateTime myDT = new DateTime();

• Get present time and date:myDT = DateTime.Now;

• Extract time or date value:lblDay.Text =

myDT.DayOfWeek.ToString();• DayOfWeek, DayofYear, Day, Hour, Second,

Month, Year properties allow access to various parts of DateTime.

DateTimeStruct

Page 4: DateTime, Code Regions, and Multiple Form Applications

4

Formatting DateTime output

• Use Format Specifiers with ToString()

• or use ToLongDateString(), ToLongTimeString(), ToShortDateString(), or ToShortTimeString() methods.

d

D

t

T

f

F

6/4/2007

Monday, June 04, 2007

12:00 AM

12:00:00 AM

Monday, June 04, 2007 12:00 AM

Monday, June 04, 2007 12:00:00 AM

Page 5: DateTime, Code Regions, and Multiple Form Applications

5

Generate DateTime Values with DateTimePicker Control• The DateTimePicker control furnishes a visible

interface for selecting DateTime values.• Dates are selected with a perpetual calendar

display.• Times and dates may be displayed in several

formats.• Use the TimeSpan class to manipulate and

compare times and dates.

DatePicker

Page 6: DateTime, Code Regions, and Multiple Form Applications

Code Regions

Showing/Hiding and Creating Code Regions

Page 7: DateTime, Code Regions, and Multiple Form Applications

7

Regions in Code

• We have seen before that the region, Windows Form Designer Generated Code, is a collapsible region.

• When we want to view the contents, we click the in front of region.

• To collapse the region, we click the in front of region.

Page 8: DateTime, Code Regions, and Multiple Form Applications

8

Making Your Own Collapsible Regions in Code

• You can make your own collapsible region in code, for example, a region for the class-level variables.

• Type #region regionDescription above the class-level variable declarations.

• Type #endregion after the last line of class-level variable declarations.

• You will now have a user-defined region.

Page 9: DateTime, Code Regions, and Multiple Form Applications

Multiple Form Applications

Single Document Interface

Page 10: DateTime, Code Regions, and Multiple Form Applications

10

Launching a Second Form• Use the Add Windows Form…

selection from the Project menu.

• A new form class is added to the project.

Page 11: DateTime, Code Regions, and Multiple Form Applications

11

Launching a Second Form

• An instance of the second form must be created within the original application form.

• Once the form object is created, it may be displayed by running its Show() method.

• Multiple instances of the second form can be created and displayed.

SecondSDI

Page 12: DateTime, Code Regions, and Multiple Form Applications

12

Startup Form as “Parent”

• Launching a second form in this way causes the second form to depend upon the first for survival.

• Closing the first form causes the second form to close as well.

Page 13: DateTime, Code Regions, and Multiple Form Applications

13

Hiding a Form

• Run the Hide() method of the original form to remove it from view prior to showing the second form.

• Now the second form commands the application and can not lose focus unless it is closed.

• Closing the second form leaves the original form running, but not visible.

HideParent

Page 14: DateTime, Code Regions, and Multiple Form Applications

14

Showing a Hidden Form

• We could run the Show() method of the original form from the second form—but only if the second form is aware of the first form

• If we created a new instance of the first form within the second form it would be different from the original.

• One way to solve the problem is to pass the identity of the original form to the second form as a constructor argument.

Page 15: DateTime, Code Regions, and Multiple Form Applications

15

Identifying the Parent Form

• If the second form has a class scope variable of type Form, the constructor can use its argument to assign the identity of the “parent” form.

• This then allows the second form to run the Show() method of the “parent”.

FormConstructor

Page 16: DateTime, Code Regions, and Multiple Form Applications

Multiple Form Applications

Owned Forms

Page 17: DateTime, Code Regions, and Multiple Form Applications

17

Owned Forms

• Subsequent SDI forms can be launched from a startup form as Owned Forms.

• Although these forms can transfer focus back to the startup form they remain on top of the startup form at all times.

• Add a new form to the Owned Forms collection of the startup form by setting its Owner property to the startup form.

OwnedForms

Page 18: DateTime, Code Regions, and Multiple Form Applications

18

Owned Forms

• Potential uses for owned forms are as specialized help forms, customized “toolboxes” or overview maps.

• Use an owned form when you don’t want to lose sight of a form in a multiple SDI application, even if it loses focus.

Page 19: DateTime, Code Regions, and Multiple Form Applications

Multiple Form Applications

Multiple Document Interface

Page 20: DateTime, Code Regions, and Multiple Form Applications

20

MDI Forms

• Standard Windows forms may exist as either MDI parents or MDI children.

• MDI children may only be displayed within the client area of their parent and may not be dragged outside that client area.

• Like owned forms, MDI children always remain on top of their parent form.

Page 21: DateTime, Code Regions, and Multiple Form Applications

21

MDI Parent Forms

• Any standard Windows form can be converted to an MDI parent by setting its IsMdiContainer property to True.

• By convention, MDI parent forms have a dark gray client area.

• Because MDI parents are intended solely as containers for other forms, they should not include controls.

Page 22: DateTime, Code Regions, and Multiple Form Applications

22

MDI Parent Forms

• Use menu items in the MenuStrip control of the MDI parent to control activities associated with displaying MDI child forms.

MDIParent

Page 23: DateTime, Code Regions, and Multiple Form Applications

23

MDI Child Forms

• Forms destined to become children of an MDI parent should be instantiated within the MDI parent class.

• Any standard Windows form can become an MDI child.

• Set the MdiParent property of the child instance to the parent.

• Show the child form.

MDIChildren

Page 24: DateTime, Code Regions, and Multiple Form Applications

24

MDI Child Form Menus

• When an MDI Child has focus within an MDI parent form, the menustrips of the Parent and Child are merged and menuitems from both are displayed.

• The merging of the menus to present a sensible display is coordinated by adjusting the MergeAction and MergeIndex properties of each menuitem.

• The Visible property of the child menustrip must be set to false if you do not wish to see both parent and child menustrips.

Page 25: DateTime, Code Regions, and Multiple Form Applications

25

The Windows Menu

• By design convention the Menustrip of an MDI parent form has a Window menu item that lists the displayed child forms and indicates which has focus.

• The contents of this menuitem (mnuWindow) are automatically generated by simply setting the MdiWindowListItem property of the parent menustrip to the appropriate menuitem (mnuWindow).

Page 26: DateTime, Code Regions, and Multiple Form Applications

26

The Window Menu

• Three different modes of display are available for child forms: cascade, horizontal tile, and vertical tile.

• These modes can be set with the LayoutMdi() method of the parent form.

• Use the foreach loop to close all forms in the child collection of the parent at once.

MDIWindows