info 3231 chapter 3

¡Supera tus tareas y exámenes ahora con Quizwiz!

Name and describe some of the properties of Buttons.

ANSWER: Some of the properties of buttons are as follows: BackColor-Gets or sets the background color for the control Enabled-Gets or sets whether the control is enabled Font-Gets or sets the current font for the control ForeColor-Gets or sets the foreground color of the control Name-Gets or sets the name of the control Size-Gets or sets the size of the control Text-Gets or sets the text associated with the control Visible-Gets or sets a value indicating whether the control is visible

Explain how to add functionality to a Button on a Form.

Adding functionality to a Button is easy when you use the IDE. After you have dragged a Button onto a Form, you can double-click it to create a method that executes when the user clicks the button. As you create GUI applications, you will frequently switch back and forth between the visual environment and the code. You can use the tabs or shortcut keys, or click View on the main menu and then click Code or Designer for the desired view. If you scroll through the exposed code, you will see many generated statements, some of which are confusing. However, to make a Button perform an action, you can ignore most of the statements. You only need to write code between the curly braces of the method with the following header: private void button1_Click(object sender, EventArgs e) You can write any statements between the curly braces of the button1_Click() method. For example, you can declare variables, perform arithmetic statements, and produce output.

Explain how to refactor your code to fix problems after renaming a Button that already had a Click() method.

Code refactoring is the process of changing a program's internal structure without changing the way the program works. After you change the name of the Button using the Designer, switch to view the code. Right-click the name of the method (e.g., button1_Click()). From the menu that appears, click Rename. When you click Rename, a dialog box opens. Type the new method name-for example, using the button's Name, an underscore, and Click, but not adding the parentheses that appear in the method header. When you finish, click OK. A Preview Changes dialog box will highlight the change. You can confirm the change by clicking Apply.

Compare GUI and console applications. How would you choose which is more appropriate for a given task?

GUI applications look "snazzier." It is easy to add colors and fonts to them, and they contain controls that a user can manipulate with a mouse. Also, users are accustomed to GUI applications from their experiences on the Web. However, GUI programs usually take longer to develop than their console counterparts because you can spend a lot of time setting up the controls, placing them on a Form, and adjusting their sizes and relative positions before you write the code that does the actual work of the program. GUI applications created in the IDE also require much more disk space to store. Console applications look duller, but they can often be developed more quickly because you do not spend much time setting up objects for a user to manipulate. When you are learning programming concepts like decision-making and looping, you might prefer to keep the interface simple so that you can better concentrate on the new logical constructs being presented.

What are Labels and how do you use them?

Labels are controls that you use to display text to communicate with an application's user. Just like a Button, you can drag a Label onto a Form. By default, the first Label you drag onto a Form is named label1. You can change its Text property to display any string of text; depending on the amount of text you enter, you might need to change the size of the Label by dragging its resizing handles or altering its Size property. If you want to create multiple lines of text on a Label, use the arrow to the right of the Text property to open an editor and type the needed text in separate lines.

What are TextBoxes and how do you use them?

TextBoxes are controls through which a user can enter input data in a GUI application. If a user will enter data in a TextBox, you frequently want to start with its Text property empty, but you are allowed to add text to its Text property if desired. When a user types a value into a TextBox in an executing program, it becomes the value for the Text property of the TextBox. Because a user might type any value, the value in a TextBox is a string by default. When you write an application in which the user is supposed to enter a number, you must convert the entered string into a number. You are familiar with this process because it's exactly the same action you must take when a user enters data in response to a ReadLine() statement in a console application.

How do you create a Form visually using the Visual Studio C# IDE?

To create a Form visually using the IDE, you select New Project after starting Visual Studio and then choose Windows Forms Application. By default, Visual Studio names your first Windows Forms Application WindowsFormsApplication1. You can change the name if you want, and you can browse to choose a location to save the application.

What conventions are usually applied when naming Forms and controls?

Usually, you want to provide reasonable Name property values for all the controls you place on a Form. Although any identifier that starts with a letter is legal, note the following conventions: * Start control names with a lowercase letter and use camel casing as appropriate. * Start Form names with an uppercase letter and use camel casing as appropriate. A Form is a class and, by convention, C# class names start with an uppercase letter. * Use the type of the object in the name. For example, use names such as okButton, firstValueTextBox, or ArithmeticForm. * Professional programmers usually do not retain the default names for Forms and controls. An exception is sometimes made for Labels that never change. For example, if three labels provide directions or explanations to the user and are never altered during a program's execution, many programmers would approve of retaining their names as label1, label2, and label3. Most often, you will want to name controls as soon as you add them to a Form. If you rename a control after you have created an event for it, the event code will generate errors. To fix this, you must refactor the code.

How do you delete an unwanted event-handling method?

When you are working in the Form Designer in the IDE, it is easy to inadvertently double-click a control and automatically create an event-handling method that you do not want. For example, you might double-click a Label named label1 by mistake and generate a method named label1_Click(). You can leave the automatically created method empty so that no actions result, but in a professional program you typically would not include empty methods. Such empty methods that never execute are known as orphaned methods. You should not just delete an orphaned method because, behind the scenes, other code might have been created that refers to the method; if so, the program will not compile. Instead of deleting the method code, you should click the control with the unwanted method so it appears in the Properties window, then click the Events button, which looks like a lightning bolt. Select the event you want to eliminate, and delete the name of the method. This eliminates all the references to the method, and your program can again run successfully.

How do you use the Toolbox to add a control to a Form?

When you open the IDE, the left border displays a Toolbox tab. (If you do not see the tab, you can select View and then Toolbox from the IDE's main menu.) When you open the Toolbox, a list of tool groups is displayed. The list automatically closes when you move your mouse off the list and click elsewhere, or you can pin the Toolbox to the screen by clicking the pushpin icon at the top of the list. When you pin the Toolbox, the Form moves to the right to remain fully visible. Selecting All Windows Forms at the top of the Toolbox displays a complete list of available tools; selecting Common Controls displays a smaller list that is a subset of the original one. This list contains many controls: the GUI objects a user can click or manipulate. The list includes controls you probably have seen when using Windows applications-for example, Button, CheckBox, and Label. You can drag these controls onto the Form.


Conjuntos de estudio relacionados

Entrepreneurship ( 1st Quarter WITH HONORS)

View Set

Bio 110- Ch 6 Cellular Respiration

View Set

CH. 1-4 Test Review Law & Ethics

View Set

MNGT 3100 Test #2 Review Questions

View Set

PEDs Sucess: Growth and Development

View Set

Java 2 Final Inheritance and Classes

View Set