OOP in Windows Applications


In Chapter 2, you saw how to create a simple Windows application in C#. Windows applications are heavily dependent on OOP techniques, and in this section you take a look at this to illustrate some of the points made in this chapter. To do this, in the following Try It Out you work through a simple example.

Try It Out – Objects in Action

image from book
  1. Create a new Windows application in the directory C:\BegVCSharp\Chapter8 called Ch08Ex01.

  2. Add a new Button control using the Toolbox, and position it in the center of Form1, as shown in Figure 8-12.

    image from book
    Figure 8-12

  3. Double-click on the button to add code for a mouse click. Modify the code that appears as follows:

    private void button1_Click(object sender, System.EventArgs e) { ((Button)sender).Text = "Clicked!"; Button newButton = new Button(); newButton.Text = "New Button!"; newButton.Click += new EventHandler(newButton_Click); Controls.Add(newButton); } private void newButton_Click(object sender, System.EventArgs e) { ((Button)sender).Text = "Clicked!!"; } }

  4. Run the application. The form is shown in Figure 8-13.

    image from book
    Figure 8-13

  5. Click on the button marked button1. The display changes, as shown in Figure 8-14.

    image from book
    Figure 8-14

  6. Click on the button marked New Button!. The display changes, as shown in Figure 8-15.

    image from book
    Figure 8-15

How It Works

By adding just a few lines of code you've created a Windows application that does something, while at the same time illustrating some OOP techniques in C#. The phrase "Everything's an object" is even more true when it comes to Windows applications. From the form that runs to the controls on the form, you need to make use of OOP techniques all the time. Throughout this example's description, I've highlighted some of the concepts that you've looked at earlier in this chapter to show how everything fits together.

The first thing you did in your application was to add a new button to the Form1 form. This button is an object, called Button. Next, by double-clicking the button, you added an event handler to listen for the Click event that the Button object generates. This event handler is added into the code for the Form object that encapsulates your application, as a private method:

private void button1_Click(object sender, System.EventArgs e) { }

This code uses the C# keyword private as a qualifier. Don't worry too much about this for now; in the next chapter, you look at the C# code required for the OOP techniques you've seen in this chapter.

The first line of code you added changes the text on the button that is clicked. This makes use of polymorphism as seen earlier in this chapter. the Button object representing the button that you click is sent to the event handler as an object parameter, which you cast into a Button type (this is possible because the Button object inherits from System.Object, which is the .NET class that object is an alias for). You then change the Text property of the object to change the text displayed:

((Button)sender).Text = "Clicked!";

Next, you create a new Button object with the new keyword (note that namespaces are set up in this project to enable this simple syntax, otherwise you'd need to use the fully qualified name of this object, System.Windows.Forms.Button):

Button newButton = new Button(); newButton.Text = "New Button!"; 

Elsewhere in the code a new event handler is added, which you use to respond to the Click event generated by the new button:

private void newButton_Click(object sender, System.EventArgs e) {    ((Button)sender).Text = "Clicked!!"; }

You then register this event handler as a listener for the Click event, using some overloaded operator syntax. Along the way, you create a new EventHandler object using a nondefault constructor, with the name of the new event handler function:

newButton.Click += new EventHandler(newButton_Click);

Finally, you make use of the Controls property. This property is an object that is a collection of all the controls on your form, and you use its Add() method to add your new button to the form:

Controls.Add(newButton);

The Controls property illustrates that properties need not necessarily be simple types such as strings or integers but can be any kind of object.

This short example has used almost all the techniques introduced in this chapter. As you can see, OOP programming needn't be complicated — it just requires a different point of view to get right.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net