Вы находитесь на странице: 1из 18

Microsoft Virtual Labs

Creating Windows Applications


with C# -Part 1
2 Creating Windows Applications with C# -Part 1

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

Creating Windows Applications with


C# -Part 1

Objectives After completing this lab, you will know about:


„ Use MonthCalendar, PrintPreviewDialog, FolderBrowserDialog, and
NotifyIcon controls.
„ Create Main Menu and Context Menu on a Windows Form
Note: Because this lab focuses on concepts, it may not comply with Microsoft
security recommendations.

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.

Tasks Detailed steps

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

ƒ Locate the Main method in the form.


ƒ In this method, only one line of code exists:
static void Main()
{
Application.Run(new Form1());
}
ƒ Modify the code to look as follows:
static void Main()
{
Application.Run(new CreatingControlsForm());
}
f. Add a TabControl control to the form:
ƒ Select the View | Designer menu command or press SHIFT+F7
to switch back to design view.
ƒ Select the View | Toolbox menu command or press
CTRL+ALT+X to view Toolbox.
ƒ Double-click the TabControl control on the Windows Forms tab
in the Toolbox.
g. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the
TabControl control.
h. Set the following properties:
ƒ Name - tabControl
ƒ Location (Expand the Location node)
ƒ X - 24
ƒ Y - 16
ƒ Size (Expand the Size node)
ƒ Width - 528
ƒ Height - 312
i. Add the Calendar Control tab page to the tabControl control:
ƒ Locate the TabPages property at the bottom of the Properties
window.
ƒ Click the ellipsis (…).
A TabPage Collection Editor dialog box appears.
ƒ Click Add.
A new tab page tabpage1 appears in the Members listbox.
ƒ Set Text property of tabpage1 to Calendar Control.
ƒ Set Name property of tabpage1 to tabCalendarControl.
ƒ Click OK.
j. Select the View | Toolbox menu command or press CTRL+ALT+X to
view Toolbox.
k. Drag a Label control from the Toolbox and place it on the
tabCalendarControl 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 Label
control.
6 Creating Windows Applications with C# -Part 1

m. Set the following properties:


ƒ Name - calendarControlHeaderLabel
ƒ Font (Expand the Font node)
ƒ Name - Microsoft Sans Serif
ƒ Bold - True
ƒ Size - 8.25
ƒ Unit - Point
ƒ Size (Expand the Size node)
ƒ Width - 528
ƒ Height - 312
ƒ Text - To select multiple dates, click one date and
drag the mouse to another date. The maximum number of
dates that can be selected depends on the control settings. The
selected dates have to be continuous
n. Select the label control and align it with the borders of the calendar
control.
o. Select the View | Toolbox menu command or press CTRL+ALT+X to
view the Toolbox.
p. Drag a Button control from the Toolbox and drop it on the form.
q. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
r. Set the following properties:
ƒ Name - closeButton
ƒ Text - &Close
s. To write the code for the Click event of Close button:
ƒ Double click Close button in the form designer. It takes you to
closeButton_Click method in the code view of the form.
ƒ Write the following line of code in the closeButton_Click
method.
private void closeButton_Click(object sender,
System.EventArgs e)
{
this.Close();
}
Note: Visual Studio .NET automatically adds event handler function body
to respond to events.
t. Select the View | Designer menu command to switch to the form
designer.
u. Drag the MonthCalendar control from the Toolbox and drop it on the
tabCalendarControl tab page in the form designer.
v. Click the control and select the View | Properties Window menu
command or press F4 to view the properties of the MonthCalendar
control.
w. Set the following properties:
ƒ Name - labMonthCalendar
Creating Windows Applications with C# -Part 1 7

ƒ 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

MessageBox.Show("Selected Date: "


+ startDate.ToShortDateString(), "Lab2 -
Creating Windows Applications");
}
}
cc. To make the selected dates bold:
ƒ Select the View | Designer menu command to switch to the form
designer.
ƒ Click icon in the Properties window of the
labMonthCalendar control.
ƒ Locate DateSelected event and double-click the same. This takes
you to labMonthCalendar_DateSelected method in the code
view.
ƒ Add the following code to the DateSelected event of the month
calendar control or paste snippet2 in the area specified below:
// Makes the selected dates bold
private void
labMonthCalendar_DateSelected(object sender,
System.Windows.Forms.DateRangeEventArgs e)
{
DateTime startDate =
labMonthCalendar.SelectionStart.Date;
DateTime endDate =
labMonthCalendar.SelectionEnd.Date;

TimeSpan timeSpan =
endDate.Subtract(startDate);
DateTime [] selectedDates = new
DateTime [timeSpan.Days + 1];

for (int index = 0; index <


selectedDates.Length; index++)
{
selectedDates[index] =
startDate.AddDays(Convert.ToDouble(index));
}

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

press F4 to view the properties of the tabControl.


ƒ Make sure that tabControl control is selected in the dropdown
list in Properties window.
The Properties window will show events.
ƒ Click the icon in the Properties window to switch to
properties of the control.
ƒ Locate the TabPages property at the bottom of the Properties
window.
ƒ Click the ellipsis (…).
A TabPage Collection Editor dialog box appears.
ƒ To add a tab page to the collection, click the Add button.
ƒ Set Text property to Print Preview.
ƒ Set Name property to tabPrintPreview.
ƒ Click OK.
b. Click on the tabPrintPreview tab page in the form designer.
c. Drag a PrintPreviewDialog control from the Toolbox and drop it on
the tabPrintPreview tab page.
A PrintPreviewDialog control is added to the Component tray.
d. Click the control and select View | Properties Window menu
command or press F4 to view the properties window of
PrintPreviewDialog control.
e. Set the properties of the PrintPreviewDialog control:
ƒ Set Name property to labPrintPreviewDialog.
f. Drag a Button control from the Toolbox and drop it on the
tabPrintPreview tab control in the form designer.
g. Click the control and select View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
h. Set the following properties:
ƒ Name - btnPrintPreview
ƒ Text - Print Preview
The completed form should appear.
i. Double-click btnPrintPreview button in the designer.
j. To import Printing namespace, include following code at the top of the
form, above namespace Lab2_CS.
using System.Drawing.Printing;
k. Add the following code to the method btnPrintPreview_Click to
preview an empty document or paste snippet3 in the area specified
below:
private void btnPrintPreview_Click(object
sender, System.EventArgs e)
{
PrintDocument printDocument = new
PrintDocument();

// Write code to prepare the print


document
10 Creating Windows Applications with C# -Part 1

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

l. Click the control and select View | Properties Window menu


command or press F4 to view the properties window of Button control.
m. Set the following properties:
ƒ Name - systemTrayIconButton
ƒ Text - Display as System Tray Icon
The completed form should appear.
n. Double-click systemTrayIconButton button in the designer and add
the following code to the method systemTrayIconButton_Click:
private void systemTrayIconButton_Click(object
sender, System.EventArgs e)
{
displayAsSystemTrayIcon();
}
o. Add the following code to the form to add displayAsSystemTrayIcon
method definition or paste snippet6 in the area specified below.
private void displayAsSystemTrayIcon()
{
string message = "Double-click or right-
click the icon in the system tray to maximize
the form!";

MessageBox.Show(message, "Lab2 - Creating


Windows Applications");
systemTrayIcon.Visible = true;
this.Visible = false;
}
p. Select the View | Designer menu command or press SHIFT+F7 to
switch to the design view of the form.
q. Click the SystemTrayIcon control and select the View | Properties
Window menu command or press F4 to view the properties window of
the systemTrayIcon control.
r. Click in the Properties window of the systemTrayIcon control to
list all the events of the control.
s. Locate DoubleClick event and double-click the same. This takes you
to systemTrayIcon_DoubleClick method in the code view.
Note: We will add code for Right Click in a later exercise.
t. Add the following code to systemTrayIcon_DoubleClick method:
private void systemTrayIcon_DoubleClick(object
sender, System.EventArgs e)
{
this.Visible = true;
systemTrayIcon.Visible = false;
}
u. Select Debug | Start menu command or press F5 to run the
application.
v. Click System Tray Icon tab page.
w. Click Display As System Tray. A message box is displayed. Click
OK to hide the form and display it as system tray icon.
x. Double-click the icon in the system tray.
The form appears.
y. Click Close or press ALT+C to close the application.
14 Creating Windows Applications with C# -Part 1

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.

Tasks Detailed steps

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

menu command or press F4 to view the properties window of a


menuItemAbout menu item.
ƒ Locate the Click event and double-click the same.
ƒ Add the following code:
private void menuItemShow_Click(object sender,
System.EventArgs e)
{
this.Visible = true;
systemTrayIcon.Visible = false;
}
s. Add the following code to displayAsSystemTrayIcon method.
private void displayAsSystemTrayIcon()
{
string message = "Double-click or
right-click the icon in the system tray to
maximize the form!";

MessageBox.Show(message, "Lab2 - Creating


Windows Applications");
systemTrayIcon.Visible = true;
systemTrayIcon.ContextMenu =
labContextMenu;
this.Visible = false;
}
t. Select the Debug | Start menu command or press F5 to run the
application.
u. Click the System Tray Icon tab page.
v. Click Display As System Tray to hide the form and display it as
system tray icon.
A message box displays.
w. Click OK.
x. Right-click system tray icon and Select About.
A message box appears.
y. Click OK.
z. Right-click system tray icon and select Show.
The form appears.
aa. Click Close or press ALT+C to close the application.

Вам также может понравиться