Wednesday, August 10, 2011 | By: nika perales

Open PDF File using Vb6

Code snippet to open a pdf file at a click of a button in vb6

Dim acroRdrPath As String, pdfFilePath As String
Private Sub Command1_Click()

acroRdrPath = "C:\Program Files\Adobe\Acrobat 5.0\Reader\AcroRd32.exe" 

pdfFilePath = "c:\my documents\260_2C05.pdf"



Shell acroRdrPath & " " & pdfFilePath , vbNormalFocus

End Sub
 

XHTML: Open link in new window

the target attribute has been deliberately removed from xhtml, thus making it difficult for people getting started creating websites using xhtml unable to open links in new windows like in html. Here's the code to help you open a target link in a new window

add this to the head or to an external file which you can reference
function targetBlank (url) {
  blankWin = window.open(url,'_blank','menubar=yes,toolbar=yes,location=yes,directories=yes,fullscreen=no,titlebar=yes,hotkeys=yes,status=yes,scrollbars=yes,resizable=yes');
}
 

then add the onlick event like this
The test domain in a new window
Tuesday, July 26, 2011 | By: nika perales

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.


'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
 

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.

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")
Monday, July 25, 2011 | By: nika perales

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.

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