Visual Basics Practical


Q1. What do you mean by IDE? Is C++ an IDE? If yes, how?
An integrated development environment (IDE), also known as integrated design environment and integrated debugging environment, is a type of computer software that assists computer programmers to develop software. In the case of Visual Basic .NET, that IDE is Visual Studio

An Integrated Development Environment (IDE) is an application that facilitates application development. In general, an IDE is a graphical user interface (GUI)-based workbench designed to aid a developer in building software applications with an integrated environment combined with all the required tools at hand.
Most common features, such as debugging, version control and data structure browsing, help a developer quickly execute actions without switching to other applications. Thus, it helps maximize productivity by providing similar user interfaces (UI) for related components and reduces the time taken to learn the language. An IDE supports single or multiple languages.
The concept of IDE evolved from simple command based software which was not as useful as menu-driven software. Modern IDEs are mostly used in the context of visual programming, where applications are quickly created by moving programming building blocks or code nodes that generate flowchart and structure diagrams, which are compiled or interpreted.
Selecting a good IDE is based on factors, such as language support, operating system (OS) needs and costs associated with using the IDE etc.
Q2. What is an Event? What do you mean by event driven programming?
While you might visualize a Visual Studio project as a series of procedures that execute in a sequence, in reality, most programs are event driven—meaning the flow of execution is determined by external occurrences called events.
An event is a signal that informs an application that something important has occurred. For example, when a user clicks a control on a form, the form can raise a Click event and call a procedure that handles the event. Events also allow separate tasks to communicate. Say, for example, that your application performs a sort task separately from the main application. If a user cancels the sort, your application can send a cancel event instructing the sort process to stop.
               In computer programming, event-driven programming is a programming paradigm in which the flow of the program is determined by events such as user actions (mouse clicks, key presses), sensor outputs, or messages from other programs/threads. Event-driven programming is the dominant paradigm used in graphical user interfaces and other applications (e.g., JavaScript web applications) that are centered on performing certain actions in response to user input. This is also true of programming for device drivers (e.g., P in USB device driver stacks).
In an event-driven application, there is generally a main loop that listens for events, and then triggers a callback function when one of those events is detected. In embedded systems, the same may be achieved using hardware interrupts instead of a constantly running main loop. Event-driven programs can be written in any programming language, although the task is easier in languages that provide high-level abstractions, such as await and closures.














Q3. Explain If-then, If-then else, Case statement using example.

An If statement can be followed by an optional Else statement, which executes when the Boolean expression is false.
Syntax
The syntax of an If...Then... Else statement in VB.Net is as follows −
If(boolean_expression)Then
   'statement(s) will execute if the Boolean expression is true
Else
  'statement(s) will execute if the Boolean expression is false
End If
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
Flow Diagram
Example
Module decisions
   Sub Main()
       'local variable definition '
      Dim a As Integer = 100

      ' check the boolean condition using if statement
      If (a < 20) Then
          ' if condition is true then print the following
          Console.WriteLine("a is less than 20")
      Else
          ' if condition is false then print the following
          Console.WriteLine("a is not less than 20")
      End If
      Console.WriteLine("value of a is : {0}", a)
      Console.ReadLine()
   End Sub
End Module
When the above code is compiled and executed, it produces the following result −
a is not less than 20
value of a is : 100


















Q4. Is VB being a front end or back end? Explain.
The emergence of single page applications introduces a new need for web developers: a front end build process. Javascript MV* frameworks now allow web developers to build complex and sophisticated applications with many files (js, css, sass/less, html …).
The emergence of single page applications introduces a new need for web developers: a front end build process. Javascript MV* frameworks now allow web developers to build complex and sophisticated applications with many files (js, css, sass/less, html …). We’re very far from those 3 lines of JavaScript to put “a kind of magic” on your web site.
In traditional back end development such as asp.net MVC, a compiler transforms source code written in a –human readable- programming language (C#/VB for asp.net) into another computer language (MSIL for .net). Produced files are often called binaries or executables. Compilation may contain several operations: code analysis, preprocessing, parsing, language translation, code generation, code optimization….What about JavaScript?
JavaScript was traditionally implemented as an interpreted language, which can be executed directly inside a browser or a developer console. Most of the examples and sample applications we’ll find on the web have very basic structure and file organization. Some developers may think that it’s the “natural way” to build & deploy web applications, but it’s not.
Nowadays, we’re now building entire web applications using JavaScript. Working with X files is merely different. Few frameworks like RequireJS help us to build modular JavaScript applications thanks to Asynchronous Module Definitions. Again, this is not exactly what we need here because it focuses only on scripts.
What do we need to have today to build a web site? Here are common tasks you may need:
·        Validate scripts with JSLint
·        Run tests (unit/integration/e2e) with code coverage
·        Run preprocessors for scripts (coffee, typescript) or styles (LESS, SASS)
·        Follow WPO recommendations (minify, combine, optimize images…)
·        Continuous testing and Continuous deployment
·        Manage front-end components
·        Run X, execute Y
So, what is exactly a front-end build process? An automated way to run one or more of these tasks in YOUR workflow to generate your production package.
Example front-end build process for an AngularJS application
Please note that asp.net Bundling & Minification is a perfect counter-example: it allows to combine & to minify styles and scripts (two important WPO recommendations) without having a build process. The current implementation is very clever but the scope is limited to these features. By the way, I will still use it for situations where I don’t need a “full control”.
I talk mainly about Javascript here but it’s pretty the same for any kind of front-end file here. SASS/LESS/Typescript/… processing is often well integrated into IDE like Visual Studio but it’s just another way to avoid using a front-end build process.






Q5. Explain the various data types in VB. Explain difference between tool bar & tool box?
When you decide to use a variable, you are in fact asking the computer to use a certain amount of space to hold that variable. Since different variables will be used for different purposes, you should specify the kind of variable you intend to use, then the computer will figure out how much space is needed for a particular variable. Each variable you use will utilize a certain amount of space in the computer's memory.
Before declaring or using a variable, first decide what kind of role that variable will play in your program. Different variables are meant for different situations. The kind of variable you want to use is referred to as a data type. To specify the kind of variable you want to use, you type theAs keyword on the right side of the variable's name. The formula to declare such a variable is:
Dim VariableName As DataType
Once you know what kind of variable you will need, choose the appropriate data type. Data types are organized in categories such as numbers, characters, or other objects.

String

A string is an empty text, a letter, a word or a group of words considered. To declare a string variable, use the String data type. Here is an example:
Private Sub Form_Load()
    Dim CountryName As String
End Sub
After declaring the variable, you can initialize. If you want its area of memory to be empty, you can assign it two double-quotes. Here is an example:
Private Sub Form_Load()
    Dim CountryName As String
   
    CountryName = ""
End Sub
If you want to store something in the memory space allocated to the variable, assign it a word or group of words included between double-quotes. Here is an example:
Private Sub Form_Load()
    Dim CountryName As String
   
    CountryName = "Great Britain"
End Sub
You can also initialize a string variable with another.

Boolean

A Boolean variable is one whose value can be only either True or False. To declare such a variable, use the Boolean keyword. Here is an example:
Private Sub Form_Load()
    Dim IsMarried As Boolean
End Sub
After declaring a Boolean variable, you can initialize by assigning it either True or False. Here is an example:
Private Sub Form_Load()
    Dim IsMarried As Boolean
   
    IsMarried = False
End Sub
Like any other variable, after initializing the variable, it keeps its value until you change its value again.

Numeric Data Types

Introduction

A natural number is one that contains only one digit or a combination of digits and no other character, except those added to make it easier to read. Examples of natural numbers are 122, 8, and 2864347. When a natural number is too long, such 3253754343, to make it easier to read, the thousands are separated by a special character. This character depends on the language or group of language and it is called the thousands separator. For US English, this character is the comma. The thousands separator symbol is mainly used only to make the number easier to read.
To support different scenarios, Microsoft provides different types of natural numbers

Byte

A byte is a small natural positive number that ranges from 0 to 255. A variable of byte type can be used to hold small values such as a person's age, the number of fingers on an animal, etc.
To declare  a variable for a small number, use the Byte keyword. Here is an example:
Private Sub Form_Load()
    Dim StudentAge As Byte
End Sub

Integer

An integer is a natural number larger than the Byte. It can hold a value between
-32,768 and 32,767. Examples of such ranges are: the number of pages of a book.
To declare a variable of type integer, use the Integer keyword. Here is an example:
Private Sub Form_Load()
    Dim MusicTracks As Integer
End Sub
Long Integer

A long integer is a natural number whose value is between –2,147,483,648 and 2,147,483,642. Examples are the population of a city, the distance between places of different countries, the number of words of a book.
To declare a variable that can hold a very large natural number, use the Long keyword. Here is an example:
Private Sub Form_Load()
    Dim Population As Long
End Sub
Decimal Data Types

Introduction

A real number is one that displays a decimal part. This means that the number can be made of two sections separated by a symbol that is referred to as the Decimal Separator or Decimal Symbol. This symbol is different by language, country, group of languages, or group of countries. In US English, this symbol is the period as can be verified from the Regional (and Language) Settings of the Control Panel of computers of most regular users:
On both sides of the Decimal Symbol, digits are used to specify the value of the number. The number of digits on the right side of the symbol determines how much precision the number offers.


Single

A single is a decimal number whose value can range from –3.402823e38 and –1.401298e-45 if the number is negative, or 1.401298e-45 and 3.402823e38 if the number is positive.
To declare a variable that can hold small decimal numbers with no concern for precision, use the Single data type. Here is an example:
Private Sub Form_Load()
    Dim CountryName As String
    Dim IsMarried As Boolean
    Dim StudentAge As Byte
    Dim Tracks As Integer
    Dim Population As Long
    Dim Distance As Single
End Sub
Double

While the Single data type can allow large numbers, it offers less precision. For an even larger number, Microsoft Visual Basic provides the Double data type. This is used for a variable that would hold numbers that range from 1.79769313486231e308 to –4.94065645841247e–324 if the number is negative or from 1.79769313486231E308 to 4.94065645841247E–324 if the number is positive.
To declare a variable that can store large decimal numbers with a good level of precision, use the Double keyword.

In most circumstances, it is preferable to use Double instead of Single when declaring a variable that would hold a decimal number. Although the Double takes more memory spaces (computer memory is not expensive anymore(!)), it provides more precision.
Here is an example of declaring a Double variable:
Private Sub Form_Load()
    Dim Distance As Double
End Sub
Currency

The Currency data type is used for a variable that can hold monetary values. To declare such a variable, use the Currency keyword. Here is an example:
Private Sub Form_Load()
    Dim CountryName As String
    Dim IsMarried As Boolean
    Dim StudentAge As Byte
    Dim Tracks As Integer
    Dim Population As Long
    Dim Distance As Single
    Dim StartingSalary As Currency
End Sub
Toolbar
The toolbar is a set of buttons and icons that are part of any software's user interface. In most cases, the toolbar is located directly below the menu bar. The toolbar has different icons that allow the user to manage or control the program and change the settings to his preferences. The toolbar in the Microsoft Wordprogram, for instance, allows the user to change the font type, size and color, as well as paragraph styles, formats and referencing. It also has several other features for customization.
Taskbar
In a Windows operating system, the taskbar is the horizontal bar that is visible at the bottom of the screen. It was first introduced in Windows 95 and has been a part of all subsequent releases of the operating system. The taskbar helps the user locate and launch programs through the "Start" button, view programs that are open, display or change the time/date, and view programs that are functioning in the background.
Taskbar Sections
The taskbar has four main sections: the Start button to open the start menu and launch programs; the Quick Launch toolbar, which will help you start programs with a single click; the Notification area on the extreme right, which displays computer settings, date, time and status of some programs; and the middle section, which shows the programs, documents, folders or files that are open.
Differences
The taskbar is a part of the operating system, while every different software or program has its own taskbar. The taskbar has a set of fixed icons and functions. The toolbar, on the other hand, can have different parts and functions depending on the program. For instance, the Microsoft Word toolbar would have controls for fonts and paragraphs while the one for Adobe Photoshop has controls for photo editing, including colors, saturation, sharpening and blurs.
Q6. Explain different loop structure in VB with example.
Visual Basic loop structures allow you to run one or more lines of code repetitively. You can repeat the statements in a loop structure until a condition is True, until a condition is False, a specified number of times, or once for each element in a collection.
The following illustration shows a loop structure that runs a set of statements until a condition becomes true.

Running a set of statements until a condition becomes true

While Loops

The While...End While construction runs a set of statements as long as the condition specified in the Whilestatement is True. For more information, see While...End While Statement.

Do Loops

The Do...Loop construction allows you to test a condition at either the beginning or the end of a loop structure. You can also specify whether to repeat the loop while the condition remains True or until it becomes True. For more information, see Do...Loop Statement.

For Loops

The For...Next construction performs the loop a set number of times. It uses a loop control variable, also called a counter, to keep track of the repetitions. You specify the starting and ending values for this counter, and you can optionally specify the amount by which it increases from one repetition to the next. For more information, see For...Next Statement.

For Each Loops

The For Each...Next construction runs a set of statements once for each element in a collection. You specify the loop control variable, but you do not have to determine starting or ending values for it. For more information, see For Each...Next Statement.



Q7. Write a program to calculate square of a given number.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim num1 As Integer
        num1 = Val(TextBox1.Text)
        TextBox2.Text = Math.Sqrt(num1)
  End Sub






















Q8. Write a program to create a simple calculator.
Public Class HesapMakinesi 
    Dim Firstnum As Decimal 
    Dim secondnum As Decimal 
    Dim Operations As Integer 
    Dim Operator_selctor As Boolean = False 
    Private Sub HesapMakinesi_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    End Sub 

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click 
        If Operator_selctor = True Then 
            secondnum = TextBox1.Text 
            If Operations = 1 Then 
                TextBox1.Text = Firstnum + secondnum 
            ElseIf Operations = 2 Then 
                TextBox1.Text = Firstnum - secondnum 
            ElseIf Operations = 3 Then 
                TextBox1.Text = Firstnum * secondnum 
            Else 
                If secondnum = 0 Then 
                    TextBox1.Text = "Hata! Lütfen iÅŸleminizi kontrol edin." 
                Else 
                    TextBox1.Text = Firstnum / secondnum 
                End If 
            End If 
            Operator_selctor = False 
        End If 
    End Sub 

    Private Sub Button15_Click(sender As Object, e As EventArgs) Handles Button15.Click 

        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "1" 
        Else 
            TextBox1.Text = "1" 
        End If 
    End Sub 

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "2" 
        Else 
            TextBox1.Text = "2" 
        End If 
    End Sub 

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "3" 
        Else 
            TextBox1.Text = "3" 
        End If 
    End Sub 

    Private Sub Button16_Click(sender As Object, e As EventArgs) Handles Button16.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "4" 
        Else 
            TextBox1.Text = "4" 
        End If 
    End Sub 

    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "5" 
        Else 
            TextBox1.Text = "5" 
        End If 
    End Sub 

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "6" 
        Else 
            TextBox1.Text = "6" 
        End If 
    End Sub 

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "7" 
        Else 
            TextBox1.Text = "7" 
        End If 
    End Sub 

    Private Sub Button17_Click(sender As Object, e As EventArgs) Handles Button17.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "8" 
        Else 
            TextBox1.Text = "8" 
        End If 
    End Sub 

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "9" 
        Else 
            TextBox1.Text = "9" 
        End If 
    End Sub 

    Private Sub Button14_Click(sender As Object, e As EventArgs) Handles Button14.Click 
        If TextBox1.Text <> "0" Then 
            TextBox1.Text += "0" 
        End If 
    End Sub 

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click 
        TextBox1.Text = "0" 
    End Sub 

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click 
        If Not (TextBox1.Text.Contains(".")) Then 
            TextBox1.Text += "," 
        End If 
    End Sub 

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 
        Firstnum = TextBox1.Text 
        TextBox1.Text = "" 
        Operator_selctor = True 
        Operations = 1 ' = + 
    End Sub 

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click 
        Firstnum = TextBox1.Text 
        TextBox1.Text = "" 
        Operator_selctor = True 
        Operations = 2 ' - - 
    End Sub 

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click 
        Firstnum = TextBox1.Text 
        TextBox1.Text = "" 
        Operator_selctor = True 
        Operations = 3 '=x 
    End Sub 

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click 
        Firstnum = TextBox1.Text 
        TextBox1.Text = "" 
        Operator_selctor = True 
        Operations = 4 ' / 
    End Sub 

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 

    End Sub 
End Class











Q9. Write a program to create a Notepad in VB.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using fo As New OpenFileDialog
            fo.Multiselect = False
            fo.RestoreDirectory = True
            fo.Filter = "Text Files|*.txt"
            fo.FilterIndex = 1
            fo.ShowDialog()
            If (Not fo.FileName = Nothing) Then
                TextBox1.Text = File.ReadAllText(fo.FileName)
            End If
        End Using
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Using fs As New SaveFileDialog
            fs.RestoreDirectory = True
            fs.Filter = "Text Files|*.txt"
            fs.FilterIndex = 1
            fs.ShowDialog()
            If Not (fs.FileName = Nothing) Then File.WriteAllText(fs.FileName, TextBox1.Text)
        End Using
    End Sub
End Class











Q10. Write a program to find factorial of a given number.
Private Sub Command1_Click()

Dim j, i as Integer

i = CInt(Text1.Text)

Label2.Caption = facts(i)

End Sub

Private Function facts(i)

F = 1

For j = 1 To i

F = F * j

facts = F

Next j


End Function












                                                                                

Q11. Write a program to calculate the area of circle.
Dim r, b, pi As Integer
Private Sub Command1_Click()
pi = 3.14
r = Text1.Text
b = pi * (r * r)
Label2.Caption = "The Area is " & b
End Sub



Private Sub Command3_Click()
Text1.Text = ""
Label2.Caption = ""
End Sub

                                                   


Q12. Write a program to calculate area of rectangle.
Public Class Form1

    Private Sub txtLength_TextChanged(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles txtLength.TextChanged
        Me.lblAreaAnswer.Text = ""
    End Sub
    Private Sub txtWidth_TextChanged(ByVal sender As Object, _
   ByVal e As System.EventArgs) Handles txtWidth.TextChanged
        Me.lblAreaAnswer.Text = ""
    End Sub
    Private Sub radMultiplication_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles btnAnswer.Click
        Dim answer As Double
        answer = Val(Me.txtLength.Text) * Val(Me.txtWidth.Text)
        Me.lblAreaAnswer.Text = answer
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class












Q13. Write a program to calculate area of triangle.
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
TextBox2.Text = 3.14 * TextBox1.Text
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
TextBox5.Text = TextBox3.Text * TextBox4.Text
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim a As Integer = TextBox6.Text
Dim b As Integer = TextBox7.Text
Dim c As Integer = TextBox8.Text
Dim s As Double = (a + b + c) / 2
Dim findarea As Double = Math.Sqrt(s * (s - a) * (s - b) * (s - c))
TextBox9.Text = findarea
End Sub
End Class








Q14. Write a program to check whether the number is odd or even.
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Mynumber As Integer
        Dim isEven As Boolean
        Mynumber = Val(TextBox1.Text)
        If Mynumber Mod 2 = 0 Then
            isEven = True
            MsgBox("The number " & " " & Mynumber & " is an even number")
        Else
            MsgBox(Mynumber & " " & "is an Odd number")
        End If

    End Sub
End Class
















                                                                    


Q15. Write a program to find a greatest number from 3 numbers.
Function calMax(x, y, z As Variant)

If x > y And x > z Then

calMax = Str(x)
ElseIf y > x And y > z Then
calMax = Str(y)
ElseIf z > x And z > y Then

calMax = Str(z)

End If

End Function

Private Sub Command1_Click()
Dim a, b, c
a = Val(Txt_Num1.Text)
b = Val(Txt_Num2.Text)
c = Val(Txt_Num3.Text)

Lbl_Display.Caption = calMax(a, b, c)

End Sub

Private Sub Label5_Click()

End Sub

Comments