Uh-oh, it looks like your Internet Explorer is out of date.

For a better shopping experience, please upgrade now.

C# For Java Programmers

C# For Java Programmers

by Harold Cabrera, Jeremy Faircloth, Philip Chen

Java Programmers, Preprare for Microsoft's .NET initiative while enhancing your repertoire and marketability with C# for Java Progammers!

C# for Java Programmers will prepare readers for the .NET framework by building on what they already know about object-oriented languages and give them the means to maintain their flexibility and effectiveness in an


Java Programmers, Preprare for Microsoft's .NET initiative while enhancing your repertoire and marketability with C# for Java Progammers!

C# for Java Programmers will prepare readers for the .NET framework by building on what they already know about object-oriented languages and give them the means to maintain their flexibility and effectiveness in an un-certain marketplace. This book will compare and contrast the advantages and disadvantages of both Java and C# to allow programmers to make their own decisions regarding what each language is best used for.

Whatever your feelings are about Microsoft and its .NET initiative, there can be no denying that C# is here to stay. The C# language, a close cousin to Java, is a new object-oriented programming language (OOPL) designed to work within the .NET framework. It improves upon many of the vague or ill-defined areas of C++ that frequently lead programmers into trouble. C# is a strongly-typed, object-oriented language designed to give the optimum blend of simplicity, expressiveness, and performance.

  • Written specifically for Java programmers. C# for Java Programmers is not an introductory guide to C#, but builds on what Java programmers already know about object-oriented languages to give them an efficient means for making in-roads to the .NET framework.
  • Compare and Contrast. This book will compare and contrast many of the advantages and drawbacks of Java and C# to allow programmers to make informed, intelligent decisions based on the unique uses of each language.

Editorial Reviews

This guide builds on what Java programmers already know about object- oriented languages to help them master C# and the .NET framework. After an overview of the C# language, chapters cover language fundamentals, programming structures, object-oriented programing, multithreading, working with I/O streams, Web development, and the Java User Migration Path. Bagnall is a Sun Certified Java Programmer and developer. Annotation c. Book News, Inc., Portland, OR (booknews.com)

Product Details

Elsevier Science
Publication date:
Edition description:
Product dimensions:
7.40(w) x 9.10(h) x 1.50(d)

Read an Excerpt

Creating User Interfaces with Windows Forms Introduction

In this book so far, all the example codes you have seen are console-based applications. This might have been great in the good old days of DOS, but for today's programs you might like a richer user interface. In this chapter you will broaden your C# arsenal by learning how to implement a graphical user interface (GUI) using Windows Forms.

Java provided us with the Abstract Windowing Tool (AWT), and then later, with Swing for creating platform-independent GUIs. The .NET Framework followed Java's lead by providing a more unified programming model similar to Swing. However, unlike Swing it is not platform independent. A Windows Form is a tool for building Windows applications, and is built specifically for the Windows platform.

In this chapter we will look at some of the classes found in the System.Windows.Forms namespace. You will learn how to create Windows applications by looking at some of the basic controls and event handling. To wrap up the chapter you will learn about rapid application development using Visual Studio.NET.

Windows Form Classes

Direct, integrated support in Windows classes makes it very easy to create forms in the .NET environment. In Java, GUI forms are often developed using AWT/Swing. In the .NET environment, GUI forms are a part of the Windows.Forms class. Thus creating forms in the .NET environment is much more convenient than doing so in Java. In addition, just as What You See Is What You Get (WYSIWYG) Integrated Device Electronics (IDEs) (like SUN's Forte) make it easy to develop forms in Java, Visual Studio .NET makes it easy to develop forms in the .NET environment.

The basic Windowsprogramming model consists of three main parts: forms, controls, and events. Forms are windows that enclose the controls, controls are components such as buttons, and events are actions notifications such as a pressed key or mouse button down. The .NET environment provides the means for integrating these three parts to enable building of GUIs.

In this chapter we will create a working financial calculator that accepts user inputs for principal and calculates the amount of interests using a particular interest rate. This example will illustrate some of the major GUI concepts and make use of some Windows components.

Windows Form Class Hierarchy

The System.Windows.Forms namespace (also known as the WinForms namespace) provides the necessary classes for implementing forms and the various controls that can be placed onto these forms. Figure 12.1 shows some of the major classes within the Windows Form class hierarchy.

Figure 12.1 Windows Form Class Hierarchy

As you can see, this is very similar to Java's AWT/Swing hierarchy where similar objects are derived from java.lang.Object. The Form class in C# is similar to the Applet class in Java, Button class is likewise similar to Button/JButton, TextBox class is analogous to TextField class, and so on. The methods for adding controls to forms are also very similar between C# and Java, as we shall see in the following sections.


It is interesting to note that form itself is a special type of control known as a ContainerControl. ContainerControls can contain other controls (such as Buttons, TextBoxes, and Labels).

Creating a Windows Form Application

A form is a representation of any window in an application. To create a form, simply derive from the Form class (i.e., “extending” the Form class) within the Windows.Forms namespace. The Form class can be used to create different types of windows that can interact with the user: standard, tool, borderless, floating, or even modal dialog box (see Table 12.1). Table 12.1 Different Types of Windows

Window Function

Standard Normal application window (default)

Tool Window that drops down with a list of selectable tools

Borderless Window that does not have borders (cannot be resized)

Floating Window that always stays (floats) on top of other windows

Modal Window that always locks focus when it is active

In this chapter, we will create a financial calculator that makes use of the Form class. This financial calculator will have a GUI application window (i.e., a form) that takes a user's inputs for principal and returns an output for the computed interest based on a particular interest rate. The form window will look like Figure 12.2.

Figure 12.2 Financial Calculator

To create a form, three basic steps are involved. First, using the System.Windows.Forms namespace, we will derive the form from the Form class. Our declaration will look like this:

public class InterestCalculator : Form

Second, we need to set several of the Forms' properties. The Forms class has several properties that you can set to determine how it is going to look. Some of these properties include:

  • Text Title text of the form.
  • Size Size of the form.
  • DesktopLocation Initial location of the form.
  • ForeColor Foreground color of the form.

For our calculator we will set only two properties: the From.Text and the Form.Size. The declaration looks like this:

this.Text = "Interest Calculator"; // Sets title
this.Size = new Size(200,225); // Sets size

The only major difference between Java and C# is that C# uses properties, and Java uses accessor methods such as setText() and setSize(). Finally, to display the form you must provide a Main() method to get the ball rolling. To do this we have to call the Run() method from the Application class and supply it an object instance of form as an argument. The declaration looks like the following:
Application.Run(new InterestCalculator());
The Application class found in the System.Windows.Forms namespace provides static methods to manage an application (such as methods to start and stop an application, and to process Windows messages) and properties to get information about an application. This class is sealed and therefore it cannot be inherited.
The following listing illustrates how these three steps are performed for our Financial Calculator example:
using System;
using System.Windows.Forms;

public class InterestCalculator : Form
public InterestCalculator()
this.Text = "Interest Calculator"; // Sets title
this.Size = new Size(200,225); // Sets size

public static void Main(string[] args)
Application.Run(new InterestCalculator());
The form class has several member functions that enable the programmer to manipulate the form. Some of the more useful methods of a form are listed in Table 12.2.

Table 12.2 Form Member Methods

Method Function

FindForm() Retrieves the form that the control is on
Focus() Sets input focus to the control
Invalidate() Invalidates the control and causes a paint message to be sent
PointToClient() Computes the location of the specified screen point into client coordinates
PointToScreen() Computes the location of the specified client point into screen coordinates
ShowDialog() Shows the form as a modal dialog box
Dispose() Releases the resources used
Close() Closes the form (calls Dispose() automatically)

The next steps involve instantiating controls and adding the appropriate event handlers to the controls. The following section illustrates how these two steps are done.

Meet the Author

Harold Cabrera (Sun Certified Java Programmer) was the Technical Editor of the Sun Certified Programmer for Java 2 Study Guide 2nd Edition and is a Software Engineer and Co-Founder of IdleWorks Inc. Harold is the Lead Architect for the software development team at IdleWorks, which develops distributed processing solutions for large- and medium-sized businesses with supercomputing needs. Chris Peiris currently lectures on Distributed Object Technology and Software Component Technologies subjects at Monash University, Caulfield, Victoria, Australia. He has been designing and developing MS Web solutions since 1995. His expertise lies in developing scalable, high-performance Web solutions for financial institutions and media groups.Brian Bagnall (Sun Certified Java Programmer and Developer) is the author of the popular book Core LEGO MINDSTORMS Programming and co-author of the Sun Certified Programmer for Java 2 Study Guide. Brian has worked for IBM and other leading computer companies. He is a key programmer of leJOS, a Java SDK for LEGO MINDSTORMS. Brian has bridged the world of LEGO MINDSTORMS and .NET by figuring out how to program the LEGO RCX brick or Cybermaster using C#.

Customer Reviews

Average Review:

Post to your social network


Most Helpful Customer Reviews

See all customer reviews