'Handle Fade in of form Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)_ Handles MyBase.Load fade_in() End Sub 'Handle Fade Out of form Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing fade_out() End Sub 'Fade in Public Sub fade_in() For FadeIn = 0.0 To 1.1 Step 0.1 Me.Opacity = FadeIn Me.Refresh() Threading.Thread.Sleep(100) Next End Sub 'Fade out: Public Sub fade_out() For FadeOut = 90 To 10 Step -10 Me.Opacity = FadeOut / 100 Me.Refresh() Threading.Thread.Sleep(50) Next End Sub
The programmer's notebook is a reference for programmers. Here you can find code snippet's for VB.Net, C# .Net, WPF and many more...
Pages
Simplest Form Fade effect for VB.net
here's a simple fade effect that you can add to your WinForms application. With this code you'll be able to make a form Fade in on load and fade out on close.
How to Export Dataset contents to Excel File
If you have come across the need to export the contents of your dataset in VB.net to an excel file then this code will help you. With this code snippet you will be able to dump the entire contents of the dataset to an excel file.
And here's how you use the code:
Public Sub ExportDatasetToExcel(ByVal ds As DataSet, ByVal strExcelFile As String) Dim conn As New OleDbConnection(String.Format("provider=Microsoft.Jet.OLEDB.4.0; Data Source='{0}';" & "Extended Properties='Excel 8.0;HDR=YES;'", strExcelFile)) conn.Open() Dim strTableQ(ds.Tables.Count) As String Dim i As Integer = 0 'making table query For i = 0 To ds.Tables.Count - 1 strTableQ(i) = "CREATE TABLE [" & ds.Tables(i).TableName & "](" Dim j As Integer = 0 For j = 0 To ds.Tables(i).Columns.Count - 1 Dim dCol As DataColumn dCol = ds.Tables(i).Columns(j) strTableQ(i) &= " [" & dCol.ColumnName & "] varchar(255) , " Next strTableQ(i) = strTableQ(i).Substring(0, strTableQ(i).Length - 2) strTableQ(i) &= ")" Dim cmd As New OleDbCommand(strTableQ(i), conn) cmd.ExecuteNonQuery() Next 'making insert query Dim strInsertQ(ds.Tables.Count - 1) As String For i = 0 To ds.Tables.Count - 1 strInsertQ(i) = "Insert Into " & ds.Tables(i).TableName & " Values (" For k As Integer = 0 To ds.Tables(i).Columns.Count - 1 strInsertQ(i) &= "@" & ds.Tables(i).Columns(k).ColumnName & " , " Next strInsertQ(i) = strInsertQ(i).Substring(0, strInsertQ(i).Length - 2) strInsertQ(i) &= ")" Next 'Now inserting data For i = 0 To ds.Tables.Count - 1 For j As Integer = 0 To ds.Tables(i).Rows.Count - 1 Dim cmd As New OleDbCommand(strInsertQ(i), conn) For k As Integer = 0 To ds.Tables(i).Columns.Count - 1 cmd.Parameters.AddWithValue("@" & ds.Tables(i).Columns(k).ColumnName.ToString(), ds.Tables(i).Rows(j)(k).ToString()) Next cmd.ExecuteNonQuery() cmd.Parameters.Clear() Next Next End Sub
And here's how you use the code:
ExportDatasetToExcel(dsFinal, "d:\\my.xls")
VB.Net: Recursive File Search
VB.Net File Search Class that uses recursion as a way of finding the files the match a specified criteria. Very useful when you need to find specific files within your VB.net application.
And here's how you use the class to search for files.
Option Strict On Option Explicit On Imports System.IO Imports System.Collections.Specialized Public Class FileSearch Private Const DefaultFileMask As String = "*.*" Private Const DefaultDirectoryMask As String = "*" #Region " Member Variables " Private _InitialDirectory As DirectoryInfo Private _DirectoryMasks As StringCollection Private _FileMasks As StringCollection ' Private _Directories As New ArrayList Private _Files As New ArrayList #End Region #Region " Properites " Public Property InitialDirectory() As DirectoryInfo Get Return _InitialDirectory End Get Set(ByVal Value As DirectoryInfo) _InitialDirectory = Value End Set End Property Public Property DirectoryMask() As StringCollection Get Return _DirectoryMasks End Get Set(ByVal Value As StringCollection) _DirectoryMasks = Value End Set End Property Public Property FileMask() As StringCollection Get Return _FileMasks End Get Set(ByVal Value As StringCollection) _FileMasks = Value End Set End Property Public ReadOnly Property Directories() As ArrayList Get Return _Directories End Get End Property Public ReadOnly Property Files() As ArrayList Get Return _Files End Get End Property #End Region #Region " Constructors " Public Sub New() End Sub Public Sub New( _ ByVal BaseDirectory As String) Me.New(New DirectoryInfo(BaseDirectory)) End Sub Public Sub New( _ ByVal InitialDirectory As DirectoryInfo) _InitialDirectory = InitialDirectory End Sub #End Region Public Overloads Sub Search(ByVal InitalDirectory As String, _ Optional ByVal FileMask As String = Nothing, _ Optional ByVal DirectoryMask As String = Nothing) Search( _ New DirectoryInfo(InitalDirectory), _ FileMask, _ DirectoryMask _ ) End Sub Public Overloads Sub Search( _ Optional ByVal InitalDirectory As DirectoryInfo = Nothing, _ Optional ByVal FileMask As String = Nothing, _ Optional ByVal DirectoryMask As String = Nothing) _Files = New ArrayList _Directories = New ArrayList If Not IsNothing(InitalDirectory) Then _InitialDirectory = InitalDirectory End If If IsNothing(_InitialDirectory) Then Throw New ArgumentException("A Directory Must be specified!", "Directory") End If If IsNothing(FileMask) OrElse FileMask.Length = 0 Then _FileMasks = New StringCollection _FileMasks.Add(DefaultFileMask) Else _FileMasks = ParseMask(FileMask) End If If IsNothing(DirectoryMask) OrElse DirectoryMask.Length > 0 Then _DirectoryMasks = New StringCollection _DirectoryMasks.Add(DefaultDirectoryMask) Else _DirectoryMasks = ParseMask(DirectoryMask) End If DoSearch(_InitialDirectory) End Sub Private Sub DoSearch(ByVal BaseDirectory As DirectoryInfo) Try For Each fm As String In _FileMasks Files.AddRange(BaseDirectory.GetFiles(fm)) Next _ Catch u As UnauthorizedAccessException 'Siliently Ignore this error, there isnt any simple 'way to avoid this error. End Try Try Dim Directories As New ArrayList For Each dm As String In _DirectoryMasks Directories.AddRange(BaseDirectory.GetDirectories(dm)) _Directories.AddRange(Directories) Next For Each di As DirectoryInfo In Directories DoSearch(di) Next Catch u As UnauthorizedAccessException 'Siliently Ignore this error, there isnt any simple 'way to avoid this error. End Try End Sub 'Masks are formated like *.jpeg;*.jpg Private Shared Function ParseMask(ByVal Mask As String) As StringCollection If IsNothing(Mask) Then Return Nothing End If Mask = Mask.Trim(";"c) If Mask.Length = 0 Then Return Nothing End If Dim Masks As New StringCollection Masks.AddRange(Mask.Split(";"c)) Return Masks End Function Protected Overrides Sub Finalize() _Files = Nothing _Directories = Nothing MyBase.Finalize() End Sub End Class
And here's how you use the class to search for files.
Dim x As New FileSearch() x.Search(selectedPath, "*.jpg;*.jpeg;*.tif;*.tiff") For Each imageFiles As FileInfo In x.Files 'Print Fullname of the matching files to debug window Debug.Print(imageFiles.FullName) next
Recursive Function in C to generate Fibonacci Numbers
int Fibonacci(int n) { if(n==0||n==1) return 1; else return Fibonacci(n-1)+Fibonacci(n-2); }
How to Minimize Your VB.Net Application to the System Tray
How to Minimize Your VB.Net Application to the System Tray
If you want your VB.Net application to minimize to the system tray instead of closing, then this sample project will help you. First create a windows forms application, Then add the following declarations to your forms code.
The TrayIcon will hold all the information of the type of icon that you want to appear on the system tray. This includes the icon that would appear on the system tray, the text that will display when you hover on the tray icon, as well as the context menu for the tray icon.
Now Add the following code on your Forms Load Event, This will populate the contextmenu with items and add the corresponding click event handler.
After that, create the procedure below
The above code will handle the events fired when you click on the contextmenu items of of your TrayIcon.
Now add the following code which handles the creation of the NotifyIcon Object as well as showing and hiding of the tray icon itself.
Finally, add the following code on your forms FormClosing event. This will trigger the showing of the tray icon and hiding the form from view.
Note: setting E.Cancel = True instructs the system that we want to cancel the closing of the form.
Attached is the completed sample project. Feel Free to send me any questions that you may have...
Download Code
If you want your VB.Net application to minimize to the system tray instead of closing, then this sample project will help you. First create a windows forms application, Then add the following declarations to your forms code.
Private TrayIcon As NotifyIcon Private TrayMenu As ContextMenuStrip Private AppClose As Boolean 'Boolean FLAG which identifies 'that you want to exit the application
The TrayIcon will hold all the information of the type of icon that you want to appear on the system tray. This includes the icon that would appear on the system tray, the text that will display when you hover on the tray icon, as well as the context menu for the tray icon.
Now Add the following code on your Forms Load Event, This will populate the contextmenu with items and add the corresponding click event handler.
Dim mnuShowForm, mnuExit As ToolStripMenuItem Dim mnuSep As New ToolStripSeparator TrayMenu = New ContextMenuStrip mnuShowForm = New ToolStripMenuItem("Open Form") AddHandler mnuShowForm.Click, AddressOf MenuItemClick mnuExit = New ToolStripMenuItem("Exit Program") AddHandler mnuExit.Click, AddressOf MenuItemClick TrayMenu.Items.AddRange(New ToolStripItem() {mnuShowForm, mnuSep, mnuExit})
After that, create the procedure below
Private Sub MenuItemClick(ByVal sender As Object, ByVal e As System.EventArgs) Select Case CType(sender, ToolStripMenuItem).Text Case "Open Form" TrayIcon.Visible = False Me.Show() Case "Exit Program" If Not IsNothing(TrayIcon) Then ShowTrayIcon(False) TrayIcon = Nothing AppClose = True 'tell the program to exit 'this prevents the form 'from hiding itself 'instead of closing End If Application.Exit() End Select End Sub
The above code will handle the events fired when you click on the contextmenu items of of your TrayIcon.
Now add the following code which handles the creation of the NotifyIcon Object as well as showing and hiding of the tray icon itself.
Private Sub ShowTrayIcon(Optional ByVal blnShow As Boolean = True) If TrayIcon Is Nothing Then TrayIcon = New NotifyIcon TrayIcon.Icon = Me.Icon TrayIcon.ContextMenuStrip = TrayMenu TrayIcon.Text = Me.Text End If If blnShow Then TrayIcon.Visible = True Else TrayIcon.Visible = False End If End Sub
Finally, add the following code on your forms FormClosing event. This will trigger the showing of the tray icon and hiding the form from view.
Note: setting E.Cancel = True instructs the system that we want to cancel the closing of the form.
Attached is the completed sample project. Feel Free to send me any questions that you may have...
Download Code
How to close all open forms in your MDI Application in VB.Net
I have came across a situation lately where i needed to close all open forms in my MDI Application when a user clicks on the log-off button. Here's how i did it...
Private Sub CloseOpenForms 'create a copy of the collection of current open forms 'because the Application.OpenForms changes everytime we 'close an open form Dim OpenForms As FormCollection = Application.OpenForms For Each frm As Form In OpenForms 'check if current form is our MDI Parent 'if not then close it, otherwise skip it If Not frm.IsMdiContainer Then frm.Close() End If Next End Sub
WPF Binding Cheat Sheet
Getting started with a programming like WPF pretty much requires having a WPF book handy so as to help you go through simple things about the language. With this WPF Binding CheatSheet you will help your self save the time from having to scan an entire book looking how to bind controls with data as well as with other components in the language, just download this cheat sheet have it printed and have it posted in your cubicle or desk and you're all set to go...
Download Cheat Sheet
Download Cheat Sheet
Subscribe to:
Posts (Atom)