Presentation is loading. Please wait.

Presentation is loading. Please wait.

Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY.

Similar presentations


Presentation on theme: "Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY."— Presentation transcript:

1 Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY

2 Contents  Introduction.  Algorithms.  Flowcharts.  VB.Net fundamentals.  Windows forms & tools programming.  VB.Net and Directories.  VB.Net and Files.  VB.Net and Data Bases.

3 Introduction Visual Basic is an Advanced high level Visual programming language  High level : The syntax uses human being words.  Advanced : Has new data types ( as Classes ) not used in other languages.  Visual : You see what you program.

4 Algorithms An algorithm is a set of steps describing briefly and clearly a problem solution The following steps represent an algorithm to add two numbers ( N1 and N2 ) and print their sum. 1.Start. 2.Input N1, N2. 3.Sum = N1 + N2. 4.Print Sum. 5.End.

5 Flowcharts  Algorithms can be expressed, also, by flowcharts.  A flowchart is a set of connected figures or boxes.  The most important flowcharts components are : 1.Start and end box. 2. Standard Input and output box. 3.Operations box. 4.Decision box. 5.Path connection box.

6 Simple flowchart Not repetitive and not parallel. 1.Start. 2.Input N1, N2. 3.Sum = N1 + N2. 4.Print Sum. 5.End. Start Print Sums Sum = N1 + N2 Input N1, N2s End

7 Parallel flowchart 1.Start. 2.Input N1, N2. 3.If N1 > N2 max = N1 else max = N2. 4.Print max. 5.End. Has a decision box. Start Input N1, N3s ? N1 > N2 max = N2max = N1 Print maxs End YesNo

8 Repetitive flowchart 1.Start. 2.N = 10. 3.N = N + 1 4.If N < 200 go to 3. 5.Print N. 6.End. Has to repeat statements. Start ? N < 200 N = 10 N = N + 1 Print Ns End Yes No

9 VB.Net fundamentals Data types. Constants and variables. Operators. Standard input / output functions. Control statements. Subroutines and functions.

10 Data types Short : Integer numbers, use 2 Bytes of memory. Integer : Integer numbers, use 4 Bytes of memory. Long : Integer numbers, use 8 Bytes of memory. Single : Real numbers, use 4 Bytes of memory. Double : Real numbers, use 8 Bytes of memory. Decimal : Real numbers, use 12 Bytes of memory. Chr : for characters, use 2 Bytes of memory. String : for string of characters, unlimited bytes. Bollean : true and false values, 2 Bytes of memory. Byte : Values between 0 and 255, 1 Byte of memory. DateTime : date and time values, 8 Bytes.

11 Constants A constant represents a name reserving a place in memory to store an unchangeable value when executing the program. Constant declaration : Const constant_name As constant_type = constant_value Example : Const PI As Double = 3.14

12 Variables A variable represents a name reserving a place in memory to store a changeable value when executing the program. Variable declaration : Dim variable_name As variable_type Examples : Dim Var As Double Dim Array ( ) As String

13 Operators Assignment operator ( = ) used to assign values to variables and constants Examples: Dim Var As Integer var = 15 Const Str As String = “ Hello VB.Net”

14 Concatenation operator ( & ) used to join values to each other Example Console.WriteLine (“ Hello VB.Net” & “This is my first code line in VB.Net”)

15 Arithmetic operators 1. ^ Power 2. * Multiplication 3. / Division 4. + Addition 5. - Subtraction

16 Relational Operators > Bigger than < Smaller than >= Bigger than or equal <= Smaller than or equal = Equal <> Doesn't equal

17 Logical Operators And, Or, Not, Xor

18 Standard inputs / output functions The standard input / output functions are used with console applications to input data from the keyboard and output information on screen The Standard input / output functions are members in the Console Class belonging to System.IO Namespace

19 Standard output functions The standard output functions are : Write and WriteLine The difference between them is : The WriteLine unction sends the curser to next line after execution, whereas the Write function doesn't. To call them we use the following statements : Console.Write ( ) Console.WriteLine ( )

20 Example : Standard output functions Imports System.IO Module Module1 Sub Main ( ) Dim Age As integer Age = 18 Console.WriteLine ( “Hello VB.Net”) Console.Write ( “your age is “ & Age ) End Sub End Module

21 Standard input functions The standard input functions are : Read ( ) and ReadLine ( ) The difference between them is : The RedLine function reads data from the keyboard until you strike the enter key, whereas the Read function reads data from the keyboard until it find the first blank. To call them we use the following statements : Console.Read ( ) Console.ReadLine ( )

22 Example : Standard input functions Imports System.IO Module Module1 Sub Main ( ) Dim Age As integer Dim Name As String Console.WriteLine ( “ Enter your Name” ) Name = Console.ReadLine ( ) Console.WriteLine( “Enter your age “) Age = Console.Read ( ) Console.WriteLine ( Name ) Console.Write ( “your age is “ & Age ) End Sub End Module

23 Control statements If statement if ( conditional_experssion ) then statements end if

24 Example If statement Imports System.IO Module Module1 Sub Main ( ) Dim A As integer Dim B As integer A = Console.ReadLine ( ) B = Console.ReadLine ( ) if ( A > B ) then Console.WriteLine ( A ) end if if ( A < B ) then Console.WriteLine ( B ) end if End Sub End Module

25 If …….. else statement if ( conditional_experssion ) then statements1 else statements2 end if

26 Example If …….. else statement Imports System.IO Module Module1 Sub Main ( ) Dim A As integer Dim B As integer A = Console.ReadLine ( ) B = Console.ReadLine ( ) if ( A > B ) then Console.WriteLine ( A ) else Console.WriteLine ( B ) end if End Sub End Module

27 If …….. Elseif ……. Else statement If ( conditional_experssion1 ) then statements1 Elseif ( conditional_experssion2 ) then statements2 Else statements3 End if

28 Example If …….. elseif ……. Else statement Imports System.IO Module Module1 Sub Main ( ) Dim A As integer Dim B As integer A = Console.ReadLine ( ) B = Console.ReadLine ( ) if ( A > B ) then Console.WriteLine ( A ) elseif ( A < B ) then Console.WriteLine ( B ) else Console.WriteLine ( “A = B “ ) end if End Sub End Module

29 Select case statement The following example describes how to use this statement Dim Day_Of_Week As Integer Day_Of_Week = Console.ReadLine ( ) Select Case Day_Of_Week Case 0 Console.WriteLine ( “Friday” ) Case 1 Console.WriteLine ( “Saturday” ) Case 2 Console.WriteLine ( “Sunday” ) Case 3 Console.WriteLine ( “Monday” )

30 The loop For statement for I = value of beginning to value of end statements Next Example Dim I As Integer for I = 0 to 10 Console.WriteLine (I) Next

31 The loop For each statement Used with Arrays Example Dim Days_Of_Week ( ) As Integer = { 0, 1, 2, 3 } Dim Day As Integer for each Day In Days_Of_Week Console.WriteLine ( Day ) Next

32 The loop While Statement While ( condition ) Statements End while Example Dim N As Integer N = Console.ReadLine ( ) While ( N 0 ) Console.WriteLine ( “Enter another number”) End while

33 The loop Do ….. While Statement Do Statements Loop while ( Condition ) Example Dim N As Integer N = Console.ReadLine ( ) Do Console.WriteLine ( “Enter another number”) Loop while ( N 0 )

34 Subroutines A subroutine is a specific task program. Subroutine declaration : Sub subroutine_name ( ) Statements to be executed End sub Subroutine Calling : To call a subroutine you write its name followed by ( )

35 Subroutine Example Imports System.IO Module Module1 Sub Show_message_Box ( ) MsgBox ( “ VB.Net” ) End Sub Sub Main ( ) Show_message_Box ( ) End Sub End Module

36 Functions A function is a specific task program. Function declaration : Function function_name ( ) As function_return_value_type Statements to be executed End Function Function Calling : To call a function you write its name followed by ( )

37 Function Example Imports System.IO Module Module1 Function Add ( ByVal N1 As integer, ByVal N2 As integer ) As Integer Return ( N1 + N2 ) End Function Sub Main ( ) Dim Sum As Integer Sum = Add ( 10, 40 ) MsgBox ( Sum ) End Sub End Module

38 Windows forms programming  VB.Net is a powerful tool used to build Windows-Based applications.  Each window in Windows-Based application contains tools such as Buttons.  Each tool is related to events, such as mouse click.  Each tool is programmed to perform a specific task when a related tool event happen.  Each tool has many properties, such as Appearance properties.  When programming a tool, the most important used properties are : The ( name ) property and the Text property.

39 Form tool programming Forms represent the main windows in Windows-Based Applications 1. Run the Visual Studio application. 2. At the Start page window click create project, a new project window appear. 3. Chose Visual Basic, then Windows option. 4. Double click on the Windows Forms Application icon, the Form1.vb window will appear in the design mode. 5. The Form1.vb window contains a Window named Form1 in the Design mode. 6. The Form1 window represents the main tool for the application in which all other needed application tools will be placed on. 7. Locate the property Text in the properties window and change its value to Form. 8. Go to build menu and select BuildApplicatin option. 9. Go to debug menu and select start debugging option.

40 Button tool programming Buttons represent the most important tools in windows applications 1. Run the Visual Studio application. 2. Re-Open the previous programmed form in the design mode. 3. From the toolbox window, drag the tool Button and drop it into the form window. 4. Locate the property ( Name ) in the properties window and change its value to Click_Me. 5. Locate the property Text in the properties window and change its value to Click_Me. 6. Double click on the Button, the initial code for this Button will appear. 7. Insert the statement Click_Me.Text = “Done” into the Button code. 8. Go to build menu and select BuildApplicatin option. 9. Go to debug menu and select start debugging option. Important Note : You can use the ( Name ) property to access and modify all other tool properties.

41 Label tool programming Labels are, usually, used to name other tools in windows applications 1. Run the Visual Studio application. 2. Re-Open the previous programmed form in the design mode. 3. From the toolbox window, drag the tool label and drop it into the form window. 4. Locate the property ( Name ) in the properties window and change its value to Label1. 5. Locate the property Text in the properties window and change its value to Visual Basic. 6. Double click on the Click_Me Button, the initial code will appear. 7. Insert the statement Label1.Text = “Visual Studio” into the Button code. 8. Go to build menu and select BuildApplicatin option. 9. Go to debug menu and select start debugging option.

42 TextBox tool programming Text Boxes are, usually, used to input data and output information 1. Run the Visual Studio application. 2. Re-Open the previous programmed form in the design mode. 3. From the toolbox window, drag the tool TextBox and drop it into the form window. 4. Locate the property ( Name ) in the properties window and change its value to TextBox1. 5. Locate the property Text in the properties window and change its value to Visual Basic. 6. Double click on the Click_Me Button, the initial code will appear. 7. Insert the statement TextBox1.Text = “VB.Net 210” into the Button code. 8. Go to build menu and select BuildApplicatin option. 9. Go to debug menu and select start debugging option.

43 Project Developing a Calculator

44 VB.Net and Directories

45 Directory operations Programming All functions handling Directories are members in the Directory Class belonging to System.IO Namespace. These function are : 1. GreateDirectory : Used to create Directories. Example : Directory.GreateDirectory ( “ C:\Sample01” ) 2. Delete : Used to delete directories. Example1 : Directory.Delete (“ C:\Sample02 “, False ) Example2 : Directory.Delete (“ C:\Sample03 “, True )

46 Directory operations Programming 3. Exists : To know if the directory exists or no. Example : if Directory.Exists( “ C:\Sample01” ) then 4. Move : To move a directory. Ex. : Directory.Move(“C:\Sample01“, “C:\Sample02\Sample01 ) 5. GetCurrentDirectory : To know the current directory. Ex. : Console.WriteLine ( Directory. GetCurrentDirectory ( ) ) 6. SetCurrentDirectory : To move to another directory. Ex. : Directoy.SetCurrentDirectory ( “C:\D1\D2” )

47 Directory operations Programming 7. GetCreationTime : To know the creation date and time. Ex. : Console.WriteLine ( Directory. GetCreationTime (“C:\D1” ) ) 8. GetDirectories:To get the names of Subdirectories in a directory. Ex. Dim Dirs ( ) As String = Directory. GetDirectories ( “ C:\ ” ) 9. GetFiles : To get the names of files in a directory. Ex1. Dim Files ( ) As String = Directory. GetFiles ( “ C:\ ” ) Ex2. Dim Files ( ) As String = Directory. GetFiles ( “ *.* ” )

48 Example : Directory operations programming Imports System.IO Module Module1 Sub Main ( ) If Directory.Exists( “ C:\D1 ” ) then Console.WriteLine ( “ D1 already exists ” ) Console.WriteLine (Directory. GetCreationTime(“ C:\D1” ) ) Else Directory.CreateDirectory( “ C:\D1” ) End Sub End Module

49 VB.Net and Files

50 General File operations Programming The general File operation Functions are : Copy, Move, Exists and Delete These functions are members in File class belonging to System.IO Namespace. 1. Copy function is used to copy files. Example : File.Copy ( “ C:\f1.txt“, “ D:\ f1.txt “)

51 General File operations Programming 2. Move function is used to move files. Example : File.Move (“ C:\f1.txt “, “ C:\T1\T2\f1.txt” ) 3. Delete function is used to delete files. Example : File.Delete ( “ C:\f1.txt“ ) 4. Exists function is used to check the existence of a file. Example : if File.Exists (“ C:\f1.txt “ ) Then

52 Example : Some File operations programming Imports System.IO Module Module1 Sub Main ( ) If File.Exists( “ C:\f1.txt ” ) = true then File.Copy( “ C:\f1.txt”, “ C:\D1\f1.txt” ) Else MsgBox( “ f1.text is not exists” ) End Sub End Module

53 Working with Text files Creating, Opening, Writing and Reading To create, open, write and read Text files we can use members of StreamWitter and StreamReader Classes belonging to System.IO Namespace. The StreamReader reading functions are : Read and ReadLine. The StreamWriter writing functions are : Write and WriteLine.

54 Reading from Text files To open and read a text file, we have to declare an object of type StreamReader, then use this object to call one of reading functions. Example : Reading one line from the file named File_Name Dim Line As String Dim SR As New StreamReader ( File_Name ) Line = SR.ReadLine ( )

55 Example : Reading a text file contents Line by Line Imports System.IO Module Module1 Sub Main ( ) Dim Line As String Dim SR As New StreamReader ( “C:\f1.txt” ) Do Contents = SR.ReaLine ( ) If ( Contents <> Nothing ) then Console.WriteLine ( Contents ) end If Loop While ( Contents <> Nothing ) SR.Close ( ) End Sub End Module

56 Writing to Text files To create or open a text file and writing to it, we have to declare an object of type StreamWriter, then use this object to call one of writing functions. Example : Writing one line to the file named File_Name Dim SW As New StreamWriter ( File_Name ) SW.WriteLine ( “ Hello VB.Net” )

57 Example : writing to a text file Imports System.IO Module Module1 Sub Main ( ) Dim SW As New StreamWriter ( “C:\f1.txt” ) Dim I As Integer For I = 0 to 10 SW.WriteLine ( “Hello VB.Net ”, I ) Next SW.Close ( ) End Sub End Module

58 Open / Save Files using Open / Save Dialog Boxes - Open / Save Dialog Boxes work with Windows Application. - Open File Dialog Box is used to look for a file to open it. - Save File Dialog is used to look for a file to save information. - VB.Net has Two classes to handle Open / Save File Dialog Boxes. - These classes are : OpenFileDialog class and SaveFileDialog class.

59 Open / Save File Dialog Boxes - The classes’s main members are: ShowDialog Function and FileName property. - ShowDialg Function is used to display the dialog box when it is called. - FileName property is a String type variable used to store the file name and to access it. - To display a dialog Box, you have to declare an object of its class type, then. - Calling the ShowDialog function using the declared object name. - To open or save a file you have to look for it in the corresponding dialog Box and click it. - When you click the open or save button on the corresponding dialog box, the FileName property will store the file name ( The file name includes the path to the file ). - The FileName property is used, then, to open and save the file.

60 Example : Working with Open / save File Dialog Boxes 1. Launch Visual studio Application. 2. Click Visual Basic > Windows application. 3. Drag and drop two Buttons controls and one TextBox control into the displayed form in design mode. 4. Chang the Text property for the Buttons to Open for the first one and to Save for the second. 5. Insert the following Statements to the Codes of Buttons.

61 Statements to be inserted to Open button code Dim OpenFileDialog1 As New OpenFileDialog OpenFileDialog1.ShowDialog( ) Dim SR as New StreamReader(OpenFileDialog1.FileName ) TextBox1.Text = SR.ReadLine( ) SR.Close( )

62 Statements to be inserted to Save button code Dim SaveFileDialog1 As New SaveFileDialog SaveFileDialog1.ShowDialog( ) Dim SW as New StreamWriter(SaveFileDialog1.FileName ) SW.WriteLine( TextBox1.Text ) SW.Close( )

63 VB.Net and Data Bases.

64 ADO.Net VB.Net has a technology allow to connect and handle Databases. This technology called ADO.Net ( ADO = Active Data Objects ) has a set of Classes allowing to perform operations on Databases. These Classes are: 1. Connection Class : Allow to connect, open and close the Databases. Connect, open and close can be done by creating objects of Type Connection Class, then, using them to call Connection class members. Example : how to create a Connection Object : Dim Con As New OleDb.OleDbConnection (“PROVIDER = ?“ & “Data Source = ?” )

65 Example how to Create a connection Object to connect to database, open and close it Dim Con As New OleDb.OleDbConnection Dim dbProvider As String Dim dbSource As String dbProvider = “PROVIDER = Microsoft.Jet.OLEDB.4.0;” dbSource = “Data Source = C:\AddressBook.mdb” Con.ConnectionString = dbProvider & dbSource Con.Open ( ) MsgBox ( “Data Base is now open” ) Con.Close ( )

66 ADO.Net 2. Command Class : Used to perform queries on Databases. Example : how to create a Command Object to perform a query. Dim sql As String = “ SELECT *FROM tblContacts “

67 ADO.Net 3. Data Adapter Class : The Data Adapter object is used to fill the data Set object with records from a database table. It acts as mediator between the connection Object and Data Set Object. The parameters have to be passed to the Data Adapter object are the Command and connection Objects. Example : how to create Adapter object. Dim da As New OleDb.OleDbDataAdapter ( Sql, Con )

68 ADO.Net 4. Data Set Class : The Data set object is used to hold all information from a Database. To create a Data Set object we can use the following statement. Dim ds As New DataSet To fill the Data Set object with records from database tables we can use the following statement: da.Fill ( ds, “AddressBook”) AddressBook is the name of database.

69 Example : Display records of the table in database ( AddressBook ) Imports System.IO Module Module1 Sub Main ( ) Dim Con As New OleDb.OleDbConnection Dim dbProvider As String Dim dbSource As String dbProvider = “PROVIDER = Microsoft.Jet.OLEDB.4.0;” dbSource = “Data Source = C:\AddressBook.mdb” Con.ConnectionString = dbProvider & dbSource Dim DS As New DataSet Dim DA As OleDb.OledbDataAdapter Dim Sql As String Con.Open ( ) Sql = “SELECT * FROM tblContacts” DA = New OleDb.OleDbDataAdapter (Sql, Con ) DA.Fill ( DS, “AddressBook”) MsgBox ( “Data Base is now open” ) Con.Close ( ) MsgBox ( “Data Base is now Closed” ) Console.WriteLine ( DS.Tables ( “AddressBook”).Rows(0).Item(1) End Sub End Module

70 VB.Net and Data Bases – Easy way VB.Net has a wizard enable to create applications for databases reading, updating and scrolling. 1. From the menu File > New Project > windows Applications. 2. From the menu View > Solution Explorer. 3. From the Solution Explorer window > Data Source Tab > Click Add new data source link > database from the wizard and flow the instructions or : From the Menu Data > Add new data source > database and follow the wizard’s instructions.


Download ppt "Visual BASIC Programming For CCS 301 course Dr. Ahmad ABDELHAY."

Similar presentations


Ads by Google