0% found this document useful (0 votes)
16 views5 pages

Vbnet Cheat Sheet

vb.net cheat sheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Vbnet Cheat Sheet

vb.net cheat sheet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VB.

NET + WinForms Cheat Sheet

Basics
Module Program
Sub Main()
Application.Run(New Form1()) ' Start Form1
End Sub
End Module

Variables & Data Types


Dim i As Integer = 10
Dim d As Double = 5.67
Dim s As String = "Hello"
Dim b As Boolean = True
Const PI As Double = 3.14159

Control Structures
If i > 5 Then
MessageBox.Show("Greater than 5")
Else
MessageBox.Show("Less or equal to 5")
End If

Functions & Subs


Sub ShowMessage(msg As String)
MessageBox.Show(msg)
End Sub

Function Multiply(a As Integer, b As Integer) As Integer


Return a * b
End Function

WinForms Controls
Button -> Button1
TextBox -> TextBox1
Label -> Label1
ComboBox -> ComboBox1
ListBox -> ListBox1
DataGridView -> DataGridView1

Button Example
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Button Clicked!")
End Sub

TextBox Example
Dim input As String = TextBox1.Text
TextBox1.Text = "Hello VB.NET"
Dim charCount As Integer = TextBox1.Text.Length
If TextBox1.Text.Contains("hello") Then MessageBox.Show("Found!")

Label Example
Label1.Text = "New Label Text

ComboBox Example
ComboBox1.Items.Add("Option 1")
ComboBox1.Items.Add("Option 2")
Dim selected As String = ComboBox1.SelectedItem.ToString()

ListBox Example
ListBox1.Items.Add("Item 1")
ListBox1.Items.Add("Item 2")
Dim idx As Integer = ListBox1.FindString("Item 1")
If idx >= 0 Then ListBox1.SelectedIndex = idx

DataGridView (DGV)
DataGridView1.Columns.Add("ID", "ID")
DataGridView1.Columns.Add("Name", "Name")
DataGridView1.Rows.Add(1, "Alice")
DataGridView1.Rows.Add(2, "Bob")
Dim val As String = DataGridView1.Rows(0).Cells("Name").Value.ToString()

Counting Rows/Columns
Dim rowCount As Integer = DataGridView1.Rows.Count - 1
Dim colCount As Integer = DataGridView1.Columns.Count

Looping Rows
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow Then
Console.WriteLine(row.Cells("Name").Value.ToString())
End If
Next

Searching DGV
Dim searchText As String = "Alice"
For Each row As DataGridViewRow In DataGridView1.Rows
If Not row.IsNewRow AndAlso row.Cells("Name").Value.ToString() = searchText Then
row.Selected = True
MessageBox.Show("Found at row " & row.Index)
Exit For
End If
Next

Filtering DGV
Dim dv As New DataView(dt)
dv.RowFilter = "Name LIKE 'A%'"
DataGridView1.DataSource = dv

Counting with LINQ


Dim countA As Integer = (From row As DataGridViewRow In DataGridView1.Rows
Where Not row.IsNewRow AndAlso row.Cells("Name").Value.ToString().StartsWith("A")
Select row).Count()

String Functions
Dim s As String = "VB.NET Cheat Sheet"
Console.WriteLine(s.Length)
Console.WriteLine(s.Contains("VB"))
Console.WriteLine(s.IndexOf("Sheet"))
Console.WriteLine(s.Replace("VB", "C#"))
Console.WriteLine(s.Split(" "c)(1))

File Operations
Dim lineCount As Integer = File.ReadAllLines("test.txt").Length
Dim found = File.ReadAllText("test.txt").Contains("keyword")

Database Search
Dim cmd As New SqlCommand("SELECT COUNT(*) FROM Users WHERE Name LIKE 'A%'", con)
con.Open()
Dim count As Integer = Convert.ToInt32(cmd.ExecuteScalar())
con.Close()

You might also like