Read Microsoft Visual C# 2005 Express Edition: Build a Program Now! Online

Authors: Patrice Pelland

Tags: #General, #Computers, #C♯ (Computer program language), #Programming Languages, #C#, #Microsoft .NET Framework, #Computer Books: Languages, #Computer Graphics, #Application software, #C# (Computer program language), #Programming, #Microsoft Visual C# .NET, #Microsoft Visual C♯ .NET, #Electronic books, #Game Programming & Design, #Computing: Professional & Programming, #C (Computer program language), #Computers - Languages, #Programming Languages - C#, #Programming & scripting languages: general

Microsoft Visual C# 2005 Express Edition: Build a Program Now! (39 page)

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
5.71Mb size Format: txt, pdf, ePub
ads

169

CSX_Chapter 9.indd 169

CSX_Chapter 9.indd 169

10/24/05 6:59:17 PM

10/24/05 6:59:17 PM

N O T E

TO CREATE A NOTIFYICON CONTROL

The NotifyIcon control does not

have a design representation on

1 In the Toolbox from the Common Controls group, drag a
NotifyIcon
on the form. It appears in the
the form surface, so it is added

component tray. Name the control
notifyWeather
.

to the component tray at the

same place where the ADO.NET

components were added in the

previous chapter.

2 Change its text property to
Weather Tracker
.

In the Toolbox from the Menus & Toolbars group, drag a
Context Menu Strip
onto the form and 3 name it
cmsNotify
.

Using the smart tag on the cmsNotify control in the component tray, select
Edit items . . .
The Items 4 Collection Editor appears.

5 In the Items Collection Editor, change the cmsNotify control’s properties using the following table:
Property

Value

BackColor

System:Gradient

InactiveCaption

ShowImageMargin

False

From the Select Item And Add To List Below drop-down list on the left, select
MenuItem
and click the 6
Add
button. Change the control’s properties using the following table:
Property

Value

(Name)

tsmiAbout

Text

About . . .

ForeColor

System:MenuHighlight

From the Select Item And Add To List Below drop-down list, select
Separator
and click the
Add
but7 ton. Change its ForeColor property to
System:MenuHighlight
.
170

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter 9.indd 170

CSX_Chapter 9.indd 170

10/24/05 6:59:18 PM

10/24/05 6:59:18 PM

From the Select Item And Add To List Below drop-down list, select
MenuItem
and click the
Add
8 button. Change the control’s properties using the following table:

Property

Value

(Name)

tsmiRefresh

Text

Refresh Weather Info

ForeColor

System:MenuHighlight

From the Select Item And Add To List Below drop-down list, select
Separator
and click the
Add
9 button. Change its ForeColor property to
System:MenuHighlight
. 10 From the Select Item And Add To List Below drop-down list, select
MenuItem
and click the
Add
button. Change the control’s properties using the following table:

Property

Value

(Name)

tsmiOptions

Text

Options …

ForeColor

System:MenuHighlight

11 From the Select Item And Add To List Below drop-down list, select
Separator
and click the
Add
button. Change its ForeColor property to
System:MenuHighlight
. 12 From the Select Item And Add To List Below drop-down list, select
MenuItem
and click the
Add
button. Change the control’s properties using the following table:

Property

Value

(Name)

tsmiOpen

Text

Open…

ForeColor

System:MenuHighlight

Chapter 9: Build Your Own Weather Tracker Application Now!

171

CSX_Chapter 9.indd 171

CSX_Chapter 9.indd 171

10/24/05 6:59:18 PM

10/24/05 6:59:18 PM

13 From the Select Item And Add To List Below drop-down list, select
MenuItem
and click the
Add
button. Change the control’s properties using the following table:

Property

Value

(Name)

tsmiExit

Text

Exit

ForeColor

System:MenuHighlight

You're finished adding items to the context menu strip. The Items Collection Editor should look like Figure 9-2.

Figure 9-2

Items for the context menu

14 Click
OK
to close the Items Collection Editor.

You now need to associate the context menu strip with the notifyWeather control, which is fairly easy to do. 15 Select the
notifyWeather
control in the component tray. In the Properties window, change the ContextMenuStrip property to
cmsNotify
.

Let's say you are currently acting as the user. In order for you to be able to click on the application when it’s in the notification area, your notifyWeather control needs an icon. The icon will later become dynamically generated by your application, and the icon will become the current temperature. Therefore, you now need to associate a temporary icon with the application, otherwise you would not be able to select it in the notification area.
172

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter 9.indd 172

CSX_Chapter 9.indd 172

10/24/05 6:59:19 PM

10/24/05 6:59:19 PM

16 In the Properties window for nofityWeather, set the Icon property to
otheroptions.ico
. This file is located in a folder named Images under the Chapter9 directory where you installed the companion content.

Figure 9-3

Context menu of the

17 Press
F5
to execute the application.

notifyWeather control

You should see this icon in your notification area:

. If you right-click on this icon, you

should see the context menu shown in Figurte 9-3.

When you are finished, the only way to stop the application is to click the blue Stop Debugging button in the Visual Studio toolbar. You will now add another way to stop the application.

TO STOP AN APPLICATION

1 Select the
tsmiExit
control from the Properties window’s drop-down list. Click on the
event
icon (yellow lightning) in the Properties window and then double-click the
click
2 event. The Code view appears.

Edit the tsmiExit_Click event handler and add the Shutdown method as shown in the following code. A 3 Shutdown method is created because you always want to make your code re-usable, and a Shutdown method will enable you to do this.

1 private void tsmiExit_Click(object sender, EventArgs e)

2 {

3 this.Shutdown();

4 }

5

6 private void Shutdown()

7 {

8 if (notifyWeather.Visible)

9 {

10 notifyWeather.Visible = false;

11 }

12 Application.Exit();

13 }

Chapter 9: Build Your Own Weather Tracker Application Now!

173

CSX_Chapter 9.indd 173

CSX_Chapter 9.indd 173

10/24/05 6:59:19 PM

10/24/05 6:59:19 PM

The first instruction of the Shutdown method will verify if the notifyWeather control is visible; if it is, make the notify icon disappear from the notification area. The last line will terminate the application. You will now be able to select the Exit menu choice from the context menu to terminate the application; you won't need to use the Stop Debugging button. You can try your application by pressing
F5
to verify whether the Exit menu choice works as expected..

Now you can exit from your application, but you don't have a way to open the Main form, which will have the weather information. To do this, you will want to link the double-click event of the notifyWeather control icon in the notification area to the action of opening the Main form in the middle of the screen. In Design view, select the
notifyWeather
control in the component tray. In the events list of 4 the Properties window, double-click the
MouseDoubleClick
event. Edit the notifyWeather_

MouseDoubleClick event handler and add the Restore method as shown in the following code.: 14 private void notifyWeather_MouseDoubleClick(object sender, MouseEventArgs e) 15 {

16 this.Restore();

17 }

18

19 private void Restore()

20 {

21 if (this.WindowState == FormWindowState.Minimized)

22 {

23 this.WindowState = FormWindowState.Normal;

24 }

25 this.Visible = true;

26 }

Again, a private method called Restore() was created in case you need it elsewhere in your application. The first line of code in the Restore method is there because it is impossible to know in which context your method will be called. In your case, when you created the form, you set the WindowState property to minimized and ShowInTaskbar to false so that the form starts minimized and the user doesn’t see it. When you start the application, the first time the user selects the Open… menu choice (you’ll code this soon) or double-clicks the notifyWeather control, the user won’t be able to see the form if you only have set its Visible property to true. Therefore, you need to verify in which WindowState the form appears. If it’s still minimized, you need to set it to Normal so that the focus is on the Main form.

Press
F5
to test the changes. Double-click the
notifyWeather
icon in the notification area and the 5 Main form should appear.

174

Microsoft Visual C# 2005 Express Edition: Build a Program Now!

CSX_Chapter 9.indd 174

CSX_Chapter 9.indd 174

10/24/05 6:59:20 PM

10/24/05 6:59:20 PM

Now let’s see what happens if the user clicks the Close button (red X). If you close the application by using the Close button, it closes permanently. Yet, our design requirements state that the application should simply minimize back to the notification area when the user clicks the Close button. Therefore, you’ll now intercept an event that occurs just before the form is closed and just before the form object is deleted, which is an event called the FormClosing event. By using a FormClosing event, you can extract the reason for the form’s closing and in this way intercept the event when the user clicks the Close button. In Design view, select the
Main
form. Go to the events list in the Properties window and double-click 6 the
FormClosing
event. Add the following code to the Main_FormClosing event handler. 27 private void Main_FormClosing(object sender, FormClosingEventArgs e) 28 {

29 if (e.CloseReason == CloseReason.UserClosing)

30 {

31 e.Cancel = true;

32 this.Hide();

33 }

M O R E I N F O

34 }

If you want to learn more about

why a form might be closing, you

Part of the event is the FormClosingEventArgs, which contains the arguments that accompany the event
can search the Help system by

BOOK: Microsoft Visual C# 2005 Express Edition: Build a Program Now!
5.71Mb size Format: txt, pdf, ePub
ads

Other books

The Mask of Fu-Manchu by Sax Rohmer
Self-Sacrifice by Struan Stevenson
Unknown by Poppy
The Devil and Danna Webster by Jacqueline Seewald
Love Me With Fury by Taylor, Janelle
Cait and the Devil by Annabel Joseph
The House at Tyneford by Natasha Solomons