1
2
3
4
5
6
7
8
9
| Dim CheckBox1 As New CheckBoxMe.CheckBox1.AutoSize = TrueMe.CheckBox1.Location = New System.Drawing.Point(31, 144)Me.CheckBox1.Name = "CheckBox1"Me.CheckBox1.Size = New System.Drawing.Size(94, 17)Me.CheckBox1.TabIndex = 4Me.CheckBox1.Text = "Remember me"Me.CheckBox1.UseVisualStyleBackColor = TrueMe.Controls.Add(CheckBox1) |
1
2
| ' this code sets the checkbox's text propertyMe.CheckBox1.Text = "Remember me" |
1
2
3
4
5
6
7
8
9
10
11
12
| ' checking if the username and password are validIf Username.Text = "admin" AndAlso Password.Text = "mypwd" Then Dim mForm As Form = MainForm ' instantiate the MainForm mForm.ShowDialog() ' display the main form Me.Close() ' close the login formElse ' incorrect username or password provided Username.Text = String.Empty ' reset the username textbox Password.Text = String.Empty ' reset the password textbox ' let them know that username or password is incorrect MessageBox.Show("Incorrect username or password. Please try again!") |
1
2
3
4
5
6
7
| Private Sub DoSomething() Dim InsideVariable As String = String.Empty; ' Do Something with the variableEnd SubConsole.Write(InsideVariable)' ERROR: variable is not in scope (can't be accessed from outside) |
1
| C = A * B |
| OPERATOR | PURPOSE | EXAMPLE | RESULT |
| + | Addition | 2 + 2 | 4 |
| - | Substraction | 4 - 2 | 2 |
| - | Negation 1 operand* | - 2 | -2 |
| * | Multiplication | 2 * 2 | 4 |
| / | Division | 4 / 2 | 2 |
| \ | Integer Division | 9 \ 2 | 4 |
| ^ | Exponentiation | 2 ^ 3 | 2 * 2 * 2 = 8 |
| Mod | Modulus | 4 Mod 2 | 0 |
1
2
3
4
5
6
7
| For i As Integer = 1 To 10 If i Mod 2 = 0 Then Console.WriteLine(i) ' an even number Else ' we do nothing as the number is odd End IfNext |
2
3
4
5
6
7
| Dim nl As String = Environment.NewLine' say hi and ask them for a nameConsole.WriteLine("Hello there. What's your name?" + nl)Dim input = Console.ReadLine()' say something to the userConsole.WriteLine(nl & "Nice to meet you " & input & "." & nl)Console.Read() |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| Dim prompt As String = String.EmptyDim title As String = String.EmptyDim defaultResponse As String = String.EmptyDim answer As Object' Set prompt.prompt = "Hello there. What's your name?"' Set title.title = "Getting user input"' Set default value.defaultResponse = "Your name here"' Display prompt, title, and default value.answer = InputBox(prompt, title, defaultResponse)' Say something to the userMessagebox.Show("Nice to meet you " & answer) |
1
2
| Dim IsUsernameValid As BooleanIsUsernameValid = Not (Username.Text = "admin") |
1
2
| Dim IsUserValid As BooleanIsUserValid = (Username.Text = "admin") And (Password.Text = "mypwd") |
1
2
| Dim IsUserValid As BooleanIsUserValid = (Username.Text = "admin") AndAlso (Password.Text = "mypwd") |
1
2
| Dim IsPasswordValid As BooleanIsPasswordValid = (Password.Text = "mypass") Or (Password.Text = "mypwd") |
1
2
| Dim IsPasswordValid As BooleanIsPasswordValid = (Password.Text = "mypwd") OrElse (Password.Text = "mypass") |
1
2
| Dim IsUsernameValid As BooleanIsUsernameValid = (Username.Text = "admin") Xor (Username.Text = "user") |
1
2
3
4
5
6
| If booleanExpression = True Then DoSomething()Else DoSomethingElse()End If' NOTE: an if statement is optionally followed by an else clause |
1
2
3
4
5
6
7
8
9
| Dim IsUsernameValid As BooleanIf Username.Text = "user" Then IsUsernameValid = FalseElseIf Username.Text = "admin" IsUsernameValid = TrueElse IsUsernameValid = FalseEnd If' NOTE: an if statement is optionally followed by an ElseIf clause as well |
1
2
3
4
5
| ' make a full name concatenate the first and the last nameDim FullName As String = String.EmptyFullName = FirstName.Text & " " & LastName.Text' test the resultConsole.WriteLine(FullName) |
1
2
3
4
| Dim MyString As String = String.Empty MessageBox.Show(MyString.Length) ' = 0MyString = "Hello World!" MessageBox.Show(MyString.Length) ' = 12 |
1
2
3
| Dim MyString As String = "Hello World!"Dim substring As String = MyString.Substring(0, 5) MessageBox.Show(substring) ' = Hello |
1
2
3
| Dim MyString As String = "Hello World!"Dim substring As String = MyString.Substring(6) MessageBox.Show(substring) ' = World! |
1
2
3
| Dim project As String = "World"Dim result As String = String.Format("Hello {0}!", project)MessageBox.Show(result) ' = Hello World! |
1
2
3
| Dim amount As Decimal = 500Dim result As String = String.Format("{0, -20} {1, -3} dollars", "Thanks for paying me ", amount)Console.WriteLine(result) ' = Thanks for paying me 500 dollars |
1
2
3
| Dim amount As Decimal = 500Dim result As String = String.Format("{0, -20} {1, 10:c}", "Thanks for paying me ", amount)Console.WriteLine(result) ' = Thanks for paying me $500.00 |
1
2
3
4
5
6
7
| Dim OriginalString As String = "Hello User"Dim ModifiedString As String = String.Empty;If OriginalString.Contains("Hello") Then ModifiedString = OriginalString.Replace("Hello", "Good Bye") Console.WriteLine(ModifiedString) ' = Good Bye User"End If |