320 likes | 337 Views
Chapter Nine. Using GUI Objects and the Visual Studio IDE. Objectives. How to create a MessageBox How to add functionality to MessageBox buttons How to create a Form How to create a Form that is a program’s main window. Objectives. How to place a Button on a window
E N D
Chapter Nine Using GUI Objects and the Visual Studio IDE
Objectives • How to create a MessageBox • How to add functionality to MessageBox buttons • How to create a Form • How to create a Form that is a program’s main window
Objectives • How to place a Button on a window • How to use the Visual Studio IDE to design a Form • Learn about the code created by the IDE • How to add functionality to a Button on a Form • How to use the Visual Studio Help Search function
Creating a MessageBox • A MessageBox is a GUI object that can contain text, buttons, and symbols that inform and instruct a user • You must use the static class method Show() to display a message box, because its constructor is not public
Creating a MessageBox • Output of MessageBox1 program
Creating a MessageBox • There are many overloaded versions of the Show() method
Creating a MessageBox • Arguments used with the MessageBox.Show() method
Adding Functionality to MessageBox Buttons • DialogResult is an enumeration, or list of values that correspond to a user’s potential MessageBox button selection
Adding Functionality to MessageBox Buttons • Output of HamburgerAddition program
Creating a Form • Forms provides an interface for collecting, displaying, and delivering information to a user through buttons, list of options, text fields, and other controls • Unlike the MessageBox class, you can create an instance of the Form class
Creating a Form • Output of CreateForm1
Creating a Form • You can change the appearance, size, color, and window management features of a Form by setting its instance fields or properties • The Form class contains approximately 100 properties that can be used to set various configurations of the Form class • The ShowDialog() method displays a form as a modal dialog box
Creating a Form • CreateForm2 class and Output
Creating a Form that is a Program’s Main Window • When you create a new main window, you must complete two steps: • You must derive a new custom class from the base class System.Windows.Forms.Form • You must write a Main() method that calls the Application.Run() method, and you must pass an instance of your newly created Form class as an argument
Creating a Form that is a Program’s Main Window • Window1 class and the Window1 object
Creating a Form that is a Program’s Main Window • When you want to add property settings to a program’s main window, you can do so within the class constructor
Creating a Form that is a Program’s Main Window • The Window2 object
Placing a Button on a Window • A Button is a GUI object you can click to cause some action • For a Button to be clickable, you need to use the System.Windows.Forms.Control class
Placing a Button on a Window • Output of WindowWithButton program
Using the Visual Studio IDE to Design a Form • Using a text editor when programming and designing GUIs is a very tedious and error prone task • Just determining an attractive and useful layout in which to position all components on your Form takes many lines of code and a lot of trial and error • The Visual Studio IDE provides a wealth of tools to help make the Form design process easier
Understanding the Code Created by the IDE • The automatically generated code is simply a collection of C# statements and method calls similar to the ones created by hand • When you use the Designer in the IDE to design your forms, you save a lot of typing, which reduces the errors you create • Because the IDE generates so much code automatically, it is often more difficult to find and correct errors in programs created using the IDE than in programs you create by hand
Understanding the Code Created by the IDE • The code automatically generated by the IDE includes: • using statements and namespace creation • Declarations of forms and other objects • Comments • Methods and method calls
Adding Functionality to a Button on a Form • In most cases, it is easier to design a Form using the IDE than it is to write by hand the code a Form requires • Adding functionality to a Button is particularly easy using the IDE
Adding Functionality to a Button on a Form • Selecting the source file • MessageBox that appears after clicking Press
Adding a Second Button to a Form • Forms often contain multiple Button objects; a Form can contain as many Buttons as you need • Each Button has a unique identifier which allows you to provide unique methods that execute when a user clicks each Button
Adding a Second Button to a Form • Two Buttons on a Form
Adding a Second Button to a Form • Make a Choice Form after user clicks Sausage Button
Using the Visual Studio Help Search Function • The ultimate authority on the classes available in C# is the Visual Studio Search facility
Using the Visual Studio Help Search Function • Button Class documentation
Chapter Summary • A MessageBox is a GUI object that can contain text, buttons, and symbols that inform and instruct a user. You use the static class method Show() to display a MessageBox. • DialogResult is an enumeration, or list of values that correspond to a user’s potential MessageBox button selections • Forms provide an interface for collecting, displaying, and delivering user information
Chapter Summary • You can create a child class from Form that becomes the main window of an application. When you create a new main window, you must derive a new custom class from the base class System.Windows.Forms.Form, you must also write a Main() method that calls the Application.Run() method. • A window is more flexible than a MessageBox because you can place manipulatable Window components where you like on the surface of the window • The Visual Studio IDE provides a wealth of tools to help make the Form design process easier
Chapter Summary • Using the Visual Studio IDE, it is easy to create elaborate forms with a few keystrokes • In most cases, it is easier to design a Form using the IDE than it is to write by hand all the code a Form requires • Forms often contain multiple Button objects; a Form can contain as many Buttons as you need