Isbn 9780134382609 and free and pdf and download






















View larger. Preview this title online. Request a copy. Download instructor resources. Additional order info. K educators : This link is for individuals purchasing with credit cards or PayPal only. Clear, friendly, and approachable, this Fourth Edition of Starting Out With Visual C is an ideal beginning text for students with no programming experience. Detailed walk-throughs and a readable, comprehensible style make the text inviting to new programmers, while numerous practical example programs highlight the most important programming topics.

Topics are examined progressively in each chapter, with objects taught before classes. The Fourth Edition has been completely updated for Visual Studio and contains new sections on debugging, accessing controls on different forms, and auto-properties.

Important: To use the test banks below, you must download the TestGen software from the TestGen website. If you need help getting started, read the tutorials on the TestGen site. Pearson offers affordable and accessible purchase options to meet the needs of your students.

Connect with us to learn more. Tony Gaddis is the principal author of the Starting Out With series of textbooks. He is a highly acclaimed instructor with two decades of experience teaching computer science courses, primarily at Haywood Community College.

We're sorry! We don't recognize your username or password. Please try again. The work is protected by local and international copyright laws and is provided solely for the use of instructors in teaching their courses and assessing student learning.

You have successfully signed out and will be required to sign back in should you need to download more resources. Starting out with Visual C , 4th Edition. Tony Gaddis, Haywood Community College. Description For courses in Introductory C Programming. The new edition has been completely updated , revised, and tested for Visual Studio Coverage of debugging includes a new section on using the Visual Studio debugger to locate logic errors.

A new section has also been added to Chapter 6, demonstrating how to use the debugger to step into, step over, and step out of methods. Some objects have a visual part that can be seen on the screen. For example, Figure shows the wage-calculator program that we discussed in the previous section. Figure shows a Form object that contains several other graphical objects.

The form shown in Figure contains two Label objects. The form shown in Figure has two TextBox objects: one in which the user enters the number of hours worked and another in which the user enters the hourly pay rate. When the user clicks a Button object with the mouse, an action takes place. The form in Figure has two Button objects. One shows the caption Calculate Gross Pay. When the user clicks this button, the program calculates and displays the gross pay. The other button shows the caption Exit.

When the user clicks this button, the program ends. As you study this book, you will create applications that incorporate many different types of objects. We could say that the form shown in Figure contains two Label controls, two TextBox controls, and two Button controls.

Not all objects can be seen on the screen, however. Some objects exist only in memory for the purpose of helping your program perform some task. For example, there are objects that read data from files, objects that generate random numbers, objects that store and sort large collections of data, and so forth. These types of objects help your program perform tasks, but they do not directly display anything on the screen. When you are writing a program, you will use objects that can help your program perform its tasks.

Before a specific type of object can be used, that object has to be created in memory. And, before an object can be created in memory, you must have a class for the object. A class is code that describes a particular type of object. NET Framework C is a very popular programming language, but there are a lot of things it cannot do by itself.

For example, you cannot use C alone to create a graphical user interface, read data from files, work with databases, or many of the other things that programs commonly need to do. C provides only the basic keywords and operators that you need to construct a program.

The answer is the. NET Framework is a collection of classes and other code that can be used, along with a programming language such as C , to create programs for the Windows operating system. For example, the. As you work through this book you will not only learn C , but you will also learn about many of the classes and other features provided by the. Writing Your Own Classes The.

NET Framework provides many prewritten classes ready for use in your programs. There will be times, however, that you will wish you had an object to perform a specific task, and no such class will exist in the.

This is not a problem because in C you can write your own classes that have the specific fields, properties, and methods that you need for any situation. In Chapter 9, you will learn to create classes for the specific objects that you need in your programs. NET Framework? The Program Development Cycle Previously in this chapter, you learned that programmers typically use high-level languages such as C to create programs. There is much more to creating a program than writing code, however.

The process of creating a program that works correctly typically requires the six phases shown in Figure The entire process is known as the program development cycle.

Most programs perform the following threestep process: Step 1. Input is received. Step 2. Some process is performed on the input.

Step 3. Output is produced. Input is any data that the program receives while it is running. The results of the process are then sent out of the program as output. If you can identify these three elements of a program input, process, and output , then you are on your way to understanding what the program is supposed to do. For example, suppose you have been asked to write a program to calculate and display the gross pay for an hourly paid employee.

Often, you will find it helpful to draw a sketch of each form that the program displays. Notice that the sketch identifies each type of control GUI object that will appear on the form. The TextBox controls will allow the user to enter input. When the user clicks the Button control that reads Exit, the program will end.

As a Visual C programmer, you have a powerful environment known as Visual Studio at your disposal. Here are the steps that the program should take to perform that task: Step 1. Get the number of hours worked from the appropriate TextBox. Get the hourly pay rate from the appropriate TextBox. Calculate the gross pay as the number of hours worked times the hourly pay rate. Step 4. Display the gross pay in a pop-up window. This is an example of an algorithm, which is a set of well-defined, logical steps that must be taken to perform a task.

An algorithm that is written out in this manner, in plain English statements, is called pseudocode. The word pseudo means fake, so pseudocode is fake code. The process of informally writing out the steps of an algorithm in pseudocode before attempting to write any actual code is very helpful when you are designing a program.

Because you do not have to worry about breaking any syntax rules, you can focus on the logical steps that the program must perform. Flowcharting is another tool that programmers use to design programs. A flowchart is a diagram that graphically depicts the steps of an algorithm. They represent steps in which the program reads input or displays output.

They represent steps in which the program performs some process on data, such as a mathematical calculation. To step through the symbols in the proper order, you begin at the Start terminal and follow the arrows until you reach the End terminal. During this process, you will refer to the pseudocode or flowcharts that you created in Step 3 and use Visual Studio to write C code.

Correct Syntax Errors You previously learned in this chapter that a programming language such as C has rules, known as syntax, that must be followed when writing a program. A syntax error occurs if the programmer violates any of these rules. If the program contains a syntax error or even a simple mistake such as a misspelled keyword, the program cannot be compiled or executed.

Virtually all code contains syntax errors when it is first written, so the programmer will typically spend some time correcting these. Once all the syntax errors and simple typing mistakes have been corrected, the program can be compiled and translated into an executable program.

A logic error is a mistake that does not prevent the program from running but causes it to produce incorrect results. Mathematical mistakes are common causes of logic errors. If the program produces incorrect results, the programmer must debug the code. This means that the programmer finds and corrects logic errors in the program.

In this event, the program development cycle starts over and continues until no errors can be found. To follow the tutorials in this book, and create Visual C applications, you will need to install Visual Studio on your computer. Visual Studio is a professional integrated development environment IDE , which means that it provides all the necessary tools for creating, testing, and debugging software. If you do not have access to Visual Studio , you can install Visual Studio Community Edition, a free programming environment that is available for download from Microsoft at www.

Visual Studio is a customizable environment. If this is the case, the screens that you see may not match exactly the ones shown in this book. Tutorial guides you through the process. Scroll down in the list. When you see Visual Studio appear, click it. As you type, the search results will appear on the right edge of the screen. Open the Visual Studio program group, and click Visual Studio Step 2: Figure shows the Visual Studio environment.

The screen shown in the fig- ure is known as the Start Page. By default, the Start Page is displayed when you start Visual Studio, but you may not see it because it can be disabled.

Notice the check box in the bottom left corner of the Start Page that reads Show page on startup. If this box is not checked, the Start Page will not be displayed when you start Visual Studio. If you do not see the Start Page, you can always display it by clicking View on the menu bar at the top of the screen and then clicking Start Page. To make sure that Visual Studio looks and behaves as described in this book, you should make sure that Visual C is selected as the programming environment.

Perform the following: 1. Select Visual C and then click the Finish button. After a moment you should see a Reset Complete window.

Click the Close button and continue with the next step in the tutorial. Next you will see a dialog box asking Are you sure you want to restore the default window layout for the environment?

Click Yes. If you are continuing with the next tutorial, leave Visual Studio running. You can exit Visual Studio at any time by clicking File on the menu bar and then clicking Exit. When you are ready to create a new application, you start a new project. Tutorial leads you through the steps of starting a new Visual C project.

After doing this, the New Project window shown in Figure should be displayed. Then, select Windows Forms Application, as shown in Figure Step 4: At the bottom of the New Project window, you see a Name text box. This is where you enter the name of your project. The Name text box will be automatically filled in with a default name. Change the project name to My First Project, as shown in Figure If you wish to change the location, click the Browse button and select the desired location.

By default, the solution name is the same as the project name. For all the projects that you create in this book, you should keep the solution name the same as the project name.

Step 5. Click the Ok button to create the project. It might take a moment for the project to be created. Once it is, the Visual Studio environment should 49 50 Chapter 1 Introduction to Computers and Programming appear, similar to Figure Notice that the name of the project, My First Project, is displayed in the title bar at the top of the Visual Studio window.

Leave Visual Studio running and complete the next tutorial. In this tutorial you will save the My First Project application and then close it. Step 1: Visual Studio should still be running from the previous tutorial. Step 2: To close the project, click File on the menu bar and then click Close Solution. The Visual Studio Environment The Visual Studio environment consists of a number of windows that you will use on a regular basis. We discuss solutions in greater detail in a moment.

When you create a new C project, a new solution is automatically created to contain it. The Solution Explorer window allows you to navigate among the files in a Visual C project. Remember that Visual Studio is a customizable environment. On the View menu, click Solution Explorer. On the View menu, click Properties. When Auto Hide is turned on, the window is displayed only as a tab along one of the edges of the Visual Studio environment. Figure shows how the Solution Explorer and Properties windows appear when their Auto Hide feature is turned on.

Notice the tabs that read Solution Explorer and Properties along the right edge of the screen. Figure also shows a Team Explorer tab. We do not discuss the Team Explorer in this book. This is the bar at the top of the Visual Studio window that provides menus such as File, Edit, View, Project, and so forth.

As you progress through this book, you will become familiar with many of the menus. Below the menu bar is the standard toolbar. The standard toolbar contains buttons that execute frequently used commands. All commands that are displayed on the toolbar may also be executed from a menu, but the standard toolbar gives you quicker access to them. Figure identifies the standard toolbar buttons that you will use most often, and Table gives a brief description of each. You will use the Toolbox extensively as you develop Visual C applications.

The Toolbox typically appears on the left side of the Visual Studio environment. If the Toolbox is in Auto Hide mode, its tab will appear as shown in Figure Figure shows the Toolbox opened, with Auto Hide turned off. The Toolbox is divided into sections, and each section has a name. If you scroll the Toolbox, you will see many other sections.

Each section can be opened or closed. If you want to open a section of the Toolbox, you simply click on its name tab. To close the section, click on its name tab again. In Figure , the Common Controls section is open. You use the Common Controls section to access controls that you frequently need, such as Buttons, Labels, and TextBoxes. You can move any section to the top of the list by dragging its name with the mouse. Using ToolTips A ToolTip is a small rectangular box that pops up when you hover the mouse pointer over a button on the toolbar or in the Toolbox for a few seconds.

The ToolTip box 1. Figure shows the ToolTip that appears when the cursor is left sitting on the Save All button. Alternatively, the windows can be floating. When windows are floating, they behave as normal windows. You may move or resize them to suit your preference. If you accidentally close the Designer window, the Solution Explorer window, or the Properties window, you can use the View menu to redisplay them. Projects and Solutions As you learn to program in Visual C , you will see the terms project and solution used often.

These terms do not mean the same thing, but they are sometimes used interchangeably. Each Visual C application that you create is called a project. A solution is a container that holds one or more Visual C projects. If you are developing applications for a large organization, you might find it convenient to store several related projects together in the same solution.

Although it is possible for a solution to hold more than one project, each project that you will create in this book will be saved in its own solution.

Each time you create a new project, you will also create a new solution to hold it. Figure illustrates this concept.

Typically, the solution will be given the same name as the project. You specify this information at the bottom of the New Project window, as shown in Figure At this location, a solution folder named My First Project will be created. If we use Windows to look inside that folder, we will see the two items shown in Figure Notice that one of the items is another folder named My First Project.

That is the project folder, which contains various files related to the project. The other item is the solution file. In Windows, you can double-click the solution file to open the project in Visual Studio. Navigate to the desired solution folder, select the solution file, and click Open.

The Open Project window will appear. Navigate to the My First Project solution, select the solution file, and click the Open button. My First Project should be opened. You will also find code samples, tutorials, articles, and access to tutorial videos. Step 1: If Visual Studio is still running on your computer from the previous tutorial, continue to Step 2. When you are finished practicing, make sure Auto Hide is turned off for each of these windows.

Your screen should look like Figure Step 3: Practice floating and docking the Solution Explorer window, the Properties window, and the Toolbox. Close each of these windows by clicking its Close button. Step 5: Do you remember which buttons on the toolbar restore the Solution Explorer, Properties window, and Toolbox? If not, move your mouse cursor over any button on the toolbar and leave it there until the ToolTip appears. Repeat this procedure on different buttons until you find the ones whose ToolTips read Solution Explorer, Properties Window, and Toolbox.

Refer to Figure and Table for further assistance. Step 6: Click the appropriate buttons on the toolbar to restore the Solution Explorer, the Properties window, and the Toolbox. You may see a dialog box asking whether you wish to save changes to a number of items. RAM b. ENIACs b. CPUs b.

ASCII c. Unicode d. ENIAC ASCII d. Unicode 63 64 Chapter 1 Introduction to Computers and Programming Unicode C code b. English words d. Java Designer window Properties window Solution explorer window New project window 65 66 Chapter 1 Introduction to Computers and Programming GUI d. Main memory is also known as RAM.

Images, such as the ones you make with your digital camera, cannot be stored as binary numbers. Compiling a program is the same as executing a program.

A graphical user interface is also known as a console interface. A syntax error does not prevent a program from being compiled and executed. Word processing programs, spreadsheet programs, e-mail programs, Web browsers, and games are all examples of utility programs.

Short Answer 1. What is the need for RAM, when it is completely volatile? Which numbering system is used in computer systems, and how does it work? What is a byte? What do you understand by utility programs?

Which particular software would you need to start up a machine? Programming Problems 5. What is the largest value that can be stored in two bytes? Could the disk drive of your computer be called an output device? What would be the positional value of the 12th bit in a two-byte memory space?

What is pseudocode? What is a flowchart? How does the binary system represent real numbers? Can you use C alone to perform advanced operations such as creating GUIs, reading data from a file, or working with databases? Why or why not? What are the names of the four areas that are indicated in the figure? What is the purpose of the Toolbox in the Visual Studio environment?

How can you access the documentation for Visual Studio? What steps must you take to open an existing project? Suppose you need to write a program that calculates the area of any rectangular field and the cost of fencing the field: Answer the following: a. What items of input must the user enter? Once the input has been entered, how will the program determine the area and cost? What output will the program display?

In this chapter, you will create your first Visual C application. Before you start, however, you need to learn some fundamental concepts about creating a GUI in Visual Studio. This section shows the basics of editing forms and creating controls. Figure shows an example. When the application runs, the form will be displayed on the screen. When a bounding box appears around an object in the Designer, it indicates that the object is selected and is ready for editing. You can easily resize the form with the mouse.

When you position the mouse cursor over any edge or corner that has a sizing handle, the cursor changes to a two-headed arrow. Figure shows examples. When the mouse cursor becomes a two-headed arrow, you can click and drag the mouse to resize form. The blank form that Visual Studio initially creates in a new project is named Form1. If you do not see the Properties window, click View on the menu bar.

On the View menu, click Properties Window. The area at the top of the Properties window shows the name of the object that is currently selected. You can see in Figure that the name of the selected object is Form1. Below that is a scrollable list of properties. Its value is , For example, assume the Form1 form is currently selected. You can perform the following steps to change its Text property to My First Program.

You can perform the following steps to change its size to pixels wide by pixels high. Step 1: In the Properties window, locate the Size property. The form will be resized as shown in Figure This causes the properties to be displayed in alphabetical order. Alternatively, the Categorized button can be selected, which causes the properties to be displayed in groups. The alphabetical listing is the default selection, and most of the time, it makes it easier to locate specific properties.

Recall from Chapter 1 that the Toolbox usually appears on the left side of the Visual Studio environment. If the Toolbox is in Auto Hide mode, you can click its tab to open it. Figure shows an example of how the Toolbox typically appears when it is open. The Toolbox shows a scrollable list of controls that you can add to a form. To add a control to a form, you simply find it in the Toolbox and then double-click it. The control will be created on the form.

For example, suppose you want to create a Button control on the form. You find it in the Toolbox, as shown in Figure , double-click it, and a Button control will appear on the form. And a Button control will be created on the form. Resizing and Moving Controls Take a closer look at the Button control that is shown on the form in Figure Notice that it is enclosed in a bounding box with sizing handles.

This indicates that the control is currently selected. When a control is selected, you can use the mouse to resize it in the same way that you learned to resize a form earlier. You can also use the mouse to move a 2. Position the mouse cursor inside the control, and when the mouse cursor becomes a four-headed arrow , you can click and drag the control to a new location. Figure shows a form with a Button control that has been enlarged and moved. When you create Button controls, they are automatically given default names such as button1, button2, and so forth.

Button controls have a Text property, which holds the text that is displayed on the face of the button. As a result, when you create a Button control, its name will be displayed on the face of the button. For example, the form in Figure contains three Button controls named button1, button2, and button3.

The text that is displayed on a button should indicate what the button will do when it is clicked. For example, a button that calculates an average might have the text Calculate Average displayed on it, and a button that prints a report might have the text Print Report displayed on it. Step 2: In the Properties window, locate the Text property. The new text will be displayed on the button. A name such as calculateTaxButton would be much better.

Here are the steps: Step 1: Make sure the control is selected. If you do not see the bounding box and sizing handles around the control, just click the control to select it.

Step 2: In the Properties window, scroll up to the top of the list of properties. You should see the Name property, as shown in Figure The Name property is enclosed in parentheses to make it appear near the top of the alphabetical list of properties. This makes it easier to find. You have successfully changed the name of the control. Table lists some identifiers that might be used for Button control names and indicates whether each is a legal or illegal identifier in C. For example, consider the following Button control names: calculatetaxbutton printreportbutton displayanimationbutton Unfortunately, these names are not easily read by the human eye because the words are not separated.

Because we cannot have spaces in control names, we need to find another way to separate the words in a multiword control name to make it more readable to the human eye. Most C programmers address this problem by using the camelCase naming convention for controls. Checkpoint 2.

In this chapter you will create your first 2. When the finished application runs, it will display the form shown on the left in Figure Notice that the form contains a button that reads Display Message.

When you click the button, the window shown on the right in the figure will appear. The process of creating this application is divided into two parts.

Tutorial leads you through the process of creating the GUI. Step 2: Start a new project by clicking File on the menu bar and then selecting New Project. Step 3: The New Project window should appear. Then, select Windows Forms Application as the type of application. In the Name text box at the bottom of the window , change the name of the project to Hello World, and then click the Ok button.

Step 4: Make sure the Toolbox, the Solution Explorer, and the Properties window are visible and that Auto Hide is turned off for each of these windows. The Visual Studio environment should appear as shown in Figure The form should appear similar to that shown in Figure Just make it appear similar to Figure Step 7: Now you are ready to add a Button control to the form.

Locate the Button tool in the Toolbox and double-click it. A Button control should appear on the form, as shown in Figure Move the Button control so it appears approximately in the center of the form, as shown in Figure After doing this, notice that the text displayed on the button has changed, as shown in Figure The Button control that you created in this application p will cause a message to be displayed when it is clicked.

The name button1 does not convey that purpose, however. The Properties window should appear as shown in Figure This causes the application to be compiled and executed. Although the application is running, it is not capable of doing anything other than displaying the form. If you click the Display Message button, nothing will happen.

That is because you have not yet written the code that executes when the button is clicked. You will do that in the next tutorial. Much of the code that you will write in an application will be event handlers. Event handlers respond to specific events that take place while an application is running. An application is more than a user interface, however.

If you want your application to perform any meaningful actions, you have to write code. This section introduces you to Visual C code and shows how to program an application to respond to button clicks.

A file that contains program code is called a source code file. When you start a C Windows Forms Application project, Visual Studio automatically creates several source code files and adds them to the project. C source code files always end with the. The code in this file performs behind-the-scenes initialization tasks that are necessary to get the application up and running.

It is important that you do not modify the contents of this file because doing so could prevent the application from executing.

When you write code that defines some action related to Form1 such as responding to a button click , you will write the code in this file. The Form1. You can think of this auto-generated code as an outline to which you can add your own code as you develop the application. If you still have the Hello World project open from the previous tutorial, right-click Form1. A pop-up menu will appear, as shown in Figure On the pop-up menu, click View Code. It will be helpful for you to know how this code is organized, however, because later you will add your own code to this file.

C code is primarily organized in three ways: namespaces, classes, and methods. So, C code is organized as methods, which are contained inside classes, which are contained inside namespaces. NET Framework, which is a collection of classes and other code. The code in the. NET Framework is organized into namespaces. The series of using directives that appears at the top of a C source code file indicate which namespaces in the.

This section of code creates a namespace for the project. This section of code is a class declaration. The line that reads public partial class, and so forth, marks the beginning of the class. This section of code is a method. The line that reads public Form1 marks the beginning of the method. The code that appears between these braces is inside the method. Figure shows how the braces in Form1. While developing a Visual C application, you will often find yourself needing to switch back and forth between the Designer and the code editor.

One way to quickly switch between the two windows is to use the tabs shown in Figure In the figure, notice that the leftmost tab reads Form1.

That is the tab for the code editor. The rightmost tab reads Form1. That is the tab for the Designer. The tabs may not always appear in this order. To switch between the Designer and the code editor, you simply click the tab for the desired window. This allows you to see the code editor and the Designer at the same time.

As shown in Figure , click the code editor tab and drag it to the desired location on the screen. If you have multiple monitors connected to your computer, you can even drag the code editor to a different monitor. To return the code editor to its position within the IDE, right-click the tab for the source code file in the code editor window and select Move to Main Document Group.

Suppose you want the application to display the message Thanks for clicking the button! To accomplish that, you need to write a special type of method known as an event handler.

An event handler is a method that executes when a specific event takes place while an application is running. In this project you need to write an event handler that will execute when the user clicks the myButton control.

To create the event handler, you double-click the myButton control in the Designer. This opens the Form1. The code that has been added to the Form1. For now you do not need to understand all parts of the event handler m code. This is the typical naming convention that Visual Studio uses when it generates event handler code.

You can think of it as an empty container to which you can add your own code. Now you know how to create an empty Click event handler for a Button control. But what code do you write inside the event handler? Message Boxes A message box is a small window, sometimes referred to as a dialog box, that displays a message. Figure shows an example of a message box displaying the message Thanks for clicking the button! Notice that the message box also has an OK button.

When the user clicks the OK button, the message box closes. Show that you can use in Visual C to display a message box. If you want to execute the MessageBox. Show method, you write a statement known as a method call. Programmers refer to the act of executing a method as calling the method. The following statement shows an example of how you would call the MessageBox. Show method to display the message box shown in Figure MessageBox. Show "Thanks for clicking the button! Show method, you write a string of characters inside the parentheses.

In programming, we use the term string to mean string of characters. The string that is written inside the parentheses will be displayed in the message box.

Notice that the string is enclosed in double quotation marks in the code. When the message is displayed as shown in Figure , however, the double quotation marks do not appear. The double quotation marks are required in the code to indicate the beginning and the end of the string. Also notice that a semicolon appears at the end of the statement. This is required by C syntax. Just as a period marks the end of a sentence, a semicolon marks the end of a programming statement in C.

When the application runs, it will display the form shown on the left in Figure When you click the button, the message box shown on the right in the figure will appear. You can click the OK button on the message box to close it. The answer is no, you cannot skip this step. This other code is necessary for the event handler to properly function.

String Literals Programs almost always work with data of some type. For example, the code shown in Figure uses the following string when it calls the MessageBox. Show method: "Thanks for clicking the button!

In C , string literals must be enclosed in double quotation marks. Many of the applications that you will develop will have multiple buttons, each with its own Click event handler. For example, the form shown in Figure has three Button controls. As shown in the figure, the controls are named firstButton, secondButton, and thirdButton. Show statement has been added to each one. During design time, you can use the Designer and the Toolbox to place controls on the form, use the Properties window to set property values, use the code editor to write code, and so forth.

This is the phase during which you create or modify the application. The project will be compiled, and if there were no errors, it will be executed. The time during which an application is executing is referred to as run time. During run time, you can interact with the running application, but you cannot use the Designer, the Toolbox, the Properties window, the code editor, or parts of Visual Studio to make changes to it. All these variations typically mean the same thing.

In Tutorial , you will open the project and add a Click event handler for the messageButton control. The event handler will call the MessageBox. Show method to display a message box with the message Hello World. Open the Hello World project that you started in Tutorial Step 2: Make sure the Form1 form is visible in the Designer, as shown in Figure If it is not, right-click Form1.

Step 3: In the Designer, double-click the messageButton control. This should cause the code editor to appear as shown in Figure When you have finished, the code window should look like Figure If you did not type the statement correctly, however, a window will appear reporting build errors. If that happens, click the No button in the window and then correct the statement so it appears exactly as shown in Figure When you click the Display Message button, the message box shown on the right in the figure will appear.

Label controls can be used to display unchanging text, or program output. When you want to display text on a form, you use a Label control. Figure shows an example of a form with two Label controls. Once you have placed a Label control on a form, you set its Text property to the text that you want to display. To create a Label control on a form, you double-click the Label control tool in the Toolbox.

As shown in Figure , a Label control will be created on the form. Alternatively, you can click and drag the Label control tool from the Toolbox onto the form. Notice that a bounding box appears around the Label control in the figure.

When you create Label controls, they are automatically given default names such as label1, label2, and so forth. So, a Label control will display its own name when it is created, as shown by the example in Figure When a Label control is selected in the Designer, you can use the Properties window to change its Text property.

Select a font, font style, and size, and click OK. The text displayed by the control will be updated with the selected attributes. Figure shows an example of Label controls with each BorderStyle setting. The image on the left in the figure shows the form in the Designer, and the image on the right shows the form at runtime.

The AutoSize property is a Boolean property, which means that it can be set to one of two possible values: True or False. For example, look at the three Label controls in Figure Each of the controls displays different amounts of text at different font sizes. If you want to manually change the size of a Label control, you have to set its AutoSize property to False.

When AutoSize is set to False, sizing handles will appear around the control, allowing you to 99 Chapter 2 Introduction to Visual C click and drag the bounding box to resize the control. In the figure, the Label control has been resized so it is much larger than the text it displays. For example, look at the label shown in Figure What if we want the text to be aligned differently within the label? For example, what if we want the text to be centered in the label or positioned in the lower-right corner?

Figure shows nine Label controls, each with a different TextAlign value. This causes a dialog box with nine buttons, as shown in the left image in Figure , to appear. As shown in the right image in the figure, the nine buttons represent the valid settings of the TextAlign property.

Using Code to Display Output in a Label Control In addition to displaying unchanging text on a form, Label controls are also useful for displaying output while an application is running.

For example, suppose you are creating an application that performs a calculation and you want to display the result of the 2. Using a Label control to display the output would be an ideal solution. This causes the result to be displayed on the form in the Label control. For example, suppose you have created a Label control and named it outputLabel.

It assigns the value that appears on its right side to the item that appears on its left side. In this example, the item on the left side of the assignment operator is the expression outputLabel. The value on the right side of the assignment operator is the string "Thank you very much". When this statement executes, the text Thank you very much is displayed in the Label control. In the Chap02 folder, you will find a project named Presidential Trivia.

The purpose of the application is to display a trivia question about a former U. When the user clicks a button, the answer to the trivia question is displayed on the form. This label displays the trivia question. This label initially appears empty, but will be used to display the answer to the trivia question. When the user clicks this button, the answer to the trivia question is displayed.

Table lists the property settings for each control of which you should take note. To open the file in the code window, right-click Form1. It contains the following statement: answerLabel. As a result, Theodore Roosevelt is displayed in the label control. When you run the application, the form appears as shown on the left in Figure Click the Show the Answer button and the answer to the trivia question appears as shown on the right in the figure.

You cannot assign a number to the Text property. If you put quotation marks around the number, it becomes a string. The following statement will work: resultLabel. The form will have three buttons: one for Italian, one for Spanish, and one for German. When the user clicks any of these buttons, the translated phrase will appear in a Label control.

The form has two Label controls and three Button controls. The names of the controls are shown in the figure. As you place each of the controls on the form, refer to Table for the relevant property settings. Write the following statement inside the event handler: translationLabel.

Note that the line numbers are not part of the code. The line numbers are shown so that you and your instructor can more easily refer to different parts of the program. The lines that appear in boldface are the ones that you typed. Make sure the code you typed appears exactly as shown here. Generic; System. ComponentModel; System. Data; System. Drawing; System. Linq; System. Text; System. Tasks; System. If you did not type the statements correctly inside the event handlers, a window will appear reporting build errors.



0コメント

  • 1000 / 1000