Part 2 Saving the Dictionary

Slides:



Advertisements
Similar presentations
Chapter 11 Data Files Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Advertisements

Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
File dialog boxes Function args and overloading. Examples in this show List directory contents Open a file for viewing Read/Write to a file Add file I/O.
More on lists, exceptions, loops and validation. You can use the exception to indicate the error that occurred Private Sub btnCheck_Click(ByVal sender.
110-C1 Chapter 2 of the text: _ text boxes, group boxes, check boxes, radio buttons, picture boxes _ defining access keys tab sequence setting the focus.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic 2008 Express Edition The IDE. Visual Basic 2008 Express The Start Page Recent Projects Open an existing project Create a New Project.
Printer & Dialog boxes Engr. Faisal ur Rehman CE-105T Spring 2007.
File Handling. Data Files Programs that can only get data from the user interface are limited. –data must be entered each time. –only small amounts of.
Multiple Forms, Standard Modules, And Menus
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
File I/O 11_file_processing.ppt
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
Tutorial 6 The Repetition Structure
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
1.
1 Advanced Computer Programming Lab Calculator Project.
CS 2340: Programming in VB Lab 2 Due 9 pm, Friday, September 14 1.
Lab 2 Due 9 pm, Friday, September Put a “&” in button text The char after “&” is underlined and becomes the Access Key Example Text of btnExit:
PRACTICE SAC 1 Some solutions. 'inputs 'sale price 'postcode 'coupon codes 'processing 'calculate commissions (commission based on cost) 'calculate postage.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
6b – Sub Procedure With Parameters Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
1.NET Web Forms Applications: Main Form © 2002 by Jerry Post.
Practical Programming COMP153-08S Lecture: Repetition.
Visual Basic. The Close Method The Close method is used to close a form. To close a form use the keyword Me to refer to the form. Me.Close()
Introduction to VB programming Dr. John P. Abraham UTPA Chapters 2 & 3.
McGraw-Hill © 2010 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 11 Data Files.
Files and Streams. What is a file? Up until now, any stored data within a program is lost when the program closes. A file is a permanent way to store.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 3 Variables and Calculations.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Computer Science Up Down Controls, Decisions and Random Numbers.
1 Displaying Dialog Boxes Kashef Mughal. 2 Midterm Stats Here we go  Average was  Low was 116  High was 184  Mid Quarter Grade - check any.
Use TryParse to Validate User Input
Visual Basic Fundamental Concepts
Apply Procedures to Develop Message, Input, and Dialog Boxes
File Handling.
Use TryParse to Validate User Input
Using GUI Objects and the Visual Studio IDE
Introduction to VB programming
Files and Streams.
للمزيد زورونا على موقعنا الإلكتروني:
Visual Basic..
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
String Variable, Methods and Properties
String Variable, Methods and Properties
Multi-Form Applications Things You Need to Know
Fundamentals of Programming in VB.Net
Part 3 Saving (without the Save button)
Dictionary Builder Part 4 Loading Files.
String Variable, Methods and Properties
Dictionary Builder Part 1 Getting Started.
String Variable, Methods and Properties
Introduction to VB programming
Files and Streams.
Chapter 11 Saving Data and Objects In Files
Presentation transcript:

Part 2 Saving the Dictionary Dictionary Builder Part 2 Saving the Dictionary

Step 3 – Saving the Dictionary At first glance this seems like simply programming btnSave, but a closer look is required…

Step 3 – Saving the Dictionary At first glance this seems like simply programming btnSave, but a closer look is required… The file might also be saved when the user clicks btnExit, or btnExtractWords, or btnOpenDictionary.

Step 3 – Saving the Dictionary It seems sensible, then, to create a sub-program to perform all the operations in saving the file. Then we can call it when needed.

Step 3 – Saving the Dictionary Private Sub saveDictionary() ' Declare the FileStream and StreamWriter object variables ' Set any properties of the SaveFileDialog and show the dialog window. ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed As DialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed As DialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, ' write the contents ' and close the objects. End If End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() ' Declare the FileStream and StreamWriter object variables Dim theFile As IO.FileStream, writer As IO.StreamWriter ' Set any properties of the SaveFileDialog and show the dialog window. dlgSaveFile.InitialDirectory = "…" Dim btnPressed As DialogResult btnPressed = dlgSaveFile.ShowDialog() ' If the user presses the Save button If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents ' and close the objects. End If End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() Dim theFile As IO.FileStream, writer As IO.StreamWriter dlgSaveFile.InitialDirectory = "…" Dim btnPressed As DialogResult btnPressed = dlgSaveFile.ShowDialog() If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents ' and close the objects. writer.Close() theFile.Close() End If End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() … If btnPressed = Windows.Forms.DialogResult.OK Then ' Create the FileStream and StreamWriter objects, theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) ' write the contents Dim indx As Short For indx = 0 To lstWords.Items.Count - 1 writer.WriteLine(lstWords.Items.Item(indx)) Next ' and close the objects. writer.Close() theFile.Close() End If End Sub

Step 3 – Saving the Dictionary Private Sub saveDictionary() … If btnPressed = Windows.Forms.DialogResult.OK Then theFile = New IO.FileStream(dlgSaveFile.FileName, _ IO.FileMode.Create, IO.FileAccess.Write) writer = New IO.StreamWriter(theFile) Dim indx As Short For indx = 0 To lstWords.Items.Count - 1 writer.WriteLine(lstWords.Items.Item(indx)) Next writer.Close() theFile.Close() ' set the flag for other Subs isSaved = True ** End If End Sub

Step 3a - isSaved The identifier isSaved is a flag. It will be set and tested by several different parts of the program. It should be declared Globally. Private isSaved As Boolean

Step 3a - isSaved It should also be intialised… Public Sub New() ' This call is required by the designer. InitializeComponent() ' Add any initialization after the ' InitializeComponent() call. isSaved = True AcceptButton = btnAddWord End Sub

Step 4 – btnSave Private Sub btnSave_Click(ByVal sender As System.Object,_ ByVal e As System.EventArgs) Handles btnSave.Click If Not isSaved Then saveDictionary() Else MessageBox.Show("There's nothing to Save!") End If End Sub

Step 5 - btnExit When the User clicks Exit, the program should check to see if the contents of lstWords have been saved. If not, it should provide the opportunity.

Step 5 - btnExit Dim btnPressed As DialogResult If isSaved Then End Else btnPressed = MessageBox.Show("The text has not been saved." & _ vbNewLine & "Save text?", "Save text?", _ MessageBoxButtons.YesNoCancel) If btnPressed = Windows.Forms.DialogResult.Yes Then ' Save the text to a file. saveDictionary() ElseIf btnPressed = Windows.Forms.DialogResult.No Then End If

Step 5a – Testing isSaved It is common that the same test could be required in different contexts. For example, the code for both buttons that read files will need to check isSaved and generate the same MessageBox that was used in btnExit.

Step 5 - btnExit It makes sense to handle these in a sub-program that can be called when needed. Private Sub btnExit_Click(…) Handles btnExit.Click If good2go(sender) Then saveDictionary() End If End End Sub