This page will have a growing list of useful code from single lines to small subroutines....
Colour in Console Apps...
Display("KES", ConsoleColor.Blue, 14)
Sub Display(ByRef Txt As String, ByRef foreg As Integer, backg as Integer)
Console.BackgroundColor = backg
Console.ForegroundColor = foreg
Console.Write(Txt)
End Sub
Sub DisplayLine(ByRef Txt As String, ByRef foreg As Integer, backg as Integer)
Console.BackgroundColor = backg
Console.ForegroundColor = foreg
Console.WriteLine(Txt)
End Sub
Console Tweaks
Console.Title = "App Window Name"
Console.SetCursorPosition(x, y)
Console.Clear()
Console.SetWindowSize(40, 40)
Random Numbers
Dim RandomN As New Random
XXXXX = RandomN.Next(First Number, 1 higher than last number)
To deal a Pack of Cards...
Dim Deck(51) As Integer
Dim Suites As New List(Of String) From {"Hearts", "Clubs", "Diamonds", "Spades"}
Dim Cards As New List(Of String) From {"Ace", "Two", "3", "4", "5", "6", "7", "8", "Nine", "Ten", "Jack", "Queen", "King"}
Dim RandomN As New Random
Dim card, suite As Integer
card = RandomN.Next(1, 14) - 1
suite = RandomN.Next(1, 5) - 1
For i = 1 To 52
Do Until Deck((suite * 13) + card) = 0
card = RandomN.Next(1, 14) - 1
suite = RandomN.Next(1, 5) - 1
Loop
If suite = 0 Or suite = 2 Then
DisplayLine(Cards(card) & " of " & Suites(suite), 2, 7)
Else
DisplayLine(Cards(card) & " of " & Suites(suite), 5, 7)
End If
Deck((suite * 13) + card) = 1
Next
More on Arrays Read this Document
Input / Output
Whilst Console.Readline() Console.WriteLine() have been used in past Comp1 Exams... Try...
name = InputBox("Enter Your Name")
MsgBox("This is" & name & "'s first VB Window")
Arrays - 1D String
Dim Suite As New List(Of String) From {"Hearts", "Clubs", "Diamonds", "Spades"}
Delay Timer.. Delay(0.25)
Sub Delay(ByVal Secs As Decimal)
Dim StartTime As Date = Now
Do Until (Now - StartTime).TotalSeconds > Secs
Loop
End Sub
Move / Draw
Sub Move(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer, ByVal steps As Integer, ByVal delay As Decimal, ByVal Symbol As String, ByVal Path As String)
Dim xpos As Decimal
Dim ypos As Decimal
Dim L2 As Decimal
L2 = 0
Do Until L2 >= steps
xpos = Int(0.5 + x1 + ((L2 / steps) * (x2 - x1)))
ypos = Int(0.5 + y1 + ((L2 / steps) * (y2 - y1)))
Console.SetCursorPosition(xpos, ypos)
Console.Write(Symbol)
TimeDelay(delay)
Console.SetCursorPosition(xpos, ypos)
Console.Write(Path)
L2 += 1
Loop
End Sub
Rectangle
Sub Rectangle(ByVal XStart As Integer, ByVal YStart As Integer, ByVal XEnd As Integer, ByVal YEnd As Integer, ByVal Steps As Integer, ByVal Delay As Decimal, ByVal Symbol As String, ByVal Path As String)
Move(XEnd, YStart, XStart, YStart, Steps, Delay, Symbol, Path)
Move(XStart, YStart, XStart, YEnd, Steps, Delay, Symbol, Path)
Move(XStart, YEnd, XEnd, YEnd, Steps, Delay, Symbol, Path)
Move(XEnd, YEnd, XEnd, YStart, Steps, Delay, Symbol, Path)
End Sub
File Input (See also [& for Output] AS book page 76/77 and VBnet6-10Console page 55- (Topic 9)
Needed before the Module1 command...
Option Explicit On
Imports System.IO
ReadFile("R:\Subjects\InfoTech\Year1213\Year12-Comp1\20-HiScores.txt") < Can be used to test (inside Sub Main)
Sub ReadFile(ByVal FileName As String)
Dim CurrentFileReader As StreamReader
Dim TextString, FirstString As String
Dim K As Byte
CurrentFileReader = New StreamReader(FileName)
For K = 0 To 9 REM this is to read 10 lines - you can change the values!
TextString = CurrentFileReader.ReadLine() REM read a whole line
FirstString = TextCsvSplitter(TextString)
Console.WriteLine(FirstString)
Console.WriteLine(TextString)
Next
End Sub
Function TextCsvSplitter(ByRef MainString As String)
Dim I As Single
Dim StartString As String
I = 1
Do Until Mid$(MainString, I, 1) = "," REM Will loop forever or generate an error if no comma!
I = I + 1
Loop
REM Text before the comma is placed in FirstSting and the remainder in RestSting
StartString = Mid$(MainString, 1, I - 1)
MainString = Mid$(MainString, I + 1, Len(MainString) - (I - 1))
Return StartString
End Function |