changes in the Design View. Any code that you need to write yourself should be placed in the Form1.cs file. At this point you might well be wondering where the Main method is and how the form gets displayed when the application runs; remember that Main defines the point at which the program starts. In the Solution Explorer, you should notice another source file called Program.cs. If you double-click this file the following code appears in the Code and Text Editor window: namespace WinFormHello { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } } You can ignore most of this code. However, the key statement is: Application.Run(new Form1()); This statement creates the form and displays it, whereupon the form takes over. In the following exercise, you'll learn how to add code that runs when the OK button on the form is clicked. Write the code for the OK button 1. Click the Form1.cs[Design] tab above the Code and Text Editor window to display Form1 in the Design View. 2. Move the mouse pointer over the OK button on the form, and then double-click the button. The Form1.cs source file appears in the Code and Text Editor window. Visual Studio 2005 has added a method called ok_Click to the Form1 class. (It has also added a statement to the InitializeComponent method in the Form1.Designer.cs file to automatically call ok_Click when the OK button is clicked. It does this by using a delegate type; delegates are discussed in Chapter 16, “Delegates and Events.”) 3. Type the MessageBox statement shown below inside the ok_Click method. The complete method should look like this: 4. private void ok_Click(object sender, System.EventArgs e) 5. { 6. MessageBox.Show("Hello " + userName.Text); } Make sure you have typed this code exactly as shown, including the trailing semicolon. You're now ready to run your first Windows program. Run the Windows program 1. On the Debug menu, click Start Without Debugging. Visual Studio 2005 saves your work, compiles your program, and runs it. The Windows form appears: 2. Enter your name, and then click OK. A message box appears welcoming you by name. 3. Click OK in the message box. The message box closes. 4. In the Form1 window, click the Close button (the X in the upper-right corner of the form). The Form1 window closes. • If you want to continue to the next chapter Keep Visual Studio 2005 running, and turn to Chapter 2. • If you want to exit Visual Studio 2005 now On the File menu, click Exit. If you see a Save dialog box, click Yes to save your work. Declaring and Using Delegates A delegate is a pointer to a method. A delegate looks and behaves much like an ordinary method when it is called. However, when you call a delegate, the runtime actually executes the method the delegate refers to. You can dynamically change the method that a delegate references, so code that calls a delegate might actually run a different method each time it executes. The best way to understand delegates is to see them in action, so let's work through an example. NOTE If you are familiar with C++, a delegate is very similar to a function pointer. However, unlike function pointers, delegates are type-safe; you can only make a delegate refer to a method that matches the signature of the delegate, and you cannot call a delegate that does not refer to a valid method. 1. tree1.Insert(5); 2. tree1.Insert(11); 3. tree1.Insert(5); 4. tree1.Insert(-12); 5. tree1.Insert(15); 6. tree1.Insert(0); 7. tree1.Insert(14); 8. tree1.Insert(-8); 9. tree1.Insert(10); 10. tree1.Insert(8); 11. tree1.Insert(8); tree1.WalkTree(); These statements create a new binary tree for holding ints. The constructor creates an initial node containing the value 10. The Insert statements add nodes to the tree, and the WalkTree method prints out the contents of the tree, which should be sorted in ascending order. NOTE Remember that the int keyword in C# is actually just an alias for the System.Int32 type; whenever you declare an int variable, you are actually declaring a struct variable of type System.Int32. The System.Int32 type implements the IComparable and IComparable<T> interfaces, which is why you can create Tree<int> variables. Similarly, the string keyword is an alias for System.String, which also implements IComparable and IComparable<T>. 12. On the Build menu, click Build Solution. Verify that the solution compiles, correcting any errors if necessary. 13. On the Debug menu, click Start Without Debugging. When the program runs, the values should be displayed in the following sequence: –12, –8, 0, 5, 5, 8, 8, 10, 10, 11, 14, 15 Press the Enter key to return to Visual Studio 2005. 14. Add the following statements to the end of the Main method, after the existing code: 15. Tree<string> tree2 = new Tree<string>("Hello"); 16. tree2.Insert("World"); 17. tree2.Insert("How"); 18. tree2.Insert("Are"); 19. tree2.Insert("You"); 20. tree2.Insert("Today"); 21. tree2.Insert("I"); 22. tree2.Insert("Hope"); 23. tree2.Insert("You"); 24. tree2.Insert("Are"); 25. tree2.Insert("Feeling"); 26. tree2.Insert("Well"); tree2.WalkTree(); These statements create another binary tree for holding strings, populate it with some test data, and then print the tree. This time, the data will be sorted alphabetically. 27. On the Build menu, click Build Solution. Verify that the solution compiles, correcting any errors if necessary. 28. On the Debug menu, click Start Without Debugging. When the program runs, the integer values will be displayed as before followed by the strings in the following sequence: Are, Are, Feeling, Hello, Hope, How, I, Today, Well, World, You, You 29. Press the Enter key to return to Visual Studio 2005. . changes in the Design View. Any code that you need to write yourself should be placed in the Form1.cs file. At this point you might well be wondering where the Main method is and how. Write the code for the OK button 1. Click the Form1.cs [Design] tab above the Code and Text Editor window to display Form1 in the Design View. 2. Move the mouse pointer over the OK button on. static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main( ) { Application.EnableVisualStyles(); Application.Run(new