Visual Basic .NET

Nils Wiki>Formal Sciences>Computer Sciences>Software Systems>Programming languages>Visual Basic .NET

Classes
Imports System.Math

Characters
Assign strings to theese character to modulate your text in wanted way.

Organizing
Form - Is simply a new window. Variables declared at the top of the forms, are only valid inside that same form. Module - Can store public variables and procedures that can be used in any form.

Procedures
(not related to event procedures)

Function Procedures Are called by name. Values goes in, a value stored in the function name goes out. (Typically used for calculations)

Creating functions with inputvariables: Function functionname(arguments eks:ByVal variable As datatype, Byref variable As datatype) As datatype function statements eks: variable1 = a-b-c variable2 = a+b-c Return variable2 + variable1 End Function

To call the functions in forms: …a = 32 …b = 34 …c = 66 variable3 = functionname(input variables eks: a, b, c)

ByVal - Will get the value from a parameter, which means that if the variable is changed in the procedure, the new variable value will not be passed back to the calling code. ByRef - Will get the value from a reference, which means that if the variable is changed in the procedure, the new variable value will be passed back to the calling code.

Sub Procedures Sub Procedures are called by name. They receive arguments, and pass back modified arguments. The Sub name does NOT contain any specific value. (Typically used to receive or process inputs, display output, or set properties)

Sub procedurename(arguments eks: ByVal variable As datatype, ByVal variable As datatype) procedure statements End Sub

To call the functions in forms: …variable1 = "value\string" procedurename(inputvariables eks: variable1, variable2)

Functions
variable = InputBox("boxtext", "boxtitle") - Prompts the user with a titled window with an inputbox.

variable = MsgBox("boxtext", “boxtitle") - Displays a titled box with some predefined text.

Conjunctions
IF Structures

If condition1 Then Statement1 executed if condition1 is true Elseif condition2 Then Statement2 executed if condition2 is true Elseif condition3 Then Statement3 executed if condition3 is true Else Statement4 executed if none of the above is true End If

Select Case Structures

Select Case variable Case value1 - Statement executed if value1 matches variable Case value2 - Statement executed if value2 matches variable

Arrays
Dim arrayname(dim1Index, dim2Indeks, ...) As datatype Where dim1Index is the upper bound of the first dimention (equals the number of elements minus 1) And dim2Index is the upper bound of second dimention (equals the number of elements minus 1) Can be compared to a table, where Dim1Index and Dim2Indeks are horizontal rows, and the declared number in between the limits are vertical columns

To call and store in arrays arrayname(5) = "value\word" Stores “value\word” in an array, first dimention block number 5

To call without caring about the upper limit Ubound(arrayname) To call without caring about the lower limit Lbound(arrayname)

Loops
For Loop

Loops as long as the counter value does not reach the end-number.

For variable = startvalue To endvalue Step interval

statements

If condition1 Then Exit For - Ends the loop if condition1 happens

Next variable - Adds one Stepvalue more to the counter value

Do Loop

Do While\Until Condition1

statements

Loop

To be sure that the Do Loop runs atleast one time use this variant.

Do

statements

Loop While\Until condition1''' '''

Counters
variable += 1 - Equivalent to variable = variable + 1

Timers
Show Time

Add a Timer from the toolbox, set timer enabled from when you want it to start, and timer to 1000 (ms) in properties Label1.Text = Timestring	Updates the label every second with the present time

Time Limit

''Add a Timer from the toolbox, and double click it. Add a statement, and it will execute when the selected time (in properties) has elapsed.''

Events
You can choose what happens when an event occur (called declerations), right next to the object symbol.

Snippets
Insert Snippet: - choose snippet from a list

Making the User Select an Image from the Hard Drive to Display
Add ToolStrip (Menus & Toolbars) Add PictureBox (Common Controls) Add OpenFileDialog (Dialogs)

OpenFileDialog.Filter = ”Bitmaps (*.bmp)|*.bmp|Metafiles (*.wmf)|*.wmf” only bmp and wmf files will open OpenFileDialog1.ShowDialog - opens the file browser

PictureBox1.Image = System.Drawing.Image.FromFile ("c:\ …. .jpg") - show the selected picture in the picturebox

(OpenFileDialog1.FileName) - Passes the complete path of the file to the program.

Using Files on the Hard Drive
The user decides where to save: Add Save File Dialog to the form SaveFileDialog1.ShowDialog FileOpen(1, SaveFileDialog1.FileName, OpenMode.Output) PrintLine(1, TextBox1.Text) FileClose(1)

The user decides what to open: Add Open File Dialog to the form OpenFileDialog1.ShowDialog FileOpen(1, OpenFileDialog1, OpenMode.Input) PrintLine(1, TextBox1.Text) FileClose(1)

The program decides where to save: FileOpen(1, "C:\Users\Nils\...\Dokument.TXT", OpenMode.Output) PrintLine(1, TextBox1.Text) FileClose(1)

The program streams text from a file: string = New StreamReader("C:\Users\Nils\...\Dokument.TXT") TextBox1.Text = StreamToDisplay.ReadToEnd string.Close

Filters: SaveFileDialog1.Filter = "Text files (*.txt)|*.txt" OpenFileDialog1.Filter = "Text files (*.txt)|*.txt"

Listbox
To show something in a listbox: ListBox1.Items.Add(writesomething)

To get something from a listbox line number 2: Listbox1.Items(2)

Additional Info
Global variables is declared at the start at the form Other global or initial commands are declared under “Private Sub” Classes are called at the start of the program If a statement continiues at the next line, use underline _ A tempelate is a ready made code