Friday, January 27, 2012

Checking if a directory exists and creating one in VB.NET

This tutorial contains you the necessary code to check if a directory exists and to create one if it returns false using VB.NET.

Code:

Imports System.IO


If Directory.Exists("c:\mydir") Then
        MessageBox.Show("Directory exists!")
Else
        Directory.CreateDirectory("c:\mydir")

        MessageBox.Show("Directory created!")
End If

Thursday, January 26, 2012

Minimize a VB.NET application to system tray using NotifyIcon

This tutorial is about using the NotifyIcon in VB.NET to minimize your application o the system tray that can be very usefull in applications that run as a server in background for instance.

Form screenshot:

VB.NET Form

Code:

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Try
            If Me.WindowState = FormWindowState.Minimized Then
                Me.Visible = False
                NotifyIcon1.Visible = True
                NotifyIcon1.ShowBalloonTip(1, "NotifyIcon", "Running Minimized", ToolTipIcon.Info)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon1.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        Try
            Me.Visible = True
            Me.WindowState = FormWindowState.Normal
            NotifyIcon1.Visible = False
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
End Class


Video Tutorial:

Saturday, January 21, 2012

For Loop VB.NET / C#

This time i leave another very usefull loop, the "For Loop", in both VB.NET and C#.
Enjoy.


C#:
  for (int i = 0; i <= 5; i++)
  {
     //Do your code here
  }


VB.NET:
  Dim i As Integer
  For i = 0 To 5
      'Do your code here
  Next i
 

Monday, January 16, 2012

Switch Case / Select Case VB.NET / C#

I needed to remember the syntax of the switch / case for the .net languages and then i decided to share them with you here too so you can learn or remember them too.

C#:
  switch (option)
    {
        case "option1":
            // code for case "option1"
            break;
        case "option2":
            // code for case "option2"
            break;
        default:
            //code for every other options
            break;
    }

VB.NET:
Select Case option
    Case "option1"
          ' code for case "option1"
    Case "option2"
          ' code for case "option2"
    Case Else 
          ' code for every other options
End Select