Programming in visual c# 2008 pdf download






















Content moves from the absolute basics to fairly involved OOP skills. Web Programming: Putting together basic Web applications, highlighting differences between Web and Windows programming. Articulate Storyline Essentials. Beginning SharePoint Development.

Beginning SharePoint Beginning PowerShell for SharePoint Follow Us! Latest Books. Click on the entry for which you are searching and the corresponding page appears in the Document window. For most controls, such as the Label control that appears in Figure 1.

For now, always choose Windows Forms. The Help topic and Search appear in tabbed windows in the main Document window; Index, Contents, and Help Favorites appear in tabbed windows docked at the left of the main window. A good way to start using Help is to view the topics that demonstrate how to look up topics in Help. Make sure to visit Managing Help Topics and Windows, which has subtopics describing how to copy topics and print topics.

Select a C object, such as a form or a control, or place the insertion point in a word in the editor and press F1. The Help window pops up with the corresponding Help topic displayed, if possible, saving you a search. Managing Windows At times you may have more windows and tabs open than you want. You can hide or close any window, or switch to a different window. Only the top window will close. Click on the main topic, Button control [Windows Forms]: and click on the entry for about Button control.

The topics included for the Professional Edition are more extensive than those for the Express Edition. In the Express Edition, only one page matches the selection and it appears in the main Document window. In the Professional Edition, several topics appear in the Index Results list. Click on a title in the Index Results to display the corresponding page in the Document window. Notice that additional links appear in the text in the Document window. You can click on a link to view another topic.

Display the Editor window of your Hello World project. Click on the Close method to place the insertion point. Press the F1 key to view context-sensitive Help. Include a label for the current special and buttons for each of the following departments: Clothing, Equipment and Accessories, Juice Bar, Membership, and Personal Training. Use appropriate names for all controls. Make sure to change the Text property of the form. Planning the Project Sketch a form Figure 1.

Plan the Objects and Properties Plan the property settings for the form and for each control. Plan the Event Methods You will need event-handling methods for each button. System; System. Visual C is an object-oriented language primarily used to write application programs that run in Windows or on the Internet using a graphical user interface GUI. In the OOP object model, classes are used to create objects that have properties, methods, and events.

NET Framework provides an environment for the objects from many languages to interoperate. To plan a project, first sketch the user interface and then list the objects and properties needed. Then plan the necessary event-handling methods. The three steps to creating a C project are 1 define the user interface, 2 set the properties, and 3 write the code.

A C application is called a solution. Each solution may contain multiple projects, and each project may contain multiple forms and additional files. The solution file has an extension of. In addition, the Visual Studio environment and the C compiler both create several more files. The Visual Studio integrated development environment IDE consists of several tools, including a form designer, an editor, a compiler, a debugger, an object browser, and a Help facility.

Visual Studio has three modes: design time, run time, and debug time. You create the user interface for an application by adding controls from the toolbox to a form. You can move, resize, and delete the controls. The Name property of a control is used to refer to the control in code. The Text property holds the words that the user sees on the screen. C code is written in methods. Project comments are used for documentation.

Good programming practice requires comments in every method and at the top of a file. Most C statements must be terminated by a semicolon. A statement may appear on multiple lines; the semicolon determines the end of the statement.

Comments and some other statements do not end with semicolons. Assignment statements assign a value to a property or a variable. Assignment statements work from right to left, assigning the value on the right side of the equal sign to the property or variable named on the left side of the equal sign. The this. Close method terminates program execution. Each event to which you want to respond requires an event-handling method, also called an event handler. You can print out the C code for documentation.

Three types of errors can occur in a C project: syntax errors, which violate the syntax rules of the C language; run-time errors, which contain a statement that cannot execute properly; and logic errors, which produce erroneous results. Finding and fixing program errors is called debugging. You must have a clean compile before you run the program. Following good naming conventions can help make a project easier to debug. C Help has very complete descriptions of all project elements and their uses.

What are objects and properties? How are they related to each other? What are the three steps for planning and creating C projects? Describe what happens in each step. What is the purpose of these C file types:. When is C in design time? What is the purpose of the Name property of a control?

Which property determines what appears on the form for a Label control? What is the purpose of the Text property of a button? To what does displayButton refer? To what does Click refer? What is a C event? Give some examples of events. What property must be set to center text in a label? What should be the value of the property? Describe the two types of comments in a C program and tell where each is generally used.

What is meant by the term debugging? What is a syntax error, when does it occur, and what might cause it? What is a run-time error, when does it occur, and what might cause it? What is a logic error, when does it occur, and what might cause it? Tell the class of control and the likely purpose of each of these object names: addressLabel exitButton nameTextBox What does context-sensitive Help mean?

How can you use it to see the Help page for a button? You may substitute any other languages for those shown. Feel free to modify the user interface to suit yourself or your instructor. Make sure to use meaningful names for your new buttons, following the naming conventions in Table 1.

Include comments at the top of every method and at the top of the file. Each button should display the hours for that department in a label. The interface should have one label for the hours, one label for the programmer name, buttons for each department, and an Exit button. Make a button for each saying with a descriptive Text property for each, as well as a button to exit the project.

Include a label that holds your name at the bottom of the form. You may change the Font properties of the label to the font and size of your choice. Make sure the buttons are large enough to hold their entire Text properties. Include buttons and labels for the contact person, department, and phone. When the user clicks on one of the buttons, display the contact information in the corresponding label. Include a button to exit. Include a label that holds your name at the bottom of the form and change the title bar of the form to something meaningful.

You may change the Font properties of the labels to the font and size of your choice. Follow good naming conventions for object names; include comments at the top of every method and at the top of the file. Make up a name for your diner and display it in a label at the top of the form. Add a label to display the appropriate special depending on the button that is pressed.

We can place an order and ship it to you. We also help with shopping for gifts; your order can be gift wrapped and sent anywhere you wish. The company title will be shortened to CS Mail Order. Include this name on the title bar of the first form of each project that you create for this case study. Your first job is to create a project that will display the name and telephone number for the contact person for the customer relations, marketing, order processing, and shipping departments.

Include a button for each department. When the user clicks on the button for a department, display the name and telephone number for the contact person in two labels.

Include a label at the bottom of the form that holds your name and give the form a meaningful title bar. The center has facilities with everything for your vehicles including sales and leasing for new and used cars and RVs, auto service and repair, detail shop, car wash, and auto parts.

Your first job is to create a project that will display current notices. Be sure to include a button for Exit. Include your name in a label at the bottom of the form. Mann x Xtreme Cinema This neighborhood store is an independently owned video rental business.

The owners would like to allow their customers to use the computer to look up the aisle number for movies by category. Create a form with a button for each category. When the user clicks on a button, display the corresponding aisle number in a label. Include a label that holds your name at the bottom of the form and change the title bar of the form to Xtreme Cinema. You may change the font properties of the labels to the font and size of your choice.

Include additional categories, if you wish. Follow good programming conventions for object names; include comments at the top of every method and at the top of the file. Management wants a computer application to allow their employees to display the address and hours for each of their branches.

Create a form with a button for each store branch. When the user clicks on a button, display the correct address and hours. Include a label that holds your name at the bottom of the form and change the title bar of the form to Cool Boards. Make up hours and locations for each. Use text boxes, masked text boxes, rich text boxes, group boxes, check boxes, radio buttons, and picture boxes effectively.

Set the BorderStyle property to make controls appear flat or three-dimensional. Select multiple controls and move them, align them, and set common properties. Make your projects easy for the user to understand and operate by defining access keys, setting an Accept and a Cancel button, controlling the tab sequence, resetting the focus during program execution, and causing ToolTips to appear.

Clear the contents of text boxes and labels. Make a control visible or invisible at run time by setting its Visible property. Disable and enable controls at design time and run time. Change text color during program execution. Concatenate join strings of text. Download the Line and Shape controls, add them to the toolbox, and use the controls on your forms.

In this chapter you will learn to use several more control types: text boxes, group boxes, check boxes, radio buttons, and picture boxes. Figure 2. Each class of controls has its own set of properties. To see a complete list of the properties for any class of control, you can 1 place a control on a form and examine the properties list or 2 click on a tool or a control and press F1 for context-sensitive Help.

Visual Studio will display the Help page for that control, and you can view a list of the properties and an explanation of their use.

The form in Figure 2. In your program code, you can use the Text property of each text box. Example nameLabel. Text; In this example, whatever the user enters into the text box is assigned to the Text property of nameLabel. If you want to display some text in a text box during program execution, assign a literal to the Text property: messageTextBox.

In the Properties window, set the property to Left, Right, or Center. In code, you can set the property using these values: HorizontalAlignment. Left HorizontalAlignment. Right HorizontalAlignment. Center messageTextBox. You can specify the format the Mask property of the data required of the user. For example, you can select a mask for a ZIP code, a date, a phone number, or a social security number. At run time, the user cannot enter characters that do not conform to the mask. For example, the phone number and social security number masks do not allow input other than numeric digits.

Example Names for Masked Text Boxes dateMaskedTextBox phoneMaskedTextBox Note: For a date or time mask, the user can enter only numeric digits but may possibly enter an invalid value; for example, a month or hour greater than The mask will accept any numeric digits, which could possibly cause your program to generate a run-time error.

You will learn to check the input values in Chapter 4. Contents of. One common use for a rich text box is for displaying URL addresses. In a regular text box, the address appears in the default font color, but the rich text box displays it as a link when the DetectUrl property is set to true. Note that it is not an active link, but it does have the formatting to show the URL as an address. You also can load formatted text into a rich text box from a file stored in rich text format rtf.

Use the LoadFile method of the rich text box. In Figure 2. LoadFile "Rich Text Boxes. The WordWrap property determines whether the contents should wrap to a second line if they do not fit on a single line. The property is set to true by default.

Both controls also have a Multiline property, which is set to false by default on a text box and true by default on a rich text box. Both WordWrap and Multiline must be set to true for text to wrap to a second line. For a regular text box, you must set Multiline to true and then adjust the height to accommodate multiple lines. If Multiline is false the default , a text box does not have resizing handles for vertical resizing.

Be aware that a text box will not automatically resize to display multiple lines even though Multiline is true; you must make the height tall enough to display the lines.

Properties button Editor for entering the Text property You can set the Text property of a multiline text box or rich text box to a very long value; the value will wrap to fit in the width of the box. You also can enter multiple lines and choose the location of the line breaks; the techniques differ depending on whether you set the Text property at design time or in code.

In code, you can use a NewLine character Environment. NewLine in the text string where you want the line to break. Usually, groups of radio buttons or check boxes are placed in group boxes. Using group boxes to group controls can make your forms easier to understand by separating the controls into logical groups. You can find the GroupBox control in the Containers tab of the toolbox.

One reason to use it in code is to set the Enabled property of the group box to false, which disables all of the controls inside the box. Check Boxes Check boxes allow the user to select or deselect an option. In any group of check boxes, any number can be selected.

The Checked property of a check box is set to false if unchecked or true if checked. You can write an event handler for the CheckedChanged event, which executes when the user clicks in the box. In Chapter 4, when you learn about if statements, you can take one action when the box is checked and another action when it is unchecked.

Use the Text property of a check box for the text you want to appear next to the box. Any radio buttons that you place directly on the form not in a group box function as a group.

A group of radio buttons inside a group box function together. The best method is to first create a group box and then create each radio button inside the group box. When you need separate lists of radio buttons for different purposes, you must include each list in a separate group box. You can find an example program later in this chapter that demonstrates using two groups of radio buttons, one for setting the background color of the form and a second set for selecting the color of the text on the form.

In Chapter 4 you will learn to determine in your code whether or not a button is selected. Place a PictureBox control on a form and then select its Image property in the Properties window.

Click on the Properties button Figure 2. Click on the Properties button to view the Select Resource dialog box. Properties button Figure 2. Make your selection here for the graphic file you want to appear in the PictureBox control; click Import to add an image to the list. An Open dialog box appears Figure 2. A preview of the image appears in the preview box. Note: To add files with an.

You can use any graphic file with the proper format that you have available. PictureBox controls have several useful properties that you can set at design time or run time. For example, set the SizeMode property to StretchImage to make the graphic resize to fill the control.

You can set the Visible property to false to make the picture box disappear. For example, to make a picture box invisible at run time, use this code statement: logoPictureBox. To accomplish this, set the Image property to null, which means empty. You also can add, remove, and rename resources using the Visual Studio Project Designer. From the Project menu, select ProjectName Properties which always shows the name of the selected project.

The Project Designer opens in the main Document window; click on the Resources tab to display the project resources Figure 2. You can use the buttons at the top of the window to add and remove images, or right-click an existing resource to rename or remove it. You can add, remove, and rename resources on this page. Using Smart Tags You can use smart tags to set the most common properties of many controls.

When you add a PictureBox or a TextBox to a form, for example, you see a small arrow in the upper-right corner of the control. The smart tag shows a few properties that you can set from there, which is just a shortcut for making the changes from the Properties window. Using Images for Forms and Controls You can use an image as the background of a form or a control. Setting a Border and Style Most controls can appear to be three-dimensional or flat.

Text boxes default to Fixed3D; labels and picture boxes default to None. Of course, you can change the property to the style of your choice.

You may want to include lines when creating a logo or you may simply want to divide the screen by drawing a line.

To create the look of a line, set the AutoSize property of your label to false, set the Text property to blank, change the BorderStyle to None, and change the Backcolor to the color you want for the line.

You can control the size of the line with the Width and Height properties, located beneath the Size property. Another way to draw a line on a form is to use the LineShape control, which you can download and install into Visual Studio. You also can draw a line on the form using the graphics methods. Drawing graphics is covered in Chapter Working with Multiple Controls You can select more than one control at a time, which means that you can move the controls as a group, set similar properties for the group, and align the controls.

Selecting Multiple Controls There are several methods of selecting multiple controls. If the controls are near each other, the easiest technique is to use the mouse to drag a selection box around the controls. Point to a spot that you want to be one corner of a box surrounding the controls, press the mouse button, and drag to the opposite corner Figure 2. When you release the mouse button, the controls will all be selected Figure 2.

Note that selected labels and check boxes with AutoSize set to true do not have resizing handles; other selected controls do have resizing handles. You also can select multiple controls, one at a time. Click on one control to select it, hold down the Ctrl key or the Shift key, and click on the next control.

You can keep the Ctrl or Shift key down and continue clicking on controls you wish to select. Ctrl—click or Shift—click on a control a second time to deselect it without changing the rest of the group. Selection handles Resizing handles When you want to select most of the controls on the form, use a combination of the two methods. Drag a selection box around all of the controls to select them and then Ctrl—click on the ones you want to deselect.

Just click anywhere on the form not on a control or select another previously unselected control. To do this, point inside one of the selected controls, press the mouse button, and drag the entire group to a new location Figure 2. After selecting the group, look at the Properties window. Any properties that appear in the window are shared by all of the controls and can be changed all at once.

For example, you may want to set the BorderStyle property for a group of controls to threedimensional or change the font used for a group of labels. Some properties appear empty; even though those properties are common to all the selected controls, they do not share a common value. You can enter a new value that will apply to all selected controls. TIP Setting the font for the form changes the default font for all controls on the form. Select your group of controls and choose any of the resizing buttons.

These can make the controls equal in width, height, or both. Then select another button to align the tops, bottoms, or centers of the controls. You also can move the entire group to a new location. Note: The alignment options align the group of controls to the control that is active indicated by white sizing handles.

Referring to Figure 2. To make another selected control the active control, simply click on it. These buttons enable you to create equal spacing between controls or to increase or decrease the space between controls. Your user interface should be clear and consistent.

Because most of your users will already know how to operate Windows programs, you should strive to make your programs look and behave like other Windows programs.

User Interface Design E R Text 2 controls operate in the standard way, define keyboard access keys, set an Accept button, and make the Tab key work correctly.

You also can define ToolTips, which are those small labels that pop up when the user pauses the mouse pointer over a control. The best way that we can accomplish these goals is to follow industry standards for the color, size, and placement of controls.

Once users become accustomed to a screen design, they will expect and feel more familiar with applications that follow the same design criteria. You should design your applications to match other Windows applications. Microsoft has done extensive program testing with users of different ages, genders, nationalities, and disabilities. We should take advantage of this research and follow their guidelines.

Take some time to examine the screens and dialog boxes in Microsoft Office as well as those in Visual Studio. One recommendation about interface design concerns color. You have probably noticed that Windows applications are predominantly gray. A reason for this choice is that many people are color blind. Also, research shows that gray is easiest for the majority of users. Although you may personally prefer brighter colors, you will stick with gray, or the system palette the user chooses, if you want your applications to look professional.

If the user changes the system theme or color, your forms and controls will conform to their settings. Colors can indicate to the user what is expected.

Use a white background for text boxes to indicate that the user should input information. Use a gray background for labels, which the user cannot change. Labels that will display a message should have a border around them; labels that provide text on the screen should have no border the default.

Group your controls on the form to aid the user. A good practice is to create group boxes to hold related items, especially those controls that require user input. This visual aid helps the user understand the information that is being presented or requested. Use a sans serif font on your forms, such as the default MS Sans Serif, and do not make them boldface.

Limit large font sizes to a few items, such as the company name. Defining Keyboard Access Keys Many people prefer to use the keyboard, rather than a mouse, for most operations. Windows is set up so that most functions can be done with either the keyboard or a mouse. You can make your projects respond to the keyboard by defining access keys, also called hot keys.

For example, in Figure 2. You can set access keys for buttons, radio buttons, and check boxes when you define their Text properties.

First, try to use the Windows standard keys whenever possible. For example, use the x of Exit and the S of Save. Only the next control from the currently active control in the tab sequence is activated when the user presses the access key.

Note: To view the access keys on controls or menus in Windows , Windows XP, or Windows Vista, you may have to press the Alt key, depending on your system settings. Select Change how your keyboard works and check the box for Underline keyboard shortcuts and access keys in the Make the keyboard easier to use dialog. Setting the Accept and Cancel Buttons Are you a keyboard user? If so, do you mind having to pick up the mouse and click a button after typing text into a text box? If one of the buttons on the form is the Accept button, pressing Enter is the same as clicking the button.

You can make one of your buttons the Accept button by setting the AcceptButton property of the form to the button name. When the user presses the Enter key, that button is automatically selected. You also can select a Cancel button.

The Cancel button is the button that is selected when the user presses the Esc key. Setting the Tab Order for Controls In Windows programs, one control on the form always has the focus. You can see the focus change as you tab from control to control.

For many controls, such as buttons, the focus appears as a thick border. Other controls indicate the focus by a dotted line or a shaded background. For text boxes, the insertion point also called the cursor appears inside the box. Some controls can receive the focus; others cannot. For example, text boxes and buttons can receive the focus, but labels and picture boxes cannot.

The Tab Order Two properties determine whether the focus stops on a control and the order in which the focus moves. Controls that are capable of receiving focus have a TabStop property, which you can set to true or false.

If you do not want the focus to stop on a control when the user presses the Tab key, set the TabStop property to false. The TabIndex property determines the order the focus moves as the Tab key is pressed. As you create controls on your form, Visual Studio assigns the TabIndex property in sequence. Most of the time that order is correct, but if you want to tab in some other sequence or if you add controls later, you will need to modify the TabIndex properties of your controls.

When your program begins running, the focus is on the control with the lowest TabIndex usually 0. Since you generally want the insertion point to appear in the first control on the form, its TabIndex should be set to 0.

The next control should be set to 1; the next to 2; and so forth. You may be puzzled by the properties of labels, which have a TabIndex property but not a TabStop.

A label cannot receive focus, but it has a location in the tab sequence. This fact allows you to create keyboard access keys for text boxes. See Figure 2.

Be aware that the behavior of radio buttons in the tab sequence is different from other controls: The Tab key takes you only to one radio button in a group the selected button , even though all buttons in the group have their TabStop and TabIndex properties set. If you are using the keyboard to select radio buttons, you must tab to the group and then use your Up and Down arrow keys to select the correct button. The result varies depending on the location of the focus and is very confusing.

The Tab Order item does not appear on the menu and is not available on the Layout toolbar unless the Design window is active. Small numbers appear in the upper-left corner of each control; these are the current TabIndex properties of the controls. Click first in the control that you want to be TabIndex zero, then click on the control for TabIndex one, and then click on the next control until you have set the TabIndex for all controls Figure 2. When you have finished setting the TabIndex for all controls, the white numbered boxes change to blue.

If you make a mistake and want to change the tab order, turn the option off and on again, and start over with TabIndex zero again, or you can keep clicking on the control until the number wraps around to the desired value. TIP To set the tab order for a group of controls, first set the TabIndex property for the group box and then set the TabIndex for controls inside the group. Creating ToolTips If you are a Windows user, you probably appreciate and rely on ToolTips, those small labels that pop up when you pause your mouse pointer over a toolbar button or control.

You can easily add ToolTips to your projects by adding a ToolTip component to a form. The new control appears in the component tray that opens at the bottom of the Form Designer Figure 2.

The component tray holds controls that do not have a visual representation at run time. You will see more controls that use the component tray later in this text.

After you add the ToolTip component, examine the properties list for other controls on the form, such as buttons, text boxes, labels, radio buttons, check boxes, and even the form itself. Each has a new ToolTip on toolTip1 property. Try this example: Add a button to any form and add a ToolTip component.

Students will use. Microsoft Excel 97 or above should be installed and activated on your PC. An informative, engaging, challenging and entertaining introduction to Visual C. Visual Basic How to Program is now available! You can preorder the book through Amazon.

See the blog entry about the book from Microsoft's Joe Stagner.



0コメント

  • 1000 / 1000