VB.NET

CodingCool VB.NET

Here is where we show basic examples of VB.NET.

Hello World Console App

For this project you will need to create a ConsoleApplication. 

Then in the main file add this code: 

Module Module1

Sub Main()

Console.WriteLine(“Hello World”)

Console.WriteLine(“Press any key to continue…”)

Console.ReadKey()

End Sub

End Module

Hello World WinForms

For this project you will need to create a WinForms app. In the designer add the following components: 

  • a button
  • a label

Then in the code behind add this code.

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs)  Handles Button1.Click

Label1.Text = “Hello World”

End Sub

End Class

Building a Simple Calculator

For this project create a WinForms app.

Setup the form to look like the following image.

Then add the following code: 

Public Class Form1

Private Sub btn_num_Click(sender As Object, e As EventArgs)  Handles btn1.Click, btn2.Click, btn3.Click, btn4.Click, btn5.Click, btn6.Click, btn7.Click, btn8.Click,_ btn9.Click, btn0.Click

Dim btn As Button = CType(sender, Button)

txtequation.Text &= btn.Text

End Sub

Private Sub btn_op_Click(sender As Object, e As EventArgs) Handles btnplus.Click, btnsubtract.Click, btnmultiply.Click, btndivide.Click

Dim btn As Button = CType(sender, Button)

txtequation.Text &= btn.Text

End Sub

Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click

txtequation.Clear()

End Sub

Private Sub btnequals_Click(sender As Object, e As EventArgs) Handles btnequals.Click

Dim equation As String = txtequation.Text

equation = equation.Replace(“x”, “*”)

equation = equation.Replace(“÷”, “/”)

Dim result As String = New Datatable().Compute(equation, Nothing).ToString()

txtequation.Text = result

End Sub

End Class

Play Music/Videos

For this project create WinForms app.

In design mode right click in the toolbox and select choose items. In the form that shows select windows media player and hit ok.

Then add the new element as well as a button to the form.

Then add the following code: 

Public Class Form1

Private Sub btnplay_Click(sender As Object, e As EventArgs) Handles btnplay.Click

Dim ofd As New OpenFileDialog

If ofd.ShowDialog() = DialogResult.Ok Then

AxWindowsMediaPlayer1.URL = ofd.FileName

End If

End Sub

End Class

Simple WebBrowser

For this project create a WinForms app and add the following componets to the form:

  • a WebBrowser control
  • a button with text of Back
  • a button with text of Forward
  • a button with text of Go
  • a textbox

Name controls according to the following code: 

Public Class Form1

Private Sub btnback_Click(sender As Object, e As EventArgs) Handles btnback.Click

WebBrowser1.GoBack

End Sub

Private Sub btnforward(sender As Object, e As EventArgs) Handles btnforwards.Click

WebBrowser1.GoForwards

End Sub

Private Sub btngo_Click(sender As Object, e As EventArgs) Handles btngo.Click

WebBrowser1.Navigate(txtsearch.Text)

End Sub

End Class

Showing notifications

For this project create a WinForms app and when it’s created install the Tulep.NotificationWindow package.

Add the following code: 

Imports Tulep.NotificationWindow

Public Class Form1

Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim popup As New PopupNotifier popup.TitleText = “Test”

popup.ContentText = “Hello” popup.Popup2

End Sub

End Class

Custom control

Create a WinForms app and add a class called Clock.

Add the following code:

Public Class Clock
Inherits Label

WithEvents timer As New Timer

Public Sub New()

tmrtime.Start()

End Sub

Private Sub timer_Tick() Handles timer.Tick

Text = Format(Now, “hh:mm:ss: tt”)

End Sub

End Class

Next rebuild the project by right clicking and hitting rebuild. On the form your Clock object should appear. Add it to the form and start the application.

Factorials

Create a WinForms app and add the following components:

  • a textbox
  • a button
  • a label

Add this code:

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim fac As Integer = 1
Dim k As Integer

For k = Val(TextBox1.Text) To 1 Step -1

fac *= k

Next
Label3.Text = fac

End Sub

End Class

Exponents

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim result As Double = Double.Parse(TextBox1.Text) ^ Double.Parse(TextBox2.Text)
Label3.Text = result

End Sub

End Class

Files

In this section we will be discussing files.

WriteAllText

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

IO.File.WriteAllText(“Your path”, TextBox1.Text)

End Sub

End Class

AppendAllText

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

IO.File.AppendAllText(“Your path”, TextBox1.Text)

End Sub

End Class

ReadAllText

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

TextBox1.Text = IO.File.ReadAllText(“Your path”)

End Sub

End Class

Create

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

If Not IO.File.Exists(“The path you want to create”) Then

IO.File.Create(“The path you want to create”).Dispose()

End If

End Sub

End Class

Delete

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

My.Computer.FileSystem.DeleteFile(“file”,
FileIO.UIOption.AllDialogs,
FileIO.RecycleOption.SendToRecycleBin,
FileIO.UICancelOption.DoNothing)

End Sub

End Class

MSSQL

MySQL