1 chapter nine using gui objects and the visual studio ide

32
1 Chapter Nine Using GUI Objects and the Visual Studio IDE

Upload: dominic-booth

Post on 04-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

1

Chapter Nine

Using GUI Objects and the Visual Studio IDE

Page 2: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

2

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

Page 3: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

3

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

Page 4: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

4

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

Page 5: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

5

Creating a MessageBox

• Output of MessageBox1 program

Page 6: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

6

Creating a MessageBox

• There are many overloaded versions of the Show() method

Page 7: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

7

Creating a MessageBox

• Arguments used with the MessageBox.Show() method

Page 8: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

8

Adding Functionality to MessageBox Buttons

• DialogResult is an enumeration, or list of values that correspond to a user’s potential MessageBox button selection

Page 9: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

9

• Output of HamburgerAddition program

Adding Functionality to MessageBox Buttons

Page 10: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

10

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

Page 11: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

11

Creating a Form

• Output of CreateForm1

Page 12: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

12

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

Page 13: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

13

Creating a Form

• CreateForm2 class and Output

Page 14: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

14

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

Page 15: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

15

Creating a Form that is a Program’s Main Window

• Window1 class and the Window1 object

Page 16: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

16

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

Page 17: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

17

Creating a Form that is a Program’s Main Window

• The Window2 object

Page 18: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

18

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

Page 19: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

19

Placing a Button on a Window

• Output of WindowWithButton program

Page 20: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

20

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

Page 21: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

21

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

Page 22: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

22

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

Page 23: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

23

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

Page 24: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

24

Adding Functionality to a Button on a Form

• Selecting the source file• MessageBox that appears after clicking Press

Page 25: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

25

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

Page 26: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

26

Adding a Second Button to a Form

• Two Buttons on a Form

Page 27: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

27

Adding a Second Button to a Form

• Make a Choice Form after user clicks Sausage Button

Page 28: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

28

Using the Visual Studio Help Search Function

• The ultimate authority on the classes available in C# is the Visual Studio Search facility

Page 29: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

29

Using the Visual Studio Help Search Function

• Button Class documentation

Page 30: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

30

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

Page 31: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

31

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

Page 32: 1 Chapter Nine Using GUI Objects and the Visual Studio IDE

32

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