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
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Text;
7 using System.Windows.Forms;
8 using System.Reflection;
9
10 namespace MyOwnBrowser
11 {
12 public partial class Splash : Form
13 {
14 public Splash()
15 {
16 InitializeComponent();
86
Microsoft Visual C# 2005 Express Edition: Build a Program Now!
C06622132.indd 86
C06622132.indd 86
10/24/05 3:37:14 PM
10/24/05 3:37:14 PM
17 this.lblApplicationTitle.Text = AssemblyTitle;
18 this.lblCopyright.Text = AssemblyCopyright;
19 this.lblVersion.Text = “Version: “ + AssemblyVersion;
20 }
21
22 #region Assembly Attribute Accessors
23
24 public string AssemblyTitle
25 {
26 get
27 {
28 // Get all Title attributes on this assembly
29 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(
30 typeof(AssemblyTitleAttribute), false);
31 // If there is at least one Title attribute
32 if (attributes.Length > 0)
33 {
34 // Select the first one
35 AssemblyTitleAttribute titleAttribute = (AssemblyTitleAttribute)attributes[0]; 36 // If it is not an empty string, return it
37 if (titleAttribute.Title != “”)
38 return titleAttribute.Title;
39 }
40 // If there was no Title attribute, or if the Title attribute was the empty string, return the .exe name 41 return System.IO.Path.GetFileNameWithoutExtension(Assembly.GetExecutingAssembly().CodeBase); 42 }
43 }
44
45 public string AssemblyVersion
46 {
47 get
48 {
49 return Assembly.GetExecutingAssembly().GetName().Version.ToString(); 50 }
51 }
52
53 public string AssemblyDescription
54 {
55 get
56 {
57 // Get all Description attributes on this assembly
Chapter 6: Modify Your Web Browser Now!
87
C06622132.indd 87
C06622132.indd 87
10/24/05 3:37:15 PM
10/24/05 3:37:15 PM
58 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(
59 typeof(AssemblyDescriptionAttribute), false);
60 // If there aren’t any Description attributes, return an empty string 61 if (attributes.Length == 0)
62 return “”;
63 // If there is a Description attribute, return its value 64 return ((AssemblyDescriptionAttribute)attributes[0]).Description; 65 }
66 }
67
68 public string AssemblyProduct
69 {
70 get
71 {
72 // Get all Product attributes on this assembly
73 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(
74 typeof(AssemblyProductAttribute), false);
75 // If there aren’t any Product attributes, return an empty string 76 if (attributes.Length == 0)
77 return “”;
78 // If there is a Product attribute, return its value 79 return ((AssemblyProductAttribute)attributes[0]).Product; 80 }
81 }
82
83 public string AssemblyCopyright
84 {
85 get
86 {
87 // Get all Copyright attributes on this assembly
88 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(
89 typeof(AssemblyCopyrightAttribute), false);
90 // If there aren’t any Copyright attributes, return an empty string 91 if (attributes.Length == 0)
92 return “”;
93 // If there is a Copyright attribute, return its value 94 return ((AssemblyCopyrightAttribute)attributes[0]).Copyright; 95 }
96 }
97
98 public string AssemblyCompany
99 {
88
Microsoft Visual C# 2005 Express Edition: Build a Program Now!
C06622132.indd 88
C06622132.indd 88
10/24/05 3:37:17 PM
10/24/05 3:37:17 PM
100 get
101 {
102 // Get all Company attributes on this assembly
103 object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(
104 typeof(AssemblyCompanyAttribute), false);
105 // If there aren’t any Company attributes, return an empty string
Figure 6-6
106 if (attributes.Length == 0)
The splash screen in action
107 return “”;
108 // If there is a Company attribute, return its value 109 return ((AssemblyCompanyAttribute)attributes[0]).
110 Company;
111 }
112 }
113 #endregion
114 }
115 }
Save the application (Ctrl+Shift+S saves all files and Ctrl+S saves the current 7 file) and press F5 to run it. Look at Figure 6-6 to see the splash screen in action with the dynamic information collected from the Project Designer Application pane.
Interacting Through Dialog Boxes
The dialog boxes you create help the user interact with the software. They are additional forms that you add to your application. In this section, you’ll add two dialog boxes to your Web browser: an About box and a Navigate dialog box.
Adding an About Box Dialog
The first dialog box you’ll add is an About box, which exists in most Windows applications. This dialog box essentially contains the same information as the splash screen, but sometimes contains more legal, system, and version information. You’ll also prepare the application for a transformation into a more feature-rich Internet browser.
Chapter 6: Modify Your Web Browser Now!
89
C06622132.indd 89
C06622132.indd 89
10/24/05 3:37:18 PM
10/24/05 3:37:18 PM
TO ADD AN ABOUT BOX DIALOG
On the Browser form, delete the
txtURL
and the
btnGo
controls. Delete the
btnGo_Click
event han1 dler by removing its signature and content from the Browser.cs file. On the Browser form, select the
Web Browser
control, and using the smart tag, select
Dock In
2
Parent Container
.
As you did for the splash screen, add a new item to your project, but this time when presented with 3 the templates, choose the
About Box
template and name it
AboutBox.cs
. Similar to the splash screen, the About box will be populated with information from the project settings from the Project Designer window. At this point, if you run the application, there is no link between your About box and the rest of your browser, so it won’t show up anywhere. Usually, the About box shows up when you request it from the Help menu, so you’ll add this missing link now.
TO LINK THE ABOUT BOX TO THE HELP MENU
Select the
Browser.cs [Design]*
tab
to return to the Browser Form Design view. Drag a
MenuStrip
1 control from the toolbox onto the design surface to add a Menu Strip control to the Browser form. An empty menu appears on the form and a component appears in the component tray. Name it
msBrowser
. To add the Help menu, select the menu strip on the form, click the smart tag, and then select
Insert
2
Standard Items
. You’ll get a familiar Windows application menu strip and its menu choices with their submenus, icons, and keyboard shortcuts.
Delete all menu choices
except
the Help menu and the About. . . menu choices under the Help menu. 3 To perform this clean-up, select any menu choice, then right-click to bring up the contextual menu and select
Delete
to remove it. Also remove the menu separators (that is, the lines separating menu choices).
90
Microsoft Visual C# 2005 Express Edition: Build a Program Now!
C06622132.indd 90
C06622132.indd 90
10/24/05 3:37:19 PM
10/24/05 3:37:19 PM
To wire the new About box form with the About. . . menu choice, double-click the
About. . .
menu 4 choice to get to the AboutToolStripMenuItem_Click event handler.
Add the following line of code to the Browser.cs file just above the browser constructor, i.e., public 5 Browser(). Adding this line of code will let you exchange data with this form if needed. AboutBox myAboutBox = new AboutBox();
Then add the following code to the AboutToolStripMenuItem_Click event handler.:
I modified the Assembly
myAboutBox.ShowDialog();
Information fields in the Project
Designer to come up with the
information displayed in the
Save the application and then run it. Select
About. . .
from the Help menu. The screen should resem
About box. You can do the same.
6
You simply add or modify the con-
ble Figure 6-7. The ShowDialog() method brings up the form in the middle of the executing applica
tent in the Description, Company,
tion, and nothing else can happen until you click one of its buttons or the red
X
to close the dialog box. In
Product, and Copyright fields.
this case, it has only the OK button.
Figure 6-7
About box dialog showing up in your
newly refined browser application
Chapter 6: Modify Your Web Browser Now!
91
C06622132.indd 91
C06622132.indd 91
10/24/05 3:37:20 PM
10/24/05 3:37:20 PM
You’re probably wondering why the application worked when you clicked the OK button
As you probably saw in the
even though you didn’t write any code to handle this event. This is an example of the pro
object-oriented tutorial movie
(go to
http://go.microsoft.com/
ductivity gains you'll get when using templates. The template includes the code to handle
fwlink/?linkid=44030&clcid=0x409
the click button event. Review the source code for the dialog box by right-clicking the
and look at lesson 6, parts 1 and
2),
this
means the current instance
AboutBox.cs filename in the Solution Explorer and selecting View Code.
of an object. In this case, this is
Now that you’ve added the About box dialog, it should be easy to add another that will
an instance of the AboutBox class.
this
is used to access all the public
allow your users to navigate to Web pages.
fields, properties, and methods
defined in the class. In this exam-
ple,
this
is allowing you to assign