Menustrip Button Combobox Webbrowser: To Create A C# Windows Application
Menustrip Button Combobox Webbrowser: To Create A C# Windows Application
Menustrip Button Combobox Webbrowser: To Create A C# Windows Application
The purpose of this topic is to acquaint you with elements of the Visual C# Express Edition integrated
development environment (IDE) as you use Windows Forms to build a relatively straightforward C#
program. Windows Forms provide your project with components, such as dialog boxes, menus, buttons, and
many other controls, that make up a standard Windows application user interface (UI). Fundamentally,
these controls are just classes from the .NET Framework class library. The view in Visual C#
Express Edition enables you to drag the controls onto your application's main form and adjust their size and
position. As you do this, the IDE automatically adds the source code to create an instance of the appropriate
class and initialize it.
This example shows you how to create your own Web browser application, which you can customize with
shortcuts to your favorite Web sites.
For a video version of this topic, see Video How to: Create a C# Windows Forms Application.
When you are moving controls around a Windows Form, you will see blue lines appear. These lines are
guides that help you line up the controls vertically and horizontally. You can also line up controls by
selecting more than one at a time. You can do this by clicking and dragging a selection box around the
controls or by holding down SHIFT as you click them. After you have multiple controls selected, you can
modify the alignment and size by using the align and resize icons. These icons appear on the +%
at the top of the window.
C#
Copy Code Ê
u
! "
# $% &
'
This code takes the currently selected item from the control, a string that contains a Web
URL, and passes it to the Web browser's Navigate method. The & method loads and displays
the contents of the Web page at that location.
15.Ê Add event handlers for the MenuStrip options.
Return to the window and double-click each sub-item in the menu in turn. Visual C#
Express Edition creates event handler methods for each. Edit these methods so that they resemble the
following code.
C#
Copy Code Ê
u (% u)$
*+&
'
u , % u)$
*, &
'
u
% u)$
*
&
'
Each of these menu handlers calls a navigation method that is supported on the WebBrowser class.
You can see from this code that the default names given to the menu options can become very
confusing. For this reason, it's a good idea to change the name of each menu control as you create it by
using the editor. The name of the handler will then reflect the name of the menu option.
16.Ê See the code that Visual C# has written for you.
The Visual C# IDE has already written initialization code for you. In
view, find the constructor
for the , class. It has a signature of u, . Right-click the $-u
method that is being called from inside the constructor, and then click ' 0 . You now see
all the code that was being written behind the scenes as you were dragging and dropping controls and
setting properties in the window.
17.Ê Add some initialization code of your own.
The last task is to add some initialization code of your own to , . The constructor should never be
used to call any code that might throw an exception. Therefore, any such code must be located
someplace else, and that location is the ,
. method. Click the ",23 tab at the
top of the code editor to go back to Windows Form. Select the form and in the window
click the !& button (the one with the lightning bolt) and then double-click +
. This will add an
event handler method and position your cursor in the method in
view.
When a user starts your program, Windows will notify your application's form by sending a Load
event. When the form receives that event, it will call the ,
. method. Methods that are called
in response to events are called event handlers. The system will call your event at the appropriate
time; your job is to put the code into the event handler that you want to execute when the event
occurs.
In
view, add two lines to the ,
. method as shown in the following code. This will
cause the WebBrowser control to display your computer's default home page and also set the initial
value of the ComboBox.
C#
Copy Code Ê
u ,
.
# $#/0&
*+&
'
18.Ê Build and run the program.
Ñress F5 to build and run the Web browser. The Windows Form is displayed on the screen, and it then
displays your computer's default home page. You can use the ComboBox control to select a Web site,
and click ' to navigate to it. The menu options enable you to return home, or move back and forth
through previously visited Web sites.
Ê