Академический Документы
Профессиональный Документы
Культура Документы
Table of Contents
Creating Windows Applications with C# -Part 1...................................................................................... 3
Exercise 1 Creating Controls on a Windows Form .............................................................................................4
Exercise 2 Creating and Using Menus ..............................................................................................................14
Creating Windows Applications with C# -Part 1 3
Scenario In this lab, you will create, debug and deploy a Windows application using the
Visual Studio .NET Integrated Development Environment (IDE).
The working solution and associated files for this lab are located in
C:\Microsoft Hands-On-Lab\DEV-HOL02\Solution folder.
Estimated time to
complete this lab: 45
minutes
4 Creating Windows Applications with C# -Part 1
Exercise 1
Creating Controls on a Windows Form
Scenario
In this exercise, you will use various properties of the MonthCalendar control to change its
behavior. You will also handle the DateSelected event of the MonthCalendar control to make the
selected dates bold. Then, you will display the print preview of an empty document using the
PrintPreviewDialog control. You will use the FolderBrowserDialog control to display the folder
browser dialog box and the selected path. Then, you will create a system tray icon using the
NotifyIcon control.
1. Setup Calendar Control. a. Create a new Windows Application project named Lab2-CS using
Microsoft Visual Studio .NET:
Navigate to Start | All Programs | Microsoft Visual Studio
.NET 2003 | Microsoft Visual Studio .NET 2003.
Select File | New | Project menu command or press
CTRL+SHIFT+N.
Click Visual C# Projects in Project Types list.
Click Windows Application in Templates list.
Type Lab2-CS in the Name field and click OK.
Visual Studio .NET creates a default form named Form1.cs. It also creates
Lab2-CS.csproj, Lab2-CS.sln, and AssemblyInfo.cs files.
b. Rename Form1.cs to CreatingControls.cs:
Select the View | Solution Explorer menu command or press
CTRL+ALT+L to open Solution Explorer.
In the Solution Explorer, right-click Form1.cs and select
Rename.
Rename the form CreatingControls.cs. and press ENTER.
c. Double-click the CreatingControls.cs file in Solution Explorer and
select the View | Properties Window menu command or press F4 to
view the properties window of control.
d. Set the following properties:
Name - CreatingControlsForm
Size (Expand the Size node)
Width - 584
Height - 460
StartPosition - CenterScreen
Text - Creating Controls Example
e. Change the code in the Main method:
Select the View | Code menu command or press F7 to switch to
code view of CreatingControlsForm form.
Creating Windows Applications with C# -Part 1 5
FirstDayOfWeek - Monday
ShowTodayCircle - False
ShowWeekNumbers - True
CalendarDimensions (Expand the CalendarDimensions node)
Width -2
Height -1
MaxSelectionCount -5
TitleBackColor - GrayText
TitleForeColor - ControlText
TrailingForeColor - ActiveCaption
x. Drag a Button control from the Toolbox and drop it on the
tabCalendarControl in the form designer.
y. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
z. Set the following properties:
Name - displaySelectedDateButton
Text - Display Selected Dates
The completed form will appear.
aa. Double-click the displaySelectedDateButton button in the designer.
Note: To save on typing a snippets file is located in C:\ Microsoft Hands-
On-Lab\HOL-02.
bb. To display the selected dates, add the following code to
displaySelectedDateButton_Click method or paste snippet1 in the
area specified below:
// Displays message box showing selected
dates.
Private void
displaySelectedDateButton_Click(object
sender, System.EventArgs e)
{
SelectionRange selectedRange =
labMonthCalendar.SelectionRange;
DateTime startDate =
selectedRange.Start.Date;
DateTime endDate =
selectedRange.End.Date;
if(startDate != endDate)
{
MessageBox.Show(string.Format("{0} {1}
{2} {3}", "Selected Date Range: From",
startDate.ToShortDateString(), "to",
endDate.ToShortDateString()), "Lab2 -
Creating Windows Applications");
}
else
{
8 Creating Windows Applications with C# -Part 1
TimeSpan timeSpan =
endDate.Subtract(startDate);
DateTime [] selectedDates = new
DateTime [timeSpan.Days + 1];
labMonthCalendar.BoldedDates =
selectedDates;
}
dd. Select the Debug | Start menu command or press F5 to run the
application.
Click the left or right arrow buttons appearing on the control to go
to the previous or next months.
Click one date and drag to previous or next dates to select
multiple dates at a time.
To display a message box showing the selected date, click
Display Selected Dates.
To close the application, click Close or press ALT+C.
2. Add Print Preview a. To add a Print Preview tab page to the tabControl Control:
functionality. Select the the View | Designer menu command or press
SHIFT+F7 to switch to the form designer.
Click the tab control and select View | Properties Window or
Creating Windows Applications with C# -Part 1 9
labPrintPreviewDialog.Document =
printDocument;
labPrintPreviewDialog.ShowDialog(this);
}
l. Select the Debug | Start menu command or press F5 to run the
application.
m. Click Print Preview tab page.
n. Click Print Preview button.
A new Print Preview window opens showing the print preview of an empty
document.
o. Click Close in the window to close the Print Preview window.
p. Click Close or press ALT+C to close the application.
3. Browse folders using the a. Add Folder Browser Dialog tab page to the tabControl Control:
Folder Browser Dialog. Select the View | Designer menu command or press SHIFT+F7
to switch to the form designer.
Click the tab control and select the View | Properties Window or
press F4 to view the properties of the tabControl.
Make sure that the tabControl control is selected in the
dropdown list in the Properties window.
Locate the TabPages property.
Click the ellipsis (…).
A TabPage Collection Editor dialog box appears.
Click Add to add a tab page to the collection.
Set the Text property to Folder Browser Dialog.
Set the Name property to tabFolderBrowserDialog.
Click OK.
b. Click on the tabFolderBrowserDialog tab page in the form designer.
c. Drag a FolderBrowserDialog control from the Toolbox and drop it on
to the tabFolderBrowserDialog tab page in the form designer.
A FolderBrowserDialog control is added to the Component tray.
d. Set the Name property of the FolderBrowserDialog control to
labFolderBrowserDialog.
e. Drag a Label control from the Toolbox and drop it on the
tabFolderBrowserDialog tab page in the form designer.
f. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Label
control.
g. Set the following properties:
Name - openFolderLabel
Text - Open Folder
h. Drag a TextBox control from the Toolbox and drop it on the
tabFolderBrowserDialog tab page in the form designer.
i. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the TextBox
control.
j. Set the following properties:
Creating Windows Applications with C# -Part 1 11
Name - folderPathTextBox
Text - (none)
k. Drag a Button control to the tabFolderBrowserDialog tab page in the
form designer.
l. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
m. Set the following properties:
Name - browseButton
Text - Browse
n. Drag another Button control from the Toolbox and drop it on the
tabFolderBrowserDialog tab page in the form designer.
o. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
p. Set the following properties:
Name - selectedFolderPathButton
Text - Selected Folder Path
The completed form should appear.
q. Double-click the browseButton button in the designer and add the
following code to browseButton_Click method or paste snippet4 in
the area specified below:
private void browseButton_Click(object sender,
System.EventArgs e)
{
labFolderBrowserDialog.ShowDialog(this);
folderPathTextBox.Text =
labFolderBrowserDialog.SelectedPath;
}
r. Select the View | Designer menu command to switch to the form
designer.
s. Double-click selectedFolderPathButton button in the designer and
add the following code to the selectedFolderPathButton_Click
method or paste snippet5 in the area specified below:
private void
selectedFolderPathButton_Click(object sender,
System.EventArgs e)
{
if(folderPathTextBox.Text.Length != 0)
{
MessageBox.Show(folderPathTextBox.Text,
"Lab2 - Creating Windows Applications");
}
else
{
MessageBox.Show("Please select a
folder by clicking 'Browse' button!", "Lab2 -
Creating Windows Applications");
}
}
t. Select the Debug | Start menu command or press F5 to run the
12 Creating Windows Applications with C# -Part 1
application.
u. Click the Folder Browser Dialog tab page.
v. Click Browse.
The Browser For Folder dialog box appears.
w. Select a folder from the dialog box and click OK.
x. Click Selected Folder Path to view a message box displaying the
selected path.
y. Click OK.
z. Click Browse again.
The dialog box auto-selects the previously selected folder.
aa. Click Close or press ALT+C to close the application.
4. Create System Tray icons. a. To add System Tray Icon tab page to the tabControl control:
Select the View | Designer menu command or press SHIFT+F7
to switch to the form designer.
Click the tab control and select the View | Properties Window or
press F4 to view the properties of the tabControl.
Make sure that tabControl control is selected in the dropdown
list in Properties window.
Locate TabPages property at the bottom of the Properties
window.
Click the ellipsis (…).
A TabPage Collection Editor dialog box appears.
Click Add to add a tab page to the collection.
Set Text property to System Tray Icon.
Set Name property to tabSystemTrayIcon.
Click OK.
b. Click on the tabSystemTrayIcon tab page in the form designer.
c. Drag a NotifyIcon control from the Toolbox and drop it on to the
tabSystemTrayIcon tab page in the form designer.
A NotifyIcon control is added to the Component tray.
d. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of NotifyIcon
control.
e. Set the following properties:
Name - systemTrayIcon
Text - System Tray Icon
f. Set Icon property of systemTrayIcon control.
g. Locate Icon property.
h. Click the ellipsis (…). File Open dialog box appears.
i. Locate and select the APPTL.ICO file in C:\Microsoft Hands-On-
Lab\HOL-02\ Solution\Lab2-CS folder.
j. Click Open.
k. Drag a Button control from the Toolbox and drop it on the
tabSystemTrayIcon tab page in the form designer.
Creating Windows Applications with C# -Part 1 13
Exercise 2
Creating and Using Menus
Scenario
In this exercise, you will create a main menu on the form to navigate to different tab pages in the
form. Then, you will add a context menu to the system tray icon, which you have created in the
previous exercise.
1. Create a Top-Level Menu. a. Select the View | Designer menu command or press SHIFT+F7 to
switch to the form designer.
b. Drag a MainMenu control from the Toolbox to the form designer or
double-click the MainMenu component in the Toolbox.
A menu displaying the text Type Here is added to the form and a
MainMenu component is added to the Component tray.
c. Click Type Here menu item.
d. Type &Go and press ENTER.
Another menu item, displaying the text Type Here appears.
Note: By prefixing the text by &, you can create an access key. For
example, you can access the menu &Go at run time by pressing ALT+G.
e. Click menu item appearing below &Go menu item and type
&Calendar Control to add a Calendar Control menu item.
f. Select the View | Properties Window or press F4 to view the
properties for this menu item.
g. Set Name property to menuItemCalendarControl.
h. Click menu item appearing below menuItemCalendarControl and
type &System Tray to add a System Tray menu item.
i. Select the View | Properties Window or press F4 to view the
properties for this menu item.
j. Set Name property to menuItemSystemTray.
k. Click the menu item appearing on the right side of the
menuItemSystemTray menu item and type Display as Sys Tray Icon
to add a sub menu item to System Tray menu item.
l. Select View | Properties Window or press F4 to view the properties
for this menu item.
m. Set Name property to menuItemDisplaySysTrayIcon.
n. Click menu item appearing below menuItemSystemTray and type
&Print Preview to add Print Preview menu item.
o. Select the View | Properties Window or press F4 to view the
properties for this menu item.
p. Set Name property to menuItemPrintPreview.
q. Click the menu item appearing below menuItemPrintPreview and
type &Folder Browser Dialog to add the Folder Browser Dialog
Creating Windows Applications with C# -Part 1 15
menu item.
r. Select the View | Properties Window menu command or press F4 to
view the properties for this menu item.
s. Set the Name property to menuItemFolderBrowserDialog.
The menu should appear.
t. Add code to menuItemCalendarControl menu item’s Click event.
Click the menu item and select the View | Properties Window
menu command or press F4 to view the properties window of the
menuItemCalendarControl menu item.
Click in the Properties window of the
menuItemCalendarControl control to list all the events of the
control.
Double-click the Click event.
Add the following code or paste snippet7 in the area specified
below:
private void
menuItemCalendarControl_Click(object sender,
System.EventArgs e)
{
tabControl.SelectedTab =
tabControl.TabPages[0];
menuItemCalendarControl.Enabled = false;
menuItemSystemTray.Enabled = true;
menuItemPrintPreview.Enabled = true;
menuItemFolderBrowserDialog.Enabled =
true;
}
u. Select the View | Designer menu command or press SHIFT+F7 to
switch to design view.
v. Add code to menuItemDisplaySysTrayIcon menu item‘s Click event:
Click the menu item and select the View | Properties Window
menu command or press F4 to view the properties window of the
menuItemDisplaySysTrayIcon menu item.
Locate Click event and double-click the same.
Add the following code:
private void
menuItemDisplaySysTrayIcon_Click(object sender,
System.EventArgs e)
{
displayAsSystemTrayIcon();
}
w. Select the View | Designer menu command or press SHIFT+F7 to
switch to the design view.
x. Add code to menuItemPrintPreview menu item’s Click event:
Click the menu item and select View | Properties Window menu
command or press F4 to view the properties window of
menuItemPrintPreview menu item.
Locate the Click event and double-click the same.
Add the following code or paste snippet8 in the area specified
below:
private void menuItemPrintPreview_Click(object
16 Creating Windows Applications with C# -Part 1
sender, System.EventArgs e)
{
tabControl.SelectedTab =
tabControl.TabPages[1];
menuItemCalendarControl.Enabled = true;
menuItemSystemTray.Enabled = true;
menuItemPrintPreview.Enabled = false;
menuItemFolderBrowserDialog.Enabled =
true;
}
y. Select the View | Designer menu command or press SHIFT+F7 to
switch to the design view.
z. Add code to menuItemFolderBrowserDialog menu item’s Click
event.
Click the menu item and select View | Properties Window menu
command or press F4 to view the properties window of
menuItemFolderBrowserDialog menu item.
Locate the Click event and double-click the same.
Add the following code or paste snippet9 in the area specified
below:
private void
menuItemFolderBrowserDialog_Click(object
sender, System.EventArgs e)
{
tabControl.SelectedTab =
tabControl.TabPages[2];
menuItemCalendarControl.Enabled = true;
menuItemSystemTray.Enabled = true;
menuItemPrintPreview.Enabled = true;
menuItemFolderBrowserDialog.Enabled =
false;
}
aa. Select the View | Designer menu command or press SHIFT+F7 to
switch to the design view.
bb. Add code to form’s Load event:
Click the form and select the View | Properties Window menu
command or press F4 to view the properties window of form.
Double-click the Load property.
Add the following code or paste snippet10 in the area specified
below:
private void CreatingControlsForm_Load(object
sender, System.EventArgs e)
{
menuItemCalendarControl.Enabled = false;
menuItemSystemTray.Enabled = true;
menuItemPrintPreview.Enabled = true;
menuItemFolderBrowserDialog.Enabled =
true;
}
cc. Select the Debug | Start menu command or press F5 to run the
application.
dd. Select different menu items and check the functionality for each menu
item.
ee. Click Close or press ALT + C to close the application.
Creating Windows Applications with C# -Part 1 17
2. Add Context Menu a. Select the View | Designer menu command or press SHIFT+F7 to
functionality. switch to the design view.
b. Drag a ContextMenu control from the Toolbox to the form designer
or double-click the ContextMenu component.
A ContextMenu control is added to the Component tray.
c. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the
ContextMenu control.
d. Click the icon in the Properties window to switch to properties of
the control.
e. Set the Name property of the ContextMenu control to
labContextMenu.
f. Click the Context Menu menu item in the form.
A Type Here menu item appears below the Context Menu.
g. Click the Type Here menu item and type &About to add an About
menu item.
h. Select the View | Properties Window or press F4 to view the
properties for this menu item.
i. Set the Name property to menuItemAbout.
j. Click the Type Here menu item appearing below the menuItemAbout
menu item and type - to add a separator on the menu bar.
k. Select the View | Properties Window or press F4 to view the
properties for this menu item.
l. Set the Name property to menuItemSeperator.
m. Click the Type Here menu item appearing below menuItemSeperator
and type &Show to add a Show menu item.
n. Select the View | Properties Window or press F4 to view the
properties for this menu item.
o. Set the Name property to menuItemShow.
Note: By setting the text to -, you can create a separator in the menu bar.
p. Add code to the menuItemAbout menu item’s Click event:
Click the menu item and select the View | Properties Window
menu command or press F4 to view the properties window of the
menuItemAbout menu item.
Click in the Properties window of the menuItemAbout
control to list all the events of the control.
Locate the Click event and double-click the same.
Add the following code:
private void menuItemAbout_Click(object sender,
System.EventArgs e)
{
MessageBox.Show("Lab2 - Creating
Windows Applications");
}
q. Select the View | Designer menu command or press SHIFT+F7 to
switch to the design view.
r. Add code to the menuItemShow menu item’s Click event:
Click the menu item and select the View | Properties Window
18 Creating Windows Applications with C# -Part 1