You are on page 1of 32

Chapter Nine

Using GUI Objects and


the Visual Studio IDE

1
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

2
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

3
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

4
Creating a MessageBox

• Output of MessageBox1 program


5
Creating a MessageBox

• There are many overloaded versions of the Show()


method

6
Creating a MessageBox

• Arguments used with the MessageBox.Show() method

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

8
Adding Functionality to MessageBox
Buttons

• Output of HamburgerAddition program


9
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

10
Creating a Form

• Output of CreateForm1
11
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

12
Creating a Form

• CreateForm2 class and Output


13
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

14
Creating a Form that is a Program’s Main
Window

• Window1 class and the Window1 object

15
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

16
Creating a Form that is a Program’s Main
Window

• The Window2 object

17
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

18
Placing a Button on a Window

• Output of WindowWithButton program

19
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

20
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

21
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

22
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

23
Adding Functionality to a Button on a Form

• Selecting the source file


• MessageBox that appears after clicking Press
24
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

25
Adding a Second Button to a Form

• Two Buttons on a Form


26
Adding a Second Button to a Form

• Make a Choice Form after user clicks Sausage Button


27
Using the Visual Studio Help Search
Function

• The ultimate authority on the classes available in


C# is the Visual Studio Search facility

28
Using the Visual Studio Help Search
Function

• Button Class documentation


29
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

30
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

31
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

32

You might also like