I NEARLY RE-INSTALLED MY OS!!!

I thought that my laptop already had too much software installed. And it's taking toll in my development environment. What put the idea in my head? When I try to move the mouse, it takes too long to go to the area I want it to go. Like waiting for a process to finish. Last resort was to reinstall...

The culprit: my faulty mouse....

FREE TRAINING!!! Cobol and .NET Programming Course


SORRY. NOT AVAILABLE AS OF THE MOMENT.
Provided by WINSOURCE SOLUTIONS
QUALIFICATION:
Must be a COLLEGE GRADUATE of an IT COURSE or IT RELATED
If interested just call 6673838 look for ABBY

Submission

August 6 Early Morning Quiz

Testing a Windows Forms Application (3 of 3)

From webpage

Testing a Windows Forms Application - Perspective


(Page 3 of 3 )

If you can code and test the Invoice Total project that's presented in this chapter, you've already learned a lot about Visual Basic programming. You know how to enter the code for the event handlers that make the user interface work the way you want it to. You know how to build and test a project. And you know some simple debugging techniques.

On the other hand, you've still got a lot to learn. In particular, you haven't learned much about the Visual Basic language. That's why the next six chapters present the Visual Basic essentials.

Terms

object-oriented programming

syntax error

object-oriented language

build error

object

code snippet

class

identifier

instance

smart tag menu

instantiation

My feature

property

comment out a line

method

bookmark

event

collapse

member

expand

dot operator

build a project

dot

run a project

argument

test a project

event-driven application

runtime error

event handler

exception

procedure declaration

bug

procedure name

debug

tool tip

break mode

line-continuation character

data tip

comment

Exercise 3-1 Code the Invoice Total form

In this exercise, you'll add code to the Invoice Total form that you designed in exercise 2-1. Then, you'll build and test the project to be sure it works. You'll also experiment with debugging and review some help information.

Copy and open the Invoice Total application

  1. Use the Windows Explorer to copy the Invoice Total project that you created for chapter 2 from the C:\VB 2008\Chapter 02 directory to the C:\VB 2008\Chapter 03 directory.
  2. Open the Invoice Total solution (InvoiceTotal.sln) that's now in the C:\VB 2008\Chapter 03\InvoiceTotal directory.

    Add code to the form and correct syntax errors
  3. Display the Invoice Total form in the Form Designer, and double-click on the Calculate button to open the Code Editor and generate the procedure declaration for the Click event of this object. Then, enter the code for this procedure as shown in figure 3-6. As you enter the code, be sure to take advantage of all of theVisual Studio features for coding including snippets.
  4. Return to the Form Designer, and double-click the Exit button to generate the procedure declaration for the Click event of this object. Enter the statement shown in figure 3-6 for this event handler.
  5. Open the Error List window as described in figure 3-9. If any syntax errors are listed in this window, double-click on each error to move to the error in the Code Editor. If the Auto Correction feature is available for an error, check to see whether its suggested correction (or one of its suggested corrections) is the one you want to make. Then, correct the error.

    Test the application
  6. Press F5 to build and run the project. If any syntax errors are detected, you'll need to correct the errors and press F5 again.
  7. When the application runs and the Invoice Total form is displayed, enter a valid numeric value in the first text box and click the Calculate button or press the Enter key to activate this button. Assuming that the calculation works, click the Exit button or press the Esc key to end the application. If either of these procedures doesn't work right, of course, you need to debug the problems and test the application again.

    Enter invalid data and display data tips in break mode
  8. Start the application again. This time, enter xx for the subtotal. Then, click the Calculate button. This will cause Visual Studio to enter break mode and display the Exception Assistant.
  9. Note the highlighted statement and read the message that's displayed in the Exception Assistant. Then, close the Assistant, and move the mouse pointer over the property in this statement to display its data tip. This shows that the code for this application needs to be enhanced so it checks for invalid data.
  10. Display the smart tag for the Text property in the highlighted statement, click its drop-down arrow, and select Text Visualizer. This shows the data exactly as it was entered in the Text Visualizer dialog box. Then, click the Stop Debugging button in Standard toolbar to end the application.

    Experiment with the Visual Basic features
  11. In the Dim statement for the discountPercent variable, change the variable name to discountPct. When you do that, a bar will appear under the last letter of the variable. Place the mouse pointer over this bar to display a drop-down arrow. Then, click on this arrow and select the Rename command. This should rename the discountPercent variable to discountPct throughout the form. But run the form to make sure it's working correctly.
  12. In the If statement, right-click on one of the occurrences of the variable named discountPct. Then, select the Rename command, and use it to rename this variable to discountPercent throughout the form. To make sure this worked, run the application.
  13. Select the lines that contain the ElseIf clauses and click on the Comment Out button in the Standard toolbar. That should change these coding lines to comments. Then, run the application to see how it works when these lines are ignored. When you're done, select the lines that were commented out and click on the Uncomment button to restore them.
  14. In the Code Editor, click on the minus sign in front of the btnCalculate_Click procedure to collapse it. Then, expand that procedure and collapse the btnExit_Click procedure. Last, print just the expanded code for this form.
  15. In the Solution Explorer, show all the files and double-click on the file named frmInvoiceTotal.Designer.vb to open it in the Code Editor. This is the code that determines how the form will look when it's instantiated. After you read chapter 11 and section 4, this code will make more sense to you. For now, though, just close the window with this code.

    Experiment with the Help feature
  16. To see how context-sensitive help works, place the insertion point in the Select method in the last statement of the first event handler and press F1. This should open a Help window that tells you more about this method.
  17. In the left pane, select the Index tab to display the Index window. Type "snippets" into the Look For box to see the entries that are listed under this topic. Next, if Visual Basic (or Visual Basic Express Edition) isn't selected in the Filtered By drop-down list, select it to show just the topics for Visual Basic. Then, click on one or more topics to display them.
  18. Use the Tools->Options command, and review the help options. In the Online group, you may want to change the loading option to "Try local only, not online," because that can speed up the access of help topics.
  19. Continue to experiment with the Index, Contents, Help Favorites, and Search features to see how they work, and try to use some of the buttons in the Standard toolbar to see how they work. Then, close the Help window.

    Exit from Visual Studio
  20. Click the Close button for the Visual Studio window to exit from this application. If you did everything and got your application to work right, you've come a long way!

Testing a Windows Forms Application (2 of 3)

From webpage

Testing a Windows Forms Application - How to debug runtime errors

 
(Page 2 of 3 )

When a runtime error occurs, Visual Studio enters break mode. In that mode, Visual Studio displays the Code Editor and highlights the statement that couldn't be executed, displays the Debug toolbar, and displays an Exception Assistant dialog box like the one shown in figure 3-17. This is designed to help you find the cause of the exception (the bug), and to debug the application by preventing the exception from occurring again or by handling the exception.

Often, you can figure out what caused the problem just by knowing what statement couldn't be executed, by reading the message displayed by the Exception Assistant, or by reading the troubleshooting tips displayed by the Exception Assistant. But sometimes, it helps to find out what the current values in some of the variables or properties in the program are.

To do that, you can place the mouse pointer over a variable or property in the code so a data tip is displayed as shown in figure 3-18. This tip displays the current value of the variable or property. You can do this with the Exception Assistant still open, or you can click on its Close button to close it. Either way, the application is still in break mode. In this figure, the data tip for the Text property of the txtSubtotal control is "$$1000", which shows that the user didn't enter valid numeric data.

Within the data tip, you'll see a magnifying glass and an arrow for a drop-down list. If you click on this arrow, you'll see the three choices shown in this figure. Then, if you click on Text Visualizer, the value in the data tip will be shown in the Text Visualizer dialog box the way it actually is. So in this simple example, the value will show as $$1000, not "$$1000". Although that isn't much different than what the data tip shows, the differences are more dramatic when the data is more complex.

Once you find the cause of a bug, you can correct it. Sometimes, you can do that in break mode and continue running the application. Often, though, you'll exit from break mode before fixing the code. To exit, you can click the Stop Debugging button in the Standard toolbar. Then, you can correct the code and test the application again.

For now, don't worry if you don't know how to correct the problem in this example. Instead, you can assume that the user will enter valid data. In chapter 7, though, you'll learn how to catch exceptions and validate all user entries for an application because that's what a professional application has to do. And in chapter 12, after you've learned the Visual Basic essentials, you'll learn a lot more about debugging.

How a project looks in break mode

Description

  1. When an application encounters a runtime error, you need to fix the error. This is commonly referred to asdebugging, and the error is commonly referred to as a bug
  2. When an application encounters a runtime error, it enters break mode. In break mode, the Debug toolbar is displayed along with the Exception Assistant window. 
     
  3. The information in the Exception Assistant window should give you an idea of what the error might be. You can also click on the links in the Troubleshooting Tips list to display more information in a Help window. 
     
  4. If you close the Exception Assistant window, the application remains in break mode. 
     
  5. To display a data tip for a property or variable, move the mouse pointer over it in the Visual Basic code. 
     
  6. If the data tip includes a drop-down arrow to the right of a magnifying glass, you can click the error and select Text Visualizer to see exactly what the data looks like. 
     
  7. To exit break mode and end the application, click the Stop Debugging button in the Standard toolbar or press Shift+F5. 
     
  8. You'll learn more about debugging and the Exception Assistant window in chapter 12.

 

Figure 3-18.   How to debug runtime errors


Testing a Windows Forms Application

From webpage

Testing a Windows Forms Application

 
(Page 1 of 3 )

Welcome to the conclusion of a five-part article series on how to code and test a Windows Forms application. In this part, we finally test our project. This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456).

How to test a project

When you test a project, you run it and make sure the application works correctly. As you test your project, you should try every possible combination of input data and user actions to be certain that the project works correctly in every case. In other words, your goal is to make the project fail. Figure 3-17 provides an overview of the testing process for Visual Basic applications.

To start, you should test the user interface. Make sure that each control is sized and positioned properly, that there are no spelling errors in any of the controls or in the form's title bar, and that the navigation features such as the tab order and access keys work properly.

Next, subject your application to a carefully thought-out sequence of valid test data. Make sure you test every combination of data that the project will handle. If, for example, the project calculates the discount at different values based on the value of the subtotal, use subtotals that fall within each range.

Finally, test the program to make sure that it properly handles invalid data entered by users. For example, type text information into text boxes that expect numeric data. Leave fields blank. Use negative numbers where they shouldn't be allowed. Remember that the goal of testing is to find all of the problems.

As you test your projects, you'll encounter runtime errors. These errors, also known as exceptions, occur when Visual Basic encounters a problem that prevents a statement from being executed. If, for example, a user enters "ABC" into the Subtotal text box on the Invoice Total form, a runtime error will occur when the program tries to assign that value to a decimal variable.

When a runtime error occurs, Visual Studio breaks into the debugger and displays an Exception Assistant window like the one in this figure. Then, you can use the debugging tools that you'll be introduced to in the next figure to debug the error.

Runtime errors, though, should only occur when you're testing a program. Before an application is put into production, it should be coded and tested so all runtime errors are caught by the application and appropriate messages are displayed to the user. You'll learn how to do that in chapter 7 of this book.

The Exception Assistant that's displayed when a runtime error occurs

How to test a project

  1. Test the user interface. Visually check all the controls to make sure they are displayed properly with the correct text. Use the Tab key to make sure the tab order is set correctly, verify that the access keys work right, and make sure that the Enter and Esc keys work properly. 
  2. Test valid input data. For example, enter data that you would expect a user to enter. 
     
  3. Test invalid data or unexpected user actions. For example, leave required fields blank, enter text data into numeric input fields, and use negative numbers where they are not appropriate. Try everything you can think of to make the application fail.

Description

  1. To test a project, you run the project to make sure it works properly no matter what combinations of valid or invalid data you enter or what sequence of controls you use. 
  2. If a statement in your application can't be executed, a runtime error, or exception, occurs. Then, if the exception isn't handled by your application, the statement that caused the exception is highlighted and an Exception Assistant window like the one above is displayed. At that point, you need to debug the application.

 

Figure 3-17.   How to test a project

Adding Features to a Windows Forms Application (5 of 5)

From webpage

Adding Features to a Windows Forms Application - Perspective

 
(Page 5 of 5 )

 

If you can design the Invoice Total form that's presented in this chapter, you've taken a critical first step toward learning how to develop Windows Forms applications with Visual Studio 2008. The next step is to add the code that makes the form work the way you want it to, and that's what you'll learn to do in the next chapter.

Terms

  template
  label
  text box
  button
  primary control
  property
  tab order
  focus
  access key

Exercise 2-1  Design the Invoice Total form

This exercise will guide you through the process of starting a new project and developing the user interface for the Invoice Total form shown in this chapter.

Set the default path and start a new project

  1. Start Visual Studio. If you want to change the import and export settings to make sure your menus are the same as the ones in this book, use Tools->Import and Export Settings command described in figure 2-1 to specify the default Visual Basic development settings. 
  2. Use the Tools->Options command to display the Options dialog box as shown in figure 2-1. Then, expand the Projects and Solutions group, select the General category, and change the Visual Studio projects location setting to C:\VB 2008. 
     
  3. If the Save New Projects When Created box and the Always Show Solution box aren't checked, check them. 
     
  4. If you want to stop the Start Page from being displayed each time you start Visual Studio, click on Startup within the Environment group. Then, select another option from the At Startup drop-down list. 
     
  5. If you're interested, take a few minutes to review the other options that are available in this dialog box. Then, close the dialog box. 
     
  6. Start a new project as shown in figure 2-2. The project should be named InvoiceTotal and it should be stored in the C:\VB 2008\Chapter 02 folder.  

    Add controls to the new form and set the properties 
  7. Use the techniques in figure 2-4 to add controls to the form so they have approximately the same sizes and locations as in figure 2-5. But don't worry about the size of the labels, just their locations. 
  8. Select groups of controls and use the buttons in the Layout toolbar to size and align the controls. But here again, let the labels automatically size themselves. Then, size the form so it looks like the one in figure 2-4. 
  9. Use the Properties window to set the properties for the form and its controls so it looks like the form in figure 2-3. These properties are summarized in figure 2-8. 
  10. Use the View->Other Windows->Document Outline command to open the window for Document Outline view. Next, use this window to check that you've named all of the controls that have Name properties in figure 2-8. Then, click on the controls in this view to see what happens, and close this window when you're done. 

    Test the user interface 
  11. Press F5 to build and run the project. That should display the form in the center of the screen, and it should look like the one in figure 2-3. 
  12. Experiment with the form to see what it can do. When you press the Tab key, notice how the focus moves from one control to another. When you click a button, notice how it indents and then pops back out just like any other Windows button control. Nothing else happens in response to these button clicks, though, because you haven't written the code for them yet. 

    Notice that the Calculate button has a dark outline around it to indicate that its function will be executed if you press the Enter key. (If it doesn't have a dark outline, you haven't set the AcceptButton property of the form to the button.) 
    When you press the Alt key, notice that an underline appears under the s in the Subtotal, the first c in Calculate, and the x in Exit to indicate that you can use an access key to work with these controls. (If the underlines don't show, you haven't entered the Text properties correctly.) 
  13. If you notice that some of the properties are set incorrectly, click the Close button in the upper right corner of the form to close the form. Then, make the necessary changes and run the project again. When you're satisfied that the form is working right, close the form to return to the Form Designer. 

    Experiment with the properties for the form and its controls 
  14. In the Form Designer, click on the form so it is selected. Then, if necessary, adjust the Properties window so you can see the description for each property. To do that, drag the bottom boundary of the window up. 
  15. Click on the Categorized button at the top of the Properties window to display the properties by category. Then, review the properties in the Appearance, Behavior, Layout, and Window Style categories. Although you won't understand all of the descriptions, you should understand some of them. 
  16. In the Window Style category, change the settings for the MaximizeBox and MinimizeBox to False to see how that changes the form. Then, to undo those changes, click twice on the Undo button in the Standard toolbar or press Ctrl+Z twice. 
  17. Click on the first text box and review the Appearance, Behavior, and Layout properties for that control. Then, repeat this process for one of the labels and one of the buttons. Here again, you won't understand all of the descriptions, but you should understand some of them. 
  18. Select all four of the labels, click on the plus sign before the Font property in the Appearance group, and change the Bold setting to True to see how that changes the form. Then, undo that change. 

    Change the name of the form files 
  19. Use one of the techniques presented in figure 2-9 to change the name of the form file from Form1.vb to frmInvoiceTotal.vb.  
  20. Note in the Solution Explorer that this also changes the name of the two subordinate files to frmInvoiceTotal.Designer.vb and frmInvoiceTotal.resx. To see these files, you may have to click on the Show All Files button and then on the plus sign for the frmInvoiceTotal.vb file. 

    Close the project and exit from Visual Studio
  21. Use the File->Close Solution command to close the project. If you've made any changes to the project since the last time you tested it, a dialog box is displayed that asks whether you want to save the changes that you made. If you want to save those changes, click Yes. 
  22. Use the File->Exit command to exit from Visual Studio.

Adding Features to a Windows Forms Application (4 of 5)

From webpage

Adding Features to a Windows Forms Application - How to name and save the files of a project

 
(Page 4 of 5 )

When you're working on a project, you may want to change the names of some of the files from their defaults. Then, you'll want to save the files with their new names.

How to name the files of a project

You may have noticed throughout this chapter that I didn't change the default name of the form (Form1.vb) that was added to the Invoice Total project when the project was created. In practice, though, you usually change the name of this form so it's more descriptive. For example, figure 2-9 shows how to use the Solution Explorer to change the name of the form file to frmInvoiceTotal.vb. When you do that, Visual Studio will also change the Name property for the form from Form1 to frmInvoiceTotal and modify any of the code that's been generated for the form accordingly.

You may also want to change the name of the project. Or, you may want to change the name of the solution so it's different from the project name. If so, you can use the technique presented in this figure to do that too.

How to save the files of a project

Figure 2-9 also describes how to save the files of a project. Because Visual Studio saves any changes you make to the files in a project when you build the project, you won't usually need to save them explicitly. However, it's easy to do if you need to.

Notice in this figure that two factors determine which files are saved: what's selected in the Solution Explorer and the command you use to perform the save operation. If, for example, a single file is selected, you can use the Save command to save just that file, and you can use the Save All command to save the file along with the project and solution that contain the file. In contrast, if a project is selected in the Solution Explorer, the Save command causes the entire project to be saved, and the Save All command causes the entire solution to be saved.

If you haven't saved all of your recent changes when you close a project, Visual Studio will ask whether you want to save them. As a result, you don't need to worry that your changes will be lost.

The Solution Explorer as a form file is being renamed

 

How to rename a file, project, or solution

  1. You can rename a file, project, or solution by right-clicking on it in the Solution Explorer window and selecting the Rename command from the shortcut menu. Or, you can select the file, project, or solution in the Solution Explorer and then press F2. Then, you can enter the new name for the file, project, or solution. 
  2. Be sure not to change or omit the file extension when you rename a file. Remember too that using a three-letter prefix to indicate the contents of the file (like frm for a form file) makes it easier to tell what each file represents. 
     
  3. When you change the name of a form file, Visual Studio will also change the Name property for the form and update any references within the existing code for the form, which is usually what you want.

 How to save a file, project, or solution

  • You can use the Save All button in the Standard toolbar or the Save All command in the File menu to save all files and projects in the solution. 
  • You can use the Save button in the Standard toolbar or the Save command in the File menu to save a file, project, or solution. The files that are saved depend on what's selected in the Solution Explorer window. If a single file is selected, just that file is saved. If a project is selected, the entire project and its solution are saved. And if a solution is selected, the entire solution and all its projects are saved. 
     
  • If you try to close a solution that contains modified files, a dialog box is displayed that asks you if you want to save those files.  

 

Figure 2-9.  How to name and save the files of a project


Adding Features to a Windows Forms Application (3 of 5)

From webpage

Adding Features to a Windows Forms Application - The property settings for the Invoice Total form

 
(Page 3 of 5 )

Figure 2-8 shows the property settings for the Invoice Total form. As you can see, you don't need to change many properties to finish the design of this form. You only need to set four properties for the form, and you only use six of the properties (Name, Text, TextAlign, ReadOnly, TabStop, and TabIndex) for the controls. Depending on the order in which you create the controls, though, you may not need to change the TabIndex settings.

Notice that the three text boxes that display the form's calculation have their ReadOnly property set to True. This setting gives the text boxes a shaded appearance, as you saw in figure 2-3, and it prevents the user from entering text into these controls. In addition, the TabStop property for these text boxes has been set to False so the user can't use the Tab key to move the focus onto these controls.

Finally, the settings for the TabIndex properties of the text box and the two buttons are 1, 2, and 3. Since the label controls can't receive the focus, and since the TabStop property for the three read-only text boxes has been set to False, the user can press the Tab key to move the focus from the Subtotal text box to the Calculate button to the Exit button.

In addition, the Subtotal label has a TabIndex property of 0 and a Text property that includes an access key of S. As a result, the user can press Alt+S to move the focus to the control that has the next available tab index. In this case, that control is the Subtotal text box, which has a TabIndex property of 1.

Of course, this is just one way that the TabIndex properties could be set. If, for example, the TabIndex properties for the 10 controls were set from 0 through 9, from top to bottom in this summary, the tab order would work the same.

How to use Document Outline view

Document Outline view is a feature that became available with Visual Studio2005. To open the window for this view, you use the View->Other Windows->Document Outline command. This opens a window to the left of the Form Designer that lists the names of all of the controls that have been added to the current form.

This view makes it easy to check whether you've named all of the controls that you're going to refer to in your Visual Basic code. You can also select a control in the Form Designer by clicking on the name of the control in Document Outline view. Although these are minor benefits, it's worth experimenting with this view to see whether you're going to want to use it.

The property settings for the form  

 

 

Form1

Text

Invoice Total

 

AcceptButton

btnCalculate

 

CancelButton

btnExit

 

StartPosition

CenterScreen

 

 

The property settings for the controls

 

 

Default nameProperty Setting
Label1Text &Subtotal:
TextAlign TabIndex MiddleLeft 0
Label2Text TextAlign Discount percent: MiddleLeft
Label3Text Discount amount:
  TextAlignMiddleLeft
Label4 TextTotal:
  TextAlignMiddleLeft
TextBox1 NametxtSubtotal
  TabIndex1
TextBox2 NametxtDiscountPercent
  ReadOnly TabStopTrue False
TextBox3 NametxtDiscountAmount
  ReadOnly TabStopTrue False
TextBox4 NametxtTotal
  ReadOnly TabStopTrue False
Button1 NamebtnCalculate
  Text&Calculate
  TabIndex2
Button2 NamebtnExit
  TextE&xit
  TabIndex3

 

 

Note

  • To provide an access key for the Subtotal text box, you can set the TabIndex and Text properties for the Subtotal label as shown above. 

Figure 2-8.  The property settings for the Invoice Total form

Adding Features to a Windows Forms Application (2 of 5)

From webpage

Adding Features to a Windows Forms Application - How to add navigation features

 
(Page 2 of 5 )

Windows forms have features that make it easier for users to move around in the forms without using the mouse. These navigation features are described in figure 2-7.

The tab order is the order in which the controls on a form receive the focuswhen the user presses the Tab key. The tab order should usually be set so the focus moves left-to-right and top-to-bottom, beginning at the top left of the form and ending at the bottom right. However, in some cases you'll want to deviate from that order. For example, if you have controls arranged in columns, you may want the tab order to move down each column.

The tab order is initially set based on the order in which you add controls to the form. So if you add the controls in the right order, you won't need to alter the tab order. But if you do need to change the tab order, you can do so by adjusting the TabIndex property settings. The TabIndex property is simply a number that represents the control's position in the tab order, beginning with zero. So, the first control in the tab order has a TabIndex of 0, the second control's TabIndex is 1, and so on.

Incidentally, chapter 10 will show you another way to set the tab order of the controls for a form. You can do that by using Tab Order view. When a form consists of more than a few controls, it is easier to use this view than to set the tab order for one control at a time.

Access keys are shortcut keys that let the user move directly to a control. You set a control's access key by using the Text property. Just precede the letter in the Text property value that you want to use as the access key with an ampersand (&). Then, the user can activate the control by pressing Alt plus the access key.

If you assign an access key to a control that can't receive the focus, such as a label control, pressing the access key causes the focus to move to the next control in the tab order. As a result, you can use an access key with a label control to create a shortcut for a text box control, which can't have an access key.

Finally, you usually should set the AcceptButton and CancelButton properties for a form. These properties specify the buttons that are activated when the user presses the Enter and Esc keys. That can make it easier for a user to work with a form. If, for example, the AcceptButton property of the Invoice Total form in figure 2-3 is set to the Calculate button, the user can press the Enter key after entering a subtotal instead of using the mouse to click the Calculate button.

How to adjust the tab order

  1. Tab order refers to the sequence in which the controls receive the focus when the user presses the Tab key. You should adjust the tab order so the Tab key moves the focus from one control to the next in a logical sequence. 
  2. Each control has a TabIndex property that indicates the control's position in the tab order. You can change this property to change a control's tab order position. 
     
  3. If you don't want a control to receive the focus when the user presses the Tab key, change that control's TabStop property to False. 
     
  4. Label controls don't have a TabStop property so they can't receive the focus.

How to set access keys

  1. Access keys are shortcut keys that the user can use in combination with the Alt key to quickly move to individual controls on the form. 
  2. You use the Text property to set the access key for a control by placing an ampersand immediately before the letter you want to use for the access key. For example, &Invoice sets the access key to I, but I&nvoice sets the access key to n
     
  3. Since the access keys aren't case sensitive, &N and &n set the same access key. 
     
  4. When you set access keys, make sure to use a unique letter for each control. If you don't, the user may have to press the access key two or more times to select a control. 
     
  5. You can't set the access key for a text box. However, if you set an access key for a label that immediately precedes the text box in the tab order, the access key will take the user to the text box.

How to set the Enter and Esc keys

  1. The AcceptButton property of the form sets the button that will be activated if the user presses the Enter key.
  2. The CancelButton property of the form sets the button that will be activated if the user presses the Esc key. This property should usually be set to the Exit button. 
     
  3. You set the AcceptButton or CancelButton values by choosing the button from a drop-down list that shows all of the buttons on the form. So be sure to create and name the buttons you want to use before you attempt to set these values.

Another way to set the tab order

  • In chapter 10, you'll learn how to use Tab Order view to set the tab order of the controls on the form. If the form consists of more than a few controls, that is the best way to set that order.

Figure 2-7.  How to add navigation features

Adding Features to a Windows Forms Application

From webpage

Adding Features to a Windows Forms Application

 
(Page 1 of 5 )

Welcome to the conclusion of a two-part series on designing a Windows Forms application with Visual Studio. In this part we'll learn how to add properties, controls, navigation features, and more. This article is excerpted from chapter two of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456)

Common properties for forms and controls

Figure 2-6 shows some common properties for forms and controls. The first two properties apply to both forms and controls. The other properties are presented in two groups: properties that apply to forms and properties that apply to controls. Note that some of the control properties only apply to certain types of controls. That's because different types of controls have different properties.

Since all forms and controls must have a Name property, Visual Studio creates generic names for all forms and controls, such as Form1 or Button1. Often, though, you should change these generic names to something more meaningful, especially if you're going to refer to them in your Visual Basic code.

To make your program's code easier to read and understand, you can begin each name with a two- or three-letter prefix in lowercase letters to identify the control's type. Then, you can complete the name by describing the function of the control. For instance, you can use a name like btnExit for the Exit button and txtSubtotal for the Subtotal text box.

For Label controls, you can leave the generic names unchanged unless you plan on modifying the properties of the labels in your code. For example, if you want to use a label control to display a message to the user, you can give that label a meaningful name such as lblMessage. But there's no reason to change the names for label controls that display text that won't be changed by the program.

Forms and most controls also have a Text property that is visible when the form is displayed. A form's Text property is displayed in the form's title bar. For a control, the Text property is usually displayed somewhere within the control. The Text property of a button, for example, is displayed on the button, and the Text property of a text box is displayed in the text box.

As you work with properties, you'll find that you can set some of them by selecting a value from a drop-down list. For example, you can select a True or False value for the TabStop property of a control. For other properties, you have to enter a number or text value. And for some properties, a button with an ellipsis (...) is displayed. Then, when you click this button, a dialog box appears that lets you set the property.

The Name property

  1. Sets the name you use to identify a control in your Visual Basic code. 
  2. Can be changed to provide a more descriptive and memorable name for forms and controls that you will refer to when you write your code (such as text boxes and buttons). 
     
  3. Doesn't need to be changed for controls that you won't refer to when you write your code (such as most labels). 
     
  4. Can use a three-letter prefix to indicate whether the name refers to a form (frm), button (btn), label (lbl), or text box (txt).

The Text property

  • Sets the text that's displayed on the form or control. Some controls such as forms and labels display the generic form or control name that's generated by Visual Studio, which you'll almost always want to change. 
  • For a form, the Text value is displayed in the title bar. For controls, the Text value is displayed directly on the control. 
     
  • For a text box, the Text value changes when the user types text into the control, and you can write code that uses the Text property to get the text that was entered by the user.  

Other properties for forms  

 

 

Property

Description

AcceptButton

Identifies the button that will be activated when the user presses the Enter key.

CancelButton

Identifies the button that will be activated when the user presses the Esc key.

StartPosition

Sets the position at which the form is displayed. To center the form, set this property to CenterScreen.

 

 

Other properties for controls  

 

 

Property

Description

Enabled

Determines whether the control will be enabled or disabled.

ReadOnly

Determines whether the text in some controls like text boxes can be edited.

TabIndex

Indicates the control's position in the tab order, which determines the order in which the controls will receive the focus when the user presses the Tab key.

TabStop

Determines whether the control will accept the focus when the user presses the Tab key to move from one control to another. Some controls, like labels, don't have the TabStop property because they can't receive the focus.

TextAlign

Sets the alignment for the text displayed on a control.

 

 

 

Figure 2-6. Common properties for forms and controls

How to Code and Test a Windows Forms Application (4 of 4)

Got this from this webpage

How to Code and Test a Windows Forms Application - How to add code to a form

 
(Page 4 of 4 )

Now that you understand some of the concepts behind object-oriented coding, you're ready to learn how to add code to a form. Because you'll learn the essentials of the Visual Basic language in the chapters that follow, though, I won't focus on the coding details right now. Instead, I'll focus on the concepts and mechanics of adding the code to a form.

How to create an event handler for the default event of a form or control

Although you can create an event handler for any event of any object, you're most likely to create event handlers for the default event of a form or control. So that's what you'll learn to do in this chapter. Then, in chapter 6, you'll learn how to create event handlers for other events.

To create an event handler for the default event of a form or control, you double-click the object in the Form Designer. When you do that, Visual Studioopens the Code Editor, generates a procedure declaration for the default event of the object, and places the insertion point between the Sub and End Sub statements that it has generated. Then, you can enter the Visual Basic statements for the procedure between the Sub and End Sub statements.

To illustrate, figure 3-4 shows the Sub and End Sub statements that were generated when I double-clicked the Calculate button on the Invoice Total form. In the Sub statement, Visual Studio generated a procedure name that consists of the name of the object that the event occurred on (btnCalculate), an underscore, and the name of the event (Click).

This procedure name is followed by two arguments in parentheses that you'll learn more about later. And the arguments are followed by a Handles clause that says that the procedure is designed to handle the Click event of the button named btnCalculate. It is this clause, not the procedure name, that determines what event the procedure handles.

For now, you should avoid modifying the procedure declaration that's generated for you when you create an event handler. In chapter 6, though, you'll learn how to modify the declaration so a single procedure can provide for more than one event.

The procedure that handles the Click event of the Calculate button

How to handle the Click event of a button

  1. In the Form Designer, double-click the control. This opens the Code Editor, generates the declaration for the procedure that handles the event, and places the cursor within this declaration. 
  2. Type the Visual Basic code between the Sub statement and the End Sub statement. 
     
  3. When you finish entering the code, you can return to the Form Designer by clicking the View Designer button in the Solution Explorer window.

How to handle the Load event for a form

  • Follow the procedure shown above, but double-click the form itself.

Description

  1. The procedure declaration for the event handler that's generated when you double-click on an object in the Form Designer includes a procedure name that consists of the object name, an underscore, and the event name. 
  2. The Handles clause in the procedure declaration determines what event the procedure handles using the object name, dot, event name syntax. 
     
  3. In chapter 6, you'll learn how to handle events other than the default event. 

Figure 3-4.   How to create an event handler for the default event of a form or control

 

  • Please check back tomorrow for the continuation of this article.

     

  • How to Code and Test a Windows Forms Application (3 of 4)

    Got this from this webpage

    How to Code and Test a Windows Forms Application - How an application responds to events

     
    (Page 3 of 4 )

    Windows Forms applications are event-driven. That means they work by responding to the events that occur on objects. To respond to an event, you code a procedure known as an event handler. In figure 3-3, you can see the event handler for the event that occurs when the user clicks the Exit button on the Invoice Total form. In this case, this event handler contains a single statement that uses the Close method to close the form.

    This figure also lists some common events for controls and forms. One control event you'll respond to frequently is the Click event. This event occurs when the user clicks an object with the mouse. Similarly, the DoubleClick event occurs when the user double-clicks an object.

    Although the Click and DoubleClick events are started by user actions, that's not always the case. For instance, the Enter and Leave events typically occur when the user moves the focus to or from a control, but they can also occur when code moves the focus to or from a control. Similarly, the Load event of a form occurs when a form is loaded into memory. For the first form of an application, this typically happens when the user starts the application. And the Closed event occurs when a form is closed. For the Invoice Total form in this figure, this happens when the user selects the Exit button or the Close button in the upper right corner of the form.

    In addition to the events shown here, most objects have many more events that the application can respond to. For example, events occur when the user positions the mouse over an object or when the user presses or releases a key. However, you don't typically respond to those events.

    Event: The user clicks the Exit button

    Response: The procedure for the Click event of the Exit button is executed

      Private Sub btnExit_Click(ByVal sender As System.Object,
              ByVal e As System.EventArgs) Handles btnExit.Click
          Me.Close()
      End Sub

    Common control events  

     

    Event

    Occurs when…

    Click

    …the user clicks the control.

    DoubleClick

    …the user double-clicks the control.

    Enter

    …the focus is moved to the control.

    Leave

    …the focus is moved from the control.

     

     

    Common form events  

     

    Event

    Occurs when…

    Load

    …the form is loaded into memory.

    Closing

    …the form is closing.

    Closed

    …the form is closed.

     

    Concepts

    1. Windows Forms applications work by responding to events that occur on objects. 
    2. To indicate how an application should respond to an event, you code an event handler, which is a Visual Basic procedure that handles the event. 
       
    3. An event can be an action that's initiated by the user like the Click event, or it can be an action initiated by program code like the Closed event. 

    Figure 3-3.   How an application responds to events

    How to Code and Test a Windows Forms Application (2 of 4)

    Got this from this webpage

    How to Code and Test a Windows Forms Application - How to refer to properties, methods, and events

     
    (Page 2 of 4 )

    As you enter the code for a form in the Code Editor window, you often need to refer to the properties, methods, and events of its objects. To do that, you type the name of the object, a period (also known as a dot operator, or dot), and the name of the member. This is summarized in figure 3-2.

    In some cases, you will refer to the properties and methods of a class instead of an object that's instantiated from the class. You'll see examples of that in later chapters. For now, you just need to realize that you refer to these properties and methods using the same general syntax that you use to refer to the properties and methods of an object. You enter the class name, a dot, and the property or method name.

    To make it easier for you to refer to the members of an object or class, VisualStudio's IntelliSense feature displays a list of the members that are available for that class or object after you type a class or object name and a period. Then, you can highlight the entry you want by clicking on it, typing one or more letters of its name, or using the arrow keys to scroll through the list. In most cases, you can then complete the entry by pressing the Tab or Enter key.

    To give you an idea of how properties, methods, and events are used in code, this figure shows examples of each. In the first example for properties, code is used to set the value that's displayed for a text box to 10. In the second example, code is used to set the ReadOnly property of a text box to True. Although you can also use the Properties window to set these values, that just sets the properties at the start of the application. By using code, you can change the properties as an application is running.

    In the first example for methods, the Select method of a text box is used to move the focus to that text box. In the second example, the Close method of a form is used to close the active form. In this example, the Me keyword is used instead of the name of the form. Here, Me refers to the current instance of the active form. Note also that the names of the methods are followed by parentheses. If a method requires parentheses like these, they're added automatically when you press the Enter key after entering the method name.

    As you progress through this book, you'll learn how to use the methods for many types of objects, and you'll learn how to supply arguments within the parentheses of a method. For now, though, just try to understand that you can call a method from a class or an object.

    Although you'll frequently refer to properties and methods as you code an application, you'll rarely need to refer to an event. That's because Visual Studio automatically generates the code for working with events, as you'll see later in this chapter. To help you understand the code that Visual Studio generates, however, the last example in this figure shows how you refer to an event. In this case, the code refers to the Click event of a button named btnExit.

    A member list that's displayed in the Code Editor window

    The syntax for referring to a member of a class or object

      ClassName.MemberName
      objectName.MemberName

    Statements that refer to properties

     

    txtTotal.Text = 10 Assigns the value 10 to the Text property of the text box named txtTotal.  
    txtTotal.ReadOnly = True Assigns the True value to the ReadOnly property of the text box named txtTotal so the user can't change its contents.
       

     

    Statements that refer to methods  

     

    txtMonthlyInvestment.Select()

    Uses the Select method to move the focus to the text box named txtMonthlyInvestment.

    Me.Close()

    Uses the Close method to close the form that contains the statement. In this example, Me is a keyword that is used to refer to the current instance of the form class.

     

     

    Code that refers to an event  

     

    btnExit.Click

    Refers to the Click event of a button named btnExit.

     

     

    How to enter member names when working in the Code Editor

    1. To display a list of the available members for a class or an object, type the class or object name followed by a period (called a dot operator, or just dot). Then, type one or more letters of the member name, and Visual Studio will filter the list so that only the members that start with those letters are displayed. You can also scroll through the list to select the member you want. 
    2. Once you've selected a member, you can press the Tab key to insert it into your code, or you can press the Enter key to insert the member and start a new line of code. 
       
    3. By default, all the available members are displayed in the list. To display just the common members, click the Common tab at the bottom of the list. 
       
    4. If a member list isn't displayed, select the Tools->Options command to display the Options dialog box. Then, expand the Text Editor group, select the Basic group, and check the Auto List Members and Parameter Information boxes. 

    Figure 3-2.  How to refer to properties, methods, and events

    How to code and Test a Windows Forms Application

    Got this from webpage

    How to Code and Test a Windows Forms Application

     
    (Page 1 of 4 )

    Yesterday we finished an article series that showed you how to design a user interface for a Windows Forms application. Today, we'll kick off a five-part article series that shows you how to bring that application to life. This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456).

    In the last chapter, you learned how to design a form for a Windows Forms application. In this chapter, you'll learn how to code and test a Windows Forms application. Here, the emphasis will be on the Visual Studio skills that you need for entering, editing, and testing the Visual Basic code for your applications. You'll learn how to write that code in the rest of this book.

    An introduction to coding

    Before you learn the mechanics of adding code to a form, it's important to understand some of the concepts behind object-oriented programming.

    Introduction to object-oriented programming

    Whether you know it or not, you are using object-oriented programming as you design a Windows form with Visual Studio's Form Designer. That's because each control on a form is an object, and the form itself is an object. These objects are derived from classes that are part of the .NET Class Library.

    When you start a new project from the Windows Application template, you are actually creating a new class that inherits the characteristics of the Form class that's part of the .NET Class Library. Later, when you run the form, you are actually creating an instance of your form class, and this instance is known as an object.

    Similarly, when you add a control to a form, you are actually adding a control object to the form. Each control is an instance of a specific class. For example, a text box control is an object that is an instance of the TextBox class. Similarly, a label control is an object that is an instance of the Label class. This process of creating an object from a class can be called instantiation.

    As you progress through this book, you will learn much more about classes and objects because Visual Basic is an object-oriented language. In chapter 11, for example, you'll learn how to use the Visual Basic language to create your own classes. At that point, you'll start to understand what's actually happening as you work with classes and objects. For now, though, you just need to get comfortable with the terms and accept the fact that a lot is going on behind the scenes as you design a form and its controls.

    Figure 3-1 summarizes what I've just said about classes and objects. It also introduces you to the properties, methods, and events that are defined by classes and used by objects. As you've already seen, the properties of an object define the object's characteristics and data. For instance, the Name property gives a name to a control, and the Text property determines the text that is displayed within the control. In contrast, the methods of an object determine the operations that can be performed by the object.

    An object's events are signals sent by the object to your application that something has happened that can be responded to. For example, a Button control object generates an event called Click if the user clicks the button. Then, your application can respond by running a Visual Basic procedure to handle the Click event.

    By the way, the properties, methods, and events of an object or class are called the members of the object or class. You'll learn more about properties, methods, and events in the next three figures.

    A form object and its ten control objects

    Class and object concepts

    1. An object is a self-contained unit that combines code and data. Two examples of objects you have already worked with are forms and controls.
    2. class is the code that defines the characteristics of an object. You can think of a class as a template for an object. 
       
    3. An object is an instance of a class, and the process of creating an object from a class is called instantiation
       
    4. More than one object instance can be created from a single class. For example, a form can have several button objects, all instantiated from the same Button class. Each is a separate object, but all share the characteristics of the Button class.

    Property, method, and event concepts

    1. Properties define the characteristics of an object and the data associated with an object.
    2. Methods are the operations that an object can perform. 
       
    3. Events are signals sent by an object to the application telling it that something has happened that can be responded to. 
       
    4. Properties, methods, and events can be referred to as members of an object. 
       
    5. If you instantiate two or more instances of the same class, all of the objects have the same properties, methods, and events. However, the values assigned to the properties can vary from one instance to another.

    Objects and forms

    • When you use the Form Designer, Visual Studio automatically generates Visual Basic code that creates a new class based on the Form class. Then, when you run the project, a form object is instantiated from the new class. 
    • When you add a control to a form, Visual Studio automatically generates Visual Basic code in the class for the form that instantiates a control object from the appropriate class and sets the control's default properties. When you move and size a control, Visual Studio automatically sets the properties that specify the location and size of the control.