How to: Create a Visual J# Windows Application (Visual J# Express) 

The following steps familiarize you with elements of the Visual J# Express Edition development environment as you build a simple Visual J# program using Windows Forms. Windows Forms provides your project with the components that make up a standard Windows application user interface, including dialog boxes, menus, buttons, and many other controls.

This example shows you how to create your own Web browser, which you can customize with shortcuts to your favorite Web sites.

Note

Many of the features of the development environment discussed in this topic will also be encountered when developing console applications, so don't skip this part simply because you don't plan on writing Windows applications.

In this section, you'll learn:

  • How to create a new Windows application.

  • How to toggle between Code and Design views.

  • How to change the Windows Form's properties.

  • How to add a menu control.

  • How to create and populate a ComboBox control.

  • How to use a Web Browser control.

  • How to create handlers for controls.

To create a J# Windows application

  1. On the File menu, point to New, and then click Project.

    The New Project dialog box appears. This dialog box lists the different default project types that you can use Visual J# Express Edition to create.

  2. Select Windows Application as your project type.

  3. Type Web Browser in the Name box to change the name of your application.

  4. Click OK.

    Visual J# Express Edition creates your project, named after the project title, and then opens the Visual J# Express Edition Windows Forms Designer. The default view is of your new Windows Form, Form1. You can change from this view to the source code view at any time by pressing F7 or by selecting Code from the View menu, and you can return to the Designer view by pressing SHIFT+F7.

    VJS Express Form

    Form1 is the form that is displayed when your application is launched. You can add various controls to this form, accept input from the user, display text and graphics, and do everything that a Windows application can do. Behind the scenes, the code that is required to manage the window has been created by Visual J# Express Edition. J# keeps all of the code in the main file for the form because J# does not support partial types.

  5. Change the size of the Windows Form.

    If you aren't already in the Designer view, press SHIFT+F7. Click on the lower-right corner of the Windows Form, and when the cursor becomes a double-headed arrow, click and pull the corner of the form until it's approximately as wide and as deep as a quarter of your screen. This is the window in which your Web sites will be displayed, so you don't want it be too cramped.

  6. Change the title of the Windows Form.

    If the Properties window isn't already open, select Properties Window from the View menu. This window lists the properties of the currently selected Windows Form or control, and it's here you can make alterations to existing values.

    Find the property Text. In the column to the right, select Form1, and type Web Browser. Press ENTER or the TAB key to move focus from the control, and you'll see that the name on your Windows Form has changed. Notice that there is a similar field called (Name). This is the name of the class that contains the Windows Form. You can change it too, but if you do, remember that the rest of the code in this task refers to the old name of Form1.

    VJS Express Properties

  7. Add a Menu control.

    Open the Toolbox by selecting Toolbox from the View menu. Scroll down the list of controls until you get to MenuStrip. Drag this control to anywhere on the Windows Form.

    VJS Express Menu Strip

    After you have released the MenuStrip control, you'll see that it creates a default menu at the top of the form.

  8. Populate the Menu.

    In the box that says Type Here, type Navigate to give the menu a name. When you press ENTER, new empty boxes appear in which you can 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.

    Navigation via MenuStrip Control

    In order to make your source code more legible, you should rename the default names given to the menu items. If you open the Properties window and click on each menu item in turn, you will see they have Design Names such as menuItem1, menuItem2, and so on.

    Rename the menu items so that the Home item is called menuItem_Home, Go Back is called menuItem_GoBack, and Go Forward is called menuItem_GoForward.

  9. Add a button.

    From the Toolbox, drag a button control to approximately the middle of the Windows Form, just below the menu bar. View the button in the Properties window, change the Text property to Go instead of button1, and change the design name from button1 to button_go.

  10. Add a ComboBox.

    Drag a ComboBox control from the Toolbox and position it to the left of the new button. Click and drag the edges and corners to resize and reposition the ComboBox until it is lined up with the button as shown in the following screenshot.

    Note

    When you are moving controls around a Windows Form, you will see blue lines displayed. 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 dragging a selection box around the controls, or holding down SHIFT as you click on them. Then use the appropriate align and resize buttons, located on the Layout Toolbar, to situate the controls.

  11. Populate the ComboBox.

    A ComboBox provides a drop-down list of options from which the user can choose. In this program, the ComboBox is going to contain a list of your favorite Web sites for quick access.

    To create the list of sites, select the ComboBox, and view its properties. Click in the column next to the Items property, and you'll see an ellipses button (…). Click this button to open the String Collection Editor dialog box, in which you can add contents to the ComboBox. Add as many Web site URLs as you want, pressing RETURN after each.

  12. Add the WebBrowser control.

    Open the Toolbox, and scroll down until you locate the WebBrowser control. Drag the control to the Windows Form. Resize the WebBrowser control to fit nicely inside the Windows Form without obscuring the ComboBox and Button controls. If the WebBrowser control doesn't resize easily, open its properties, locate the Dock setting, and make sure that it is set to none. Setting the Anchor settings to Top, Bottom, Left, Right will cause the WebBrowser control to resize properly when you resize the application window.

    The WebBrowser control is the control that does all the hard work of rendering the Web pages.

  13. Add a handler for the Button control.

    You have now finished the design stage of your application, and are at the point when you need to start adding some J# code to provide the program's functionality.

    You need to add handlers for the button and for each of the menu options. A handler is a method that is executed when a particular control is activated. Visual J# Express Edition creates empty handlers for you automatically.

    Double-click the button, and the Code editor for your project is displayed. You'll also see that the handler for the click event—the event message that occurs when the user clicks on a button—has been created for you. Add code to the handler method so it looks like the following code:

    private void button_go_Click(Object sender, System.EventArgs e)
        {
          webBrowser1.Navigate(comboBox1.get_SelectedItem().toString());
        }
    

    This code takes the currently selected item from the ComboBox control, a string containing a Web URL, and passes it to the Web Browser's navigation method. The navigation method loads and displays the contents of the Web page at that location.

  14. Add handlers for the menu options.

    Return to the Designer view by pressing F7, and double-click on each of the menu items in turn. Visual J# Express Edition creates handler methods for each. Edit these methods so they look like the following code:

    private void menuItem_Home_Click(Object sender, System.EventArgs e)
        {
            // Go to Home Page menu option.
            webBrowser1.GoHome();
        }
    private void menuItem_GoForward_Click(Object sender, System.EventArgs e)
        {
            // Go forward menu option.
            webBrowser1.GoForward();
        }
    private void menuItem_GoBack_Click(Object sender, System.EventArgs e)
        {
            // Go back menu option.
            webBrowser1.GoBack();
        }
    

    Each of these menu handlers calls a navigation method that is part of the Web Browser class.

    Note

    You can see from this code that the default names given to the menu options can become quite confusing. For this reason, it's a good idea to change the name of each menu control as you create it, using the Properties window. The name of the handler will then reflect the name of the menu option.

  15. Add some initialization code.

    The last task is to add some more code to the Form1 method. This method is the constructor for the Form1 object, and is called when the Windows Form is being created. It's therefore the ideal location to place any code that is required to change the controls' initial values or other settings.

    The following code will cause the Web Browser control to display your computer's default home page, and also set the initial value of the ComboBox. Change the method to contain the following code:

    public Form1()
        {
            InitializeComponent();
            comboBox1.set_SelectedIndex(0);
            webBrowser1.GoHome();
        }
    }
    
  16. Build and run the program.

    Press F5 to build and run your Web browser. Your Windows Form should be displayed on the screen, and then should display your computer's default home page. You can use the ComboBox 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.

    VJS Express Window

    If you are new to Visual J# programming, this would be a good time to read the Visual J# Reference section. If you want to know more about the Visual J# Express Edition development environment, and how to create console applications using IntelliSense in particular, read the previous section How to: Create a Visual J# Console Application (Visual J# Express).

See Also

Other Resources

Using Visual J# Express
Visual J# Express Development Environment Features
Visual J# Reference