1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.

Slides:



Advertisements
Similar presentations
VB List(Of type) A List can be thought of as an array that automatically adjusts its size as elements are added and removed A List may hold only objects.
Advertisements

Alford Academy Business Education and Computing1 Advanced Higher Computing Based on Heriot-Watt University Scholar Materials File Handling.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 06.
Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.
Modules Standard Modules. Standard Modules The Subs and Functions worked perfectly well where they were – inside the two lines “Public Class Form1” and.
VB – Core III Functions Sub-routines Parameter passing Modules Scope Lifetime.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
Chapter 9 Files I/O: Files, Records and Fields. Basics of File Input and Output Have created both input and outputs from programs. Persistent data: What.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Subroutines and Functions Chapter 6. Introduction So far, most of the code has been inside a single method for an event –Fine for small programs, but.
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.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Using Arrays and File Handling
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
CSCI 3327 Visual Basic Chapter 6: Methods: A Deeper Look UTPA – Fall 2011.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
PROGRAMMING Functions. Objectives Understand the importance of modular programming. Know the role of functions within programming. Use functions within.
Let’s get started using Visual Basic!. Private Sub cmdGo_Click... Dim strMessage As String Dim sngSum As Single If IsNumeric(txtNumber1.Text) = False.
File I/O 11_file_processing.ppt
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
File I/O Static void Main( ) {... } data. Topics I/O Streams Reading and Writing Text Files Formatting Text Files Handling Stream Errors File Pointers.
Programming Test #1 Solutions. Multiple Choice 1. B) the grammar of the coding language 2. C) String 3. A) Single 4. C) 2Burgers4Me 5. B) Design Time.
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To.
11-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
Reference: Lecturer Lecturer Reham O. Al-Abdul Jabba lectures for cap211 Files and Streams- I.
1 COMP3100e Developing Microsoft.Net Applications for Windows (Visual Basic.Net) Class 6 COMP3100E.
File I/O What We’ll Cover –Visual Basic Techniques for Text Files –.NET Techniques for Text Files What We’ll Not Cover –Binary File I/O –XML File I/O.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
Tutorial 9: Sequential Access Files and Printing1 Tutorial 9 Sequential Access Files and Printing.
INPUT AND OUTPUT 1. OUTPUT TextBoxes & Labels MsgBox Function/Method MessageBox Class Files (next topic) Printing (later) 2.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
1 Advanced Computer Programming Lab Calculator Project.
Copyright © 2012 Pearson Education, Inc. Chapter 5 Loops, File, and Random Numbers.
6-1 Chapter 6 Working with Arrays in VB.NET. 6-2 Learning Objectives Understand the use of list and table arrays in VB.NET projects and the difference.
6b – Sub Procedure With Parameters Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
BACS 287 File-Based Programming. BACS 287 Data Hierarchy  Database - Collection of files, relationships, integrity information, etc  Files - All records.
31/01/ Selection If selection construct.
Calculator Program Explained by Arafa Hamed. First Designing The Interface Ask yourself how many places are there that will be used to input numbers?
مقدمة في البرمجة Lecture 7. Write VB.net project using the for loop to calculate : 1- the sum of numbers from 1 to (A) numbers. 2- the sum of Odd numbers.
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.
Files and Streams. Objectives Learn about the classes that support file input/output Understand the concept of abstraction and how it related to the file.
Subroutines and Functions Chapter 6. Introduction So far, all of the code you have written has been inside a single procedure. –Fine for small programs,
Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.
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.
Lecture 7 Methods (functions and subroutines) Parameter Passing
Visual Basic I Programming
Visual Basic Fundamental Concepts
INPUT AND OUTPUT.
Reading & writing to files
Files and Streams.
Files and Streams Lect3 CT1411.
Visual Basic..
Chapter 3.5 Input and Output
CIS16 Application Development and Programming using Visual Basic.net
Files and Streams Lect10 GC201 12/1/2015.
Dictionary Builder Part 4 Loading Files.
Chapter 8 - Functions and Functionality
To understand what arrays are and how to use them
Introduction to Programming Lecture 6
Introduction to VB programming
Files and Streams.
Presentation transcript:

1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης

2 Text Files and VB.NET There is a very useful object in VB.NET called System.IO (the IO stands for Input and Output). You can use this object to read and write to text files.

3 How to Open a Text File in VB.NET To open up a text file, you need to create something called a "StreamReader". This, as its name suggests, reads streams of text. The StreamReader is an object available to System.IO. You create a StreamReader like this: Dim FILE_NAME As String = "C:\test.txt" Dim objReader As System.IO.StreamReader objReader = New System.IO.StreamReader(FILE_NAME)

4 Read To End But this won't do you any good. We haven't actually opened the text file yet. We've just told VB where the text file is and what object to open it with. You do the opening like this: TextBox1.Text = objReader.ReadToEnd Now that objReader is an object variable, it has its own properties and methods available for use (in the same way that the textbox has a Text property). One of the Methods available to our new StreamReader variable is the ReadToEnd method. This will read the whole of your text, right to the end.

5 Read To End Start a new project Add a textbox to your new form, and just leave it on the default Name of Textbox1 Set its MultiLine property to True Add a Button to your form. Set the Text property of the button to “Read from File". Double click the button and add the following code for it: Dim FILE_NAME As String = "C:\test.txt" Dim objReader As New System.IO.StreamReader(FILE_NAME) TextBox1.Text = objReader.ReadToEnd objReader.Close() What happens if the file does not exist??

6 Read To End You can, though, test to see if the file exists. If it does, you can open it; if not, you can display an error message. Amend your code to this (the new lines are in bold, red): Dim FILE_NAME As String = "C:\test.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) TextBox1.Text = objReader.ReadToEnd objReader.Close() Else MsgBox("File Does Not Exist") End If

7 Reading a Text File Line by Line Quite often, you don't want to read the whole file at once. You want to read it line by line. In which case, instead of using the ReadToEnd method, you can use the ReadLine method: The ReadLine method, as its name suggests, reads text one line at a time. In order to do this, though, you need to use a loop. You can then loop round each line and read it into a variable. Here's a coding example: Dim FILE_NAME As String = "C:\test.txt" Dim TextLine As String If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Do While objReader.Peek() <> -1 TextLine = TextLine & objReader.ReadLine() & vbNewLine Loop Textbox1.Text = TextLine Else MsgBox("File Does Not Exist") End If

8 Reading a Text File Line by Line Exercise Make the user choose the path of the file to read Store the text of the file within an array (one array cell for each line of the file) and show the content of the array in a ListBox (one ListBox item for each array cell) Show in a MessageBox the number of the line that contains the word “test” (if “test” does not exist within the file show an appropriate message)

9 How to Write to a Text File in VB.NET Instead of using the StreamReader we use the StreamWriter. The StreamWriter is used to write a stream of text to a file. Add another Button to the form you've been working on. Set the Text property of the button to "Write to File". Double click your new button to open up the coding window. Add the following: Dim FILE_NAME As String = "C:\test2.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objWriter As New System.IO.StreamWriter(FILE_NAME) objWriter.Write(TextBox1.Text) objWriter.Close() MsgBox("Text written to file") Else MsgBox("File Does Not Exist") End If

10 How to Write to a Text File in VB.NET If you don't have to write the whole text at once, you can write line by line. In that case, select WriteLine (instead of Write) from the available properties and methods. Here's an example of how to use WriteLine: Dim FILE_NAME As String = "C:\test2.txt" Dim i As Integer Dim aryText(4) As String aryText(0) = "Mary WriteLine" aryText(1) = "Had" aryText(2) = "A" aryText(3) = "Little" aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME) For i = 0 To 4 objWriter.WriteLine(aryText(i)) Next objWriter.Close()

11 How to Write to a Text File in VB.NET Exercise Make the user choose the path of the file to write Add a ListBox, insert some items and write the ListBox items within a file(one line for each ListBox Item)

12 Appending Text to a File in VB.NET There will be times when you won't want to erase all the text from your file. You'll only want to add text to what you currently have. In which case you need to Append. To append text to a file, you type a comma after your file name then type the word True: Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True)

13 Appending Text to a File in VB.NET Dim FILE_NAME As String = "C:\test.txt" Dim i As Integer Dim aryText(4) As String aryText(0) = "Mary WriteLine" aryText(1) = "Had" aryText(2) = "Another" aryText(3) = "Little" aryText(4) = "One" Dim objWriter As New System.IO.StreamWriter(FILE_NAME, True) For i = 0 To 4 objWriter.WriteLine(aryText(i)) Next objWriter.Close() MsgBox("Text Appended to the File")

14 How to Copy a File in VB.NET You can also copy a file that you've created. This time, we don't need the StreamWriter or StreamReader of System.IO. We need the File object: System.IO.File File has it's own properties and methods you can use. One of these is Copy. Here's some code that makes a copy of our test file Dim FileToCopy As String Dim NewCopy As String FileToCopy = "C:\test.txt" NewCopy = "C:\NewTest.txt" If System.IO.File.Exists(FileToCopy) = True Then System.IO.File.Copy(FileToCopy, NewCopy) MsgBox("File Copied") End If

15 How to Move a File with VB.NET This time, we use the Move method of System.IO.File. Here's some code: FileToMove = "C:\test.txt" MoveLocation = "C:\ TestFolder\test.txt" If System.IO.File.Exists(FileToMove) = True Then System.IO.File.Move(FileToMove, MoveLocation) MsgBox("File Moved") End If

16 How to Delete a File in VB.NET To delete a file from your computer, you use the Delete method of System.IO. Here's some new code for you to try: Dim FileToDelete As String FileToDelete = "C:\testDelete.txt" If System.IO.File.Exists(FileToDelete) = True Then System.IO.File.Delete(FileToDelete) MsgBox("File Deleted") End If

17 Subroutines in VB.NET Private Sub AddNumbers(ByVal first2 As Integer, ByVal second2 As Integer) Dim answer As Integer answer = first2 + second2 MsgBox "The total is " & answer End Sub Call AddNumbers(4, 5)

18 ByVal and ByRef in VB.NET The word ByVal is short for "By Value". What it means is that you are passing a copy of a variable to your Subroutine. You can make changes to the copy and the original will not be altered. ByRef is the alternative. This is short for By Reference. This means that you are not handing over a copy of the original variable but pointing to the original variable. Let's see a coding example.

19 ByVal and ByRef in VB.NET Add a new button. Double click the button and add the following code: Dim Number1 As Integer Number1 = 10 Call IncrementVariable(Number1) MsgBox(Number1) Private Sub IncrementVariable(ByVal Number1 As Integer) Number1 = Number1 + 1 End Sub What is the value of Number1 after the subroutine called? Change the parameter to the this: ByRef Number1 As Integer

20 How to Create a Function in VB.NET Private Function AddNumbers(ByVal first2 As Integer, ByVal second2 As Integer) As Integer Dim answer As Integer answer = first2 + second2 AddNumbers=answer End Function Dim sum as Integer sum=AddNumbers(4, 5) Instead of saying AddTwoNumbers = answer you can use the Return keyword. You use it like this: Return answer