Skip to content

Commit 749d7ac

Browse files
matt9uccisdwheeler
authored andcommitted
Update sample: Graphical Date Picker (#4136)
1 parent a22bf82 commit 749d7ac

File tree

1 file changed

+71
-58
lines changed

1 file changed

+71
-58
lines changed

reference/docs-conceptual/samples/Creating-a-Graphical-Date-Picker.md

Lines changed: 71 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -16,114 +16,127 @@ Copy and then paste the following into Windows PowerShell ISE, and then save it
1616
Add-Type -AssemblyName System.Windows.Forms
1717
Add-Type -AssemblyName System.Drawing
1818
19-
$form = New-Object Windows.Forms.Form
20-
21-
$form.Text = 'Select a Date'
22-
$form.Size = New-Object Drawing.Size @(243,230)
23-
$form.StartPosition = 'CenterScreen'
19+
$form = New-Object Windows.Forms.Form -Property @{
20+
StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
21+
Size = New-Object Drawing.Size 243, 230
22+
Text = 'Select a Date'
23+
Topmost = $true
24+
}
2425
25-
$calendar = New-Object System.Windows.Forms.MonthCalendar
26-
$calendar.ShowTodayCircle = $false
27-
$calendar.MaxSelectionCount = 1
26+
$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
27+
ShowTodayCircle = $false
28+
MaxSelectionCount = 1
29+
}
2830
$form.Controls.Add($calendar)
2931
30-
$OKButton = New-Object System.Windows.Forms.Button
31-
$OKButton.Location = New-Object System.Drawing.Point(38,165)
32-
$OKButton.Size = New-Object System.Drawing.Size(75,23)
33-
$OKButton.Text = 'OK'
34-
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
32+
$OKButton = New-Object Windows.Forms.Button -Property @{
33+
Location = New-Object Drawing.Point 38, 165
34+
Size = New-Object Drawing.Size 75, 23
35+
Text = 'OK'
36+
DialogResult = [Windows.Forms.DialogResult]::OK
37+
}
3538
$form.AcceptButton = $OKButton
3639
$form.Controls.Add($OKButton)
3740
38-
$CancelButton = New-Object System.Windows.Forms.Button
39-
$CancelButton.Location = New-Object System.Drawing.Point(113,165)
40-
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
41-
$CancelButton.Text = 'Cancel'
42-
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
41+
$CancelButton = New-Object Windows.Forms.Button -Property @{
42+
Location = New-Object Drawing.Point 113, 165
43+
Size = New-Object Drawing.Size 75, 23
44+
Text = 'Cancel'
45+
DialogResult = [Windows.Forms.DialogResult]::Cancel
46+
}
4347
$form.CancelButton = $CancelButton
4448
$form.Controls.Add($CancelButton)
4549
46-
$form.Topmost = $true
47-
4850
$result = $form.ShowDialog()
4951
50-
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
51-
{
52+
if ($result -eq [Windows.Forms.DialogResult]::OK) {
5253
$date = $calendar.SelectionStart
5354
Write-Host "Date selected: $($date.ToShortDateString())"
5455
}
5556
```
5657

57-
The script begins by loading two .NET Framework classes: **System.Drawing** and **System.Windows.Forms**. You then start a new instance of the .NET Framework class **Windows.Forms.Form**; that provides a blank form or window to which you can start adding controls.
58+
The script begins by loading two .NET Framework classes: **System.Drawing** and **System.Windows.Forms**.
59+
You then start a new instance of the .NET Framework class **Windows.Forms.Form**; that provides a blank form or window to which you can start adding controls.
5860

5961
```powershell
60-
$form = New-Object Windows.Forms.Form
62+
$form = New-Object Windows.Forms.Form -Property @{
63+
StartPosition = [Windows.Forms.FormStartPosition]::CenterScreen
64+
Size = New-Object Drawing.Size 243, 230
65+
Text = 'Select a Date'
66+
Topmost = $true
67+
}
6168
```
6269

63-
After you create an instance of the Form class, assign values to three properties of this class.
70+
This example assigns values to four properties of this class by using the **Property** property and hashtable.
6471

65-
- **Text.** This becomes the title of the window.
72+
1. **StartPosition**:
73+
If you don’t add this property, Windows selects a location when the form is opened.
74+
By setting this property to **CenterScreen**, you’re automatically displaying the form in the middle of the screen each time it loads.
6675

67-
- **Size.** This is the size of the form, in pixels. The preceding script creates a form that’s 243 pixels wide by 230 pixels tall.
76+
2. **Size**:
77+
This is the size of the form, in pixels.
78+
The preceding script creates a form that’s 243 pixels wide by 230 pixels tall.
6879

69-
- **StartingPosition.** This optional property is set to **CenterScreen** in the preceding script. If you don’t add this property, Windows selects a location when the form is opened. By setting the **StartingPosition** to **CenterScreen**, you’re automatically displaying the form in the middle of the screen each time it loads.
80+
3. **Text**:
81+
This becomes the title of the window.
7082

71-
```powershell
72-
$form.Text = 'Select a Date'
73-
$form.Size = New-Object Drawing.Size @(243,230)
74-
$form.StartPosition = 'CenterScreen'
75-
```
83+
4. **Topmost**:
84+
By setting this property to `$true`, you can force the window to open atop other open windows and dialog boxes.
7685

77-
Next, create and then add a calendar control in your form. In this example, the current day is not highlighted or circled. Users can select only one day on the calendar at one time.
86+
Next, create and then add a calendar control in your form.
87+
In this example, the current day is not highlighted or circled.
88+
Users can select only one day on the calendar at one time.
7889

7990
```powershell
80-
$calendar = New-Object System.Windows.Forms.MonthCalendar
81-
$calendar.ShowTodayCircle = $false
82-
$calendar.MaxSelectionCount = 1
91+
$calendar = New-Object Windows.Forms.MonthCalendar -Property @{
92+
ShowTodayCircle = $false
93+
MaxSelectionCount = 1
94+
}
8395
$form.Controls.Add($calendar)
8496
```
8597

86-
Next, create an **OK** button for your form. Specify the size and behavior of the **OK** button. In this example, the button position is 165 pixels from the form’s top edge, and 38 pixels from the left edge. The button height is 23 pixels, while the button length is 75 pixels. The script uses predefined Windows Forms types to determine the button behaviors.
98+
Next, create an **OK** button for your form.
99+
Specify the size and behavior of the **OK** button.
100+
In this example, the button position is 165 pixels from the form’s top edge, and 38 pixels from the left edge.
101+
The button height is 23 pixels, while the button length is 75 pixels.
102+
The script uses predefined Windows Forms types to determine the button behaviors.
87103

88104
```powershell
89-
$OKButton = New-Object System.Windows.Forms.Button
90-
$OKButton.Location = New-Object System.Drawing.Point(38,165)
91-
$OKButton.Size = New-Object System.Drawing.Size(75,23)
92-
$OKButton.Text = 'OK'
93-
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
105+
$OKButton = New-Object Windows.Forms.Button -Property @{
106+
Location = New-Object Drawing.Point 38, 165
107+
Size = New-Object Drawing.Size 75, 23
108+
Text = 'OK'
109+
DialogResult = [Windows.Forms.DialogResult]::OK
110+
}
94111
$form.AcceptButton = $OKButton
95112
$form.Controls.Add($OKButton)
96113
```
97114

98-
Similarly, you create a **Cancel** button. The **Cancel** button is 165 pixels from the top, but 113 pixels from the left edge of the window.
115+
Similarly, you create a **Cancel** button.
116+
The **Cancel** button is 165 pixels from the top, but 113 pixels from the left edge of the window.
99117

100118
```powershell
101-
$CancelButton = New-Object System.Windows.Forms.Button
102-
$CancelButton.Location = New-Object System.Drawing.Point(113,165)
103-
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
104-
$CancelButton.Text = 'Cancel'
105-
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
119+
$CancelButton = New-Object Windows.Forms.Button -Property @{
120+
Location = New-Object Drawing.Point 113, 165
121+
Size = New-Object Drawing.Size 75, 23
122+
Text = 'Cancel'
123+
DialogResult = [Windows.Forms.DialogResult]::Cancel
124+
}
106125
$form.CancelButton = $CancelButton
107126
$form.Controls.Add($CancelButton)
108127
```
109128

110-
Set the **Topmost** property to **$true** to force the window to open atop other open windows and dialog boxes.
111-
112-
```powershell
113-
$form.Topmost = $true
114-
```
115-
116129
Add the following line of code to display the form in Windows.
117130

118131
```powershell
119132
$result = $form.ShowDialog()
120133
```
121134

122-
Finally, the code inside the **If** block instructs Windows what to do with the form after users select a day on the calendar, and then click the **OK** button or press the **Enter** key. Windows PowerShell displays the selected date to users.
135+
Finally, the code inside the `if` block instructs Windows what to do with the form after users select a day on the calendar, and then click the **OK** button or press the **Enter** key.
136+
Windows PowerShell displays the selected date to users.
123137

124138
```powershell
125-
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
126-
{
139+
if ($result -eq [Windows.Forms.DialogResult]::OK) {
127140
$date = $calendar.SelectionStart
128141
Write-Host "Date selected: $($date.ToShortDateString())"
129142
}

0 commit comments

Comments
 (0)