Create A C# Windows Forms Application
Create A C# Windows Forms Application
8/12/08 10:00 AM
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 1 of 7
8/12/08 10:00 AM
The Windows Form you see in Designer view is a visual representation of the window that will open when your application is opened. In Designer view, you can drag various controls from the Toolbox onto the form. These controls are not really "live"; they are just images that are convenient to move around on the form into a precise location. After you have dropped a control onto the form, Visual C# works behind the scenes to create the code that will cause the real control to be positioned correctly when the program is run. This source code is in a file that is generally nested out of view. You can see this file in Solution Explorer, which is named Form1.designer.cs, if you expand Form1.cs. 5. If you are in Code view, switch to Designer view by right-clicking the code window and then clicking View Designer. Now change the size of the Windows Form. a. Click the lower-right corner of the Windows Form. b. When the pointer becomes a double-headed arrow, drag the corner of the form until it is at least as wide and as deep as a quarter of your screen. Because this is the window in which Web pages will be displayed, you don't want it to be too cramped. 6. Make sure the Properties window is displayed. Its default location is the lower-right section of the IDE, but you can move to another location if you like. The following illustration shows it in the upper-right corner. If you do not see the Properties window, on the View menu, click Properties window. This window lists the properties of the currently selected Windows Form or control, and it's here that you can change the existing values. 7. Change the title of the Windows Form. a. Click on the form to select it. b. In the Properties window, scroll down to Text, select the text "Form1," and type Web Browser. c. Press ENTER or TAB to move focus from the "Text" text box. You now see that the text at the top of your Windows Form (in the area called the title bar) has changed.
To quickly change the name of a control, right-click the control and click Properties. You can type the new name for the control in the Name property. 8. Click the Toolbox button on the toolbar, or on the View menu, click Toolbox. Scroll down the list of controls and expand Menus & Toolbars until you see MenuStrip [ http://msdn.microsoft.com/enus/library/system.windows.forms.menustrip(printer).aspx ] . Drag this control to anywhere on the Windows Form.
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 2 of 7
8/12/08 10:00 AM
This control creates a default menu at the top of the form. 9. In the box that says Type Here, type the name of the menu, in this case Navigate. When you press ENTER, new empty boxes appear to create other menus, and menu items. In the lower box, type Home. Press ENTER and more boxes are displayed. Type Go Back. Press ENTER, and type Go Forward.
These menu items form your basic Web site navigation controls. 10. Add a button. From the Toolbox, in the Common Controls category, drag a Button [ http://msdn.microsoft.com/en-us/library/system.windows.forms.button(printer).aspx ] control to approximately the middle of the Windows Form, just under the menu bar. In the Properties window, change the Text property to Go instead of button1, and change the design name, which resembles (Name), from button1 to goButton. 11. Add a ComboBox. From the Toolbox, in the Common Controls category, drag a ComboBox [ http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(printer).aspx ] control and position it to the left of the new button. Drag the edges and corners to resize and reposition the ComboBox until it is lined up with the button. Note: 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 se lection 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 Layout Toolbar at the top of the Design window.
12. Populate the ComboBox [ http://msdn.microsoft.com/enus/library/system.windows.forms.combobox(printer).aspx ] . A ComboBox [ http://msdn.microsoft.com/enus/library/system.windows.forms.combobox(printer).aspx ] provides a drop-down list of options from
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 3 of 7
8/12/08 10:00 AM
13. Add the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control. From the Toolbox, in the Common Controls category, scroll down until you locate the WebBrowser [ http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(printer).aspx ] control. Drag the control to the Windows Form. Resize the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control to fit inside the Windows Form without obscuring the ComboBox [ http://msdn.microsoft.com/enus/library/system.windows.forms.combobox(printer).aspx ] and Button [ http://msdn.microsoft.com/en-us/library/system.windows.forms.button(printer).aspx ] controls. If the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control doesn't resize easily, first set it to the desired size, open its properties, locate the Dock [ http://msdn.microsoft.com/enus/library/system.windows.forms.control.dock(printer).aspx ] setting, and make sure that it is set to none. Setting the Anchor [ http://msdn.microsoft.com/enus/library/system.windows.forms.control.anchor(printer).aspx ] settings to Top, Bottom, Left, Right will cause the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control to resize correctly when you resize the application window. The WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control is the control that does all the hard work of rendering the Web pages. You access it in your application through an instance of the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] class. View form1.Designer.cs and you will see that an instance of this class has been added to your application code, together with instances of classes that represent the other items that you added by using the designer. It is these instances that you will be using when you add event handlers for, and call methods on, the controls. 14. Add an event handler for the Button [ http://msdn.microsoft.com/enus/library/system.windows.forms.button(printer).aspx ] control. You have now finished the design stage of your application and are at the point when you can start adding some code to provide the program's functionality. The program must have event handlers for the button and for each menu option. An event handler is a method that is executed when the user interacts with the control. Visual C# Express Edition creates empty event handlers for you automatically. Double-click the button, and you'll see the Code Editor for your project appear. You'll also see that the event handler for the click event, which is the event message that occurs when the user clicks a button, has been created for you. Add code to the event handler method so that it resembles the following code. C# private void goButton_Click(object sender, System.EventArgs e) { webBrowser1.Navigate(new Uri(comboBox1.SelectedItem.ToString())); }
This code takes the currently selected item from the ComboBox control, a string that contains a Web URL, and passes it to the Web browser's Navigate [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser.navigate(printer).aspx ] method. The Navigate method loads and displays the contents of the Web page at that location. 15. Add event handlers for the MenuStrip [ http://msdn.microsoft.com/enus/library/system.windows.forms.menustrip(printer).aspx ] options. Return to the Designer 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# private void homeToolStripMenuItem_Click(object sender, System.EventArgs e) {
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 4 of 7
8/12/08 10:00 AM
Each of these menu handlers calls a navigation method that is supported on the WebBrowser [ http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser(printer).aspx ] class. Note: 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 c ontrol as you create it by using the Properties 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 Code view, find the constructor for the Form1 class. It has a signature of public Form1(). Right-click the InitializeComponent method that is being called from inside the constructor, and then click Go To Definition. 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 Properties window. 17. Add some initialization code of your own. The last task is to add some initialization code of your own to Form1. 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 Form1_Load method. Click the Form1.cs[Design] tab at the top of the code editor to go back to Windows Form. Select the form and in the Properties window click the Events button (the one with the lightning bolt) and then double-click Load. This will add an event handler method and position your cursor in the method in Code view. When a user starts your program, Windows will notify your application's form by sending a Load [ http://msdn.microsoft.com/en-us/library/system.windows.forms.form.load(printer).aspx ] event. When the form receives that event, it will call the Form1_Load 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 Code view, add two lines to the Form1_Load method as shown in the following code. This will cause the WebBrowser [ http://msdn.microsoft.com/enus/library/system.windows.forms.webbrowser(printer).aspx ] control to display your computer's default home page and also set the initial value of the ComboBox [ http://msdn.microsoft.com/enus/library/system.windows.forms.combobox(printer).aspx ] . C# private void Form1_Load(object sender, EventArgs e) { comboBox1.SelectedIndex = 0; webBrowser1.GoHome(); }
18. Build and run the program. Press 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 [ http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox(printer).aspx ] control to select a Web site, and click Go to navigate to it. The menu options enable you to return home, or move back and forth through previously visited Web sites.
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 5 of 7
8/12/08 10:00 AM
If you are new to C# programming, this would be a good time to read the C# Language Primer [ http://msdn.microsoft.com/en-us/library/zkxk2fwf(printer).aspx ] section. If you want to know more about the Visual C# Express Edition development environment and how to create console applications by using IntelliSense in particular, see How to: Create a C# Console Application [ http://msdn.microsoft.com/en-us/library/0wc2kk78(printer).aspx ] . . To learn about how to build Windows Presentation Foundation applications, see How to: Create a C# WPF Application [ http://msdn.microsoft.com/en-us/library/bb655895(printer).aspx ] .
See Also
Tasks How to: Create a C# Console Application [ http://msdn.microsoft.com/en-us/library/0wc2kk78(printer).aspx ] How to: Create a C# WPF Application [ http://msdn.microsoft.com/en-us/library/bb655895(printer).aspx ] Concepts C# Language Primer [ http://msdn.microsoft.com/en-us/library/zkxk2fwf(printer).aspx ] Other Resources Creating Your First Visual C# Application [ http://msdn.microsoft.com/en-us/library/bb383877(printer).aspx ] Visual C# Express Tips and Tricks [ http://msdn.microsoft.com/en-us/library/3zabbf9y(printer).aspx ]
Tags:
Community Content
This did not work for me
Last Edit 6:12 PM by Blaney
I kept getting this error message: "Invalid URI: The format of the URI could not be determined." in relation to this code: "webBrowser1.Navigate(newUri(comboBox1.SelectedItem.ToString()));" I tried this 2 seperate times from scratch and got the same error messege both times.
Tags: "Invalid URI: The format of the URI could not be determined." Tags: Navigate bar
Last Edit 2:49 PM by c_l Last Edit 10:18 AM by JP7
You need to add "http://" in front of your URL listed in the comboBox.
At runtime, when i click on navigate link, takes me straight back to home page rather than waiting for me to select the required option (go back, forward, home)???
You must have double clicked on the navigate menu strip root button. if you look at your code you should in that case see something like that:
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 6 of 7
8/12/08 10:00 AM
Tags:
http://msdn.microsoft.com/en-us/library/360kwx3z(printer).aspx
Page 7 of 7