1 / 36

Chapter 6

Chapter 6. Looping and Multiple Forms. Adding Icons to your form. It’s possible to add an icon to your title bar by setting the form’s icon property .

hermanm
Download Presentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Looping andMultiple Forms

  2. Adding Icons to your form • It’s possible to add aniconto your title bar by setting the form’s icon property. • You can use the NUMEROUS icons that come with Studio.Net. Of course, if you don’t find any that you like you can always create your own or find some online. • I’ve moved a copy of the icon files that come with Visual Studio.Net into our class network space for your convenience.

  3. Properties of a ListView Control • View Property – Determines how items display in the ListView Control. Switch to Details if you would like to display multiple columns. • Columns Property – used to add and edit the Columns in a ListView Control. • You can change the Text Property to give each column its own heading. • Other settings, like Width, can also be adjusted if necessary. • MultiSelect Property – specifies whether the user can select multiple items in the list. • CheckBoxes Property – determines if a check box appears with each item in the list, so the user can select multiple items.

  4. Anchoring Controls • In previous applications we’ve developed the windows used were not resizable during run time. • When a form is resized, controls on the form must be moved or resized as well in order to maintain a usable and balanced user-interface. • Rather than having to code changes for the width, height, and location of every control, VB.NET allows you to set a fixed location, or ANCHOR, for controls to specified sides of a form. • For example, in the Today’s Sales Check program the buttons were anchored to the bottom of the form, the List View control was anchored to all four sides of the form.

  5. Adding a New Form to a Project • Most .NET applications contain more than one form… that’s why you should learn how to add new ones to your project, eh?  • There are many ways to do it. Here are a couple: • You can right-click the on the project name in the Solution Explorer window and then point to Add on the shortcut menu. • You can go up to the Project Menu and click on the menu item for adding a new Windows form etc… • If the first form is used to launch the second form it is sometimes referred to as the parent form. • You can change the StartupPosition Property on the new form to CenterParent, meaning that the new form will display in the center of the first form when its launched.

  6. CheckBox Control • The CheckBox Control is used in applications to allow a user to specify one of two conditions, such as on or off, yes or no, or true or false. • In the Today’s Sales Check program the checkbox control was used to input whether an item was on sale or not. • Some important CheckBox Properties: • Checked – determines whether the box is checked or not. It’s either True or False. • Text – allows the programmer to put text next to the CheckBox explaining its purpose. • CheckAlign – Determines the location of the check box inside of the control. (right of text, left of text etc..)

  7. Declaring an Object Using an Object Variable • So you have your main form … Today’s Sales Check and the user clicks on the button to Enter Today’s Sales. You want to launch the second form from within the first form. • Well, as is the case with all forms, the Today’s Sales Entry Form is a class, which means that an instance of the class must be declared before it can be used. This can be done with an object variable. • Use the Dim statement and the obj prefix. For example: • Dim objAddItemForm as New frmAddItem

  8. Declaring an Object & Showing a Form • The New Keyword determines whether the variable contains a complete instance of the object. • If the New keyword is not used, then the variable is declared for future use but no memory is set aside for the contents of the object. • If the New Keyword is used, the object is said to be instantiated and memory is set aside for a complete copy of the object. This means the form will be modal (You can’t use a new form until this is done). • FYI: This does not need to be done for the startup form for a project… .NET does that for you! • In order to get the form up on the screen you need to call it from memory. One way to do this is with the Show() or ShowDialog() method.

  9. Show() and ShowDialog() Methods • If the Show() Method is used in a form’s code to open another form, then both forms still can receive focus and be accessed by the user. • This is called a modeless form – meaning that the user can flip back and forth between the forms. • The ShowDialog() Method is used to open a modal form. • This means that the user must complete the action on the modal form before they will be allowed to continue work on other forms.

  10. Repetition and the Do Statement • One of the most powerful aspects of computer programming is the capability of performing a set of operations repeatedly based on certain conditions. • In programming, the process of repeating a set of instructions is known as looping, or repetition. • There are four basic types of loops, two of which contain the decision to terminate the loop at the top of the control structure and two which contain the decision to terminate the loop at the top of the control structure.

  11. Looping and the Do Statement

  12. The Do While Statement • A common form of the Do statement is the Do…While. • This statement repeatedly executes a series of statements in the loop as long as the condition is true. • Two examples: (one tests at the top of the structure, one tests at the bottom of the structure) Do While intCount <=20 Do intCount = intCount +1 intCount = intCount +1 Loop Loop While intCount <=20

  13. Do Until Statements • Another common form of the Do statement is the Do…Until. • This statement loops until the condition becomes true. • Two examples: (one tests at the top of the structure, one tests at the bottom of the structure) Do Until intCount > 20 Do intCount = intCount +1 intCount = intCount +1 Loop Loop Until intCount > 20

  14. Selecting the Proper Do Statement • Your program’s logic should indicate which type of Do Loop to use. (Draw a flowchart if you are having trouble picturing it!) • If the decision to terminate is at the top of the loop, use a Do While or Do Until. • If the decision to terminate is at the bottom of the loop, use a Loop While or A Loop Until. • The loops are interchangeable as long as you make the code match the logic! • An example from our main project: Do Until objAddItemForm.txtItemDescription.Text = “” Loop

  15. Working with Collections in ListView Controls • A collection is a group of one or more objects that can be accessed and operated on as a single entity. • You have a little bit of experience with collections. • In Project 5 you used a collection to add items to your ComboBox. This is using a collection as a property of a control. • As you learned in Project 5, .NET assigns an unique index number to each item in a collection. This will come in handy when working with a ListView Control. • Before adding items to the ListView Control you must: • Create an object to hold the item • Then you must put the user’s input into that object. • Then you can add that object to the ListView Control • Finally, you can redisplay the Input Form

  16. Adding Items to a ListView Control • Text Property – this property always displays as the first column in the ListView Control. • SubItems Property – If you have more than one column in a ListView control, a SubItems collection is created. • Each Item and SubItem has an index property that refers to which column in the ListView Control it occupies.

  17. Adding Items to a ListView Control • For Example lstTodaysSales.Items(0).Text = _______________ lstTodaysSales.Items(0).SubItems(1).Text = ________

  18. Adding Items to a ListView Control • Here is the code from the main project. Let’s see if we can break down this mess! 

  19. For…Next Statement • For … Next Loops are different from the Do…Until Loop in that it has an automatic counter and condition built in. • This makes them perfect for counter-controlled loops – these are loops where you set up a variable before the loop starts, then increment the variable within the loop, and then test to see if it has reached the maximum count that you’re aiming for. • Benefits of For … Next Loops: • More efficient than a Do While Loop • Easier to read • Uses less memory • Faster execution

  20. For intCount = 1 to 100 intSum = IntSum + intCount Next MessageBox.Show(“The sum is “ & IntSum) For intCount = 93 to 3 Step -3 txtCount.Text = intCount Next For intCount = 2 to 100 Step 2 txtCount.Text = intCount Next Dim dblTotal as Double = 0 For intScores = 1 to 24 dblTotal = dblTotal + dblScore Next Examples of For…Next Loops

  21. Exiting a Loop Prematurely – The Exit Statement • It is possible to exit a loop early if necessary. Just use the Exit Statement. • Examples are below: • Exit For • Exit Do

  22. Nested For…Next Loops • When the statements of one For…Next Loop lie within the range of another For…Next Loop, the loops are said to be nested, or embedded. • An example can be seen below:

  23. Implementing a Loop Using a For…Next Statement

  24. Implementing a Loop Using a For…Next Statement • We talked about For…Next Loops being great for controlled-count loops. Here is a perfect example from the btnTotalSales in the Chapter 6 Main Project. This button is used to add to the total for items on sale and items that weren’t on sale. • Line 160 sets the intListCount variable = to the number of items in the ListView Control. The Count Property of the list box knows exactly how many items are in the control. So use it! • Line 161 and 163 set up the loop. Obviously we’ll have to add code between the For and the Next! 

  25. Accessing Items in the ListView Control so you can use them! • We’re still looking at the btnTotalSales loop … • In order to add up the sales we need to get the information from that particular column of the ListView Control. How did they do that? • The line of code might look like this: strItemSales = lstTodaysSales.Items(intIndex).SubItems(1).Text • Breaking down this code: • We need to type in the name of the ListView Control • Go to the particular items property index # that we are currently working on • Then access the actual Sales Total from the SubItems column • We’re saving the results of this into StrItemSales since text properties are strings.

  26. String Manipulation • The SubString Method allows you to retrieve part of a string (from a larger string) based on a start position and the number of characters to retrieve. • For example: strExample = “Christy Garrett” strLastName = strExample.Substring ( ___ , ___) • There are a ton of other ways to manipulate strings in .Net. They are listed on page 6.63 in your text book. • For example: In the main project we had $’s in the Total Sales column of the ListView Control. Those dollar signs would’ve messed up our ability to total up those sales figures. • We replaced those $ with 0’s in the code so we could add them up by using the Replace Method:

  27. String Manipulation

  28. String Manipulation

  29. Concatenation Operators • We learned how to concatenate strings in Chapter Four. • Concatenation Operators are another form of concatenation. It is used to save you time when you are typing up expressions. • For example: intCount = intCount + 1 can be written as intcount +=1

  30. Coding with Concatenation Operators • Below is an example with Concatenation Operators. • You can also see the use of the ControlChars.NewLine Constant. This is used to start a new line within a string.

  31. Removing Items from a ListView Control • It is very easy to clear an entire list box. Just use the Clear() Method. • For example, this code was found inside the btnClearList click event: lstTodaysSales.Items.Clear() • Other methods such as Remove() or Remove At() can be used to remove specific items from the list.

  32. Keyboard Events • This program wanted the user to be able to delete any item from the ListView Control simply by clicking on the item and then pressing the Delete key. • In order to do that you would have to set up a KeyBoard event for the ListView Control. This is very simple to do! • Most controls include three event procedures for handling user input from the keyboard. • The KeyDown event, the KeyPress event, and the KeyUp event occur when the user presses a key when the control has focus. • The code below is an example of using a KeyDown event and the RemoveAt() method to remove a selected item from the list.

  33. Coding a Form Resize Event • The Resize() event occurs when a form or control first displays or when the size changes because the user resizes the form. • Code in the Resize event is often used to ensure that the user interface remains polished and usable during run time, even after the user resizes the form. • Since the Resize event isn’t the default event for the form, you must switch to this event before you can write the code. • Let’s look at the code for the Resize event and make sure you understand it!

  34. The Me Keyword and Coding a Second Form • You’ll notice the use of the Me Keyword in this and other sections of code in Project 6. • This is another brilliant shortcut from the people at Microsoft. When coding a form, you’ll often find the need to refer to the current form. • The Me Keyword allows you to reference the form’s name without having to type it out each time. Me is whichever form you’re currently on. • For example: • Me.Close() • intColumnWidth = Me.Width – lstScores.Columns(1)Width - 12

  35. Setting the Startup Object for a Project • As you have seen, when you start a new Windows project in VB.NET it creates a form called Form1. • By default, the first form you create becomes the StartUp form. • When working with multiple forms or when you’ve renamed a form, you must tell Visual Basic which form to display first when you hit run. • This can be done from the Properties Pages. • There is more than one way to get to this screen. Make sure you know one of them!

  36. Chapter 6 Complete

More Related