Sunday, January 22, 2017

How to clear all TextBoxes on a Form in VB.net

This is a tutorial on how to clear all TextBoxes on a form in Visual Basic .NET.
Instead of calling each TextBox by its name and clear them one by one, you can use a For Each loop that clears all the TextBoxes found on the form



Here is the code that clears all TextBoxes text on a form 


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim a As Control


        For Each a In Me.Controls


            If TypeOf a Is TextBox Then


                a.Text = Nothing


            End If


        Next


    End Sub


You can also specify textboxes in a groupbox to be cleared. In the following example we will clear all textboxes in GroupBox1 only




Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
        Dim a As Control
 
        For Each a In Me.GroupBox1.Controls
 
            If TypeOf a Is TextBox Then
 
                a.Text = Nothing
 
            End If
 
        Next
 
    End Sub

What is Microsoft .Net Framework

The Microsoft .Net Framework is a platform that provides tools and technologies you need to build Networked Applications as well as Distributed Web Services and Web Applications. The .Net Framework provides the necessary compile time and run-time foundation to build and run any language that conforms to the Common Language Specification (CLS).The main two components of .Net Framework are Common Language Runtime (CLR) and .Net Framework Class Library (FCL).
The Common Language Runtime (CLR) is the runtime environment of the .Net Framework , that executes and manages all running code like a Virtual Machine. The .Net Framework Class Library (FCL) is a huge collection of language-independent and type-safe reusable classes. The .Net Framework Class Libraries (FCL) are arranged into a logical grouping according to their functionality and usability is called Namespaces. The following lessosns describes how to .Net Framework manages the code in compile time and run time .

Monday, May 19, 2014

Make a screen shot application in vb.net

i am now just going to teach you how to make a simple very small screen shot application in vb.net 

Private Sub Button1_Click(ByVal sender As System.ObjectByVal AsSystem.EventArgs) Handles Button1.Click
        TextBox1.Enabled = False
        Timer1.Start()
    End Sub


Private Sub Timer1_Tick(ByVal sender As System.ObjectByVal As System.EventArgs)Handles Timer1.Tick
        Dim snappit As String = 0
        TextBox1.Text -= snappit + 1
        If textbox1.text = 0 Then
            "skimlinks-unlinked">Me.Hide()
        End If
        If TextBox1.text = -2 Then
            Try
                Dim screenshot As Size = New Size("skimlinks-unlinked">My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim screengrab As New Bitmap("skimlinks-unlinked">My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height)
                Dim As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(screengrab)
                g.CopyFromScreen(New Point(0, 0), New Point(0, 0), screenshot)
                "skimlinks-unlinked">screengrab.Save("snap.jpg")
                MsgBox("your screen has been snapped and the snap was saved", MsgBoxStyle.Information, "SuperScreenShot")
                "skimlinks-unlinked">Me.Close()
            Catch ex As Exception
                MsgBox("sorry unable to snap your screen and save at the moment please try again later", MsgBoxStyle.Critical, "Warning!")
            End Try
        End If
    End Sub
End Class

Friday, June 28, 2013

Folder Watcher in VB.Net


Hi , In this article we're going to learn how to watch the folder activity using VB.Net


 
  Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        ListBox1.Items.Add("Modified: " & e.FullPath)

    End Sub



    Private Sub btnsetpath_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsetpath.Click

        Try

            If FolderBrowserDialog1.ShowDialog() = DialogResult.OK Then



                'select folder

                folder_txt.Text = FolderBrowserDialog1.SelectedPath

                FileSystemWatcher1.Path = folder_txt.Text



            End If

        Catch ex As Exception

            MsgBox(ex.Message, vbCritical, "File System Watcher")

        End Try

    End Sub



    Private Sub FileSystemWatcher1_Created(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created

        'if any file created add fullpath to the listbox

        ListBox1.Items.Add("Created: " & e.FullPath)

    End Sub



    Private Sub FileSystemWatcher1_Deleted(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Deleted



        'if any file deleted add fullpath to the listbox

        ListBox1.Items.Add("Deleted: " & e.FullPath)

    End Sub



    Private Sub FileSystemWatcher1_Renamed(ByVal sender As System.Object, ByVal e As System.IO.RenamedEventArgs) Handles FileSystemWatcher1.Renamed



        'if any file renamed add path to the listbox

        ListBox1.Items.Add("Renamed: " & e.FullPath)

    End Sub 

Wednesday, October 19, 2011

Get Domain, PC Name and IP Address of your local network using VB.Net

Import below namespace in your project:


Imports System.DirectoryServices

Imports System.Net 


Use this functions to get data and store in to Data table :


Private Sub GetIPS()
        Dim child As DirectoryEntry
        Dim Parent As New DirectoryEntry()
        Try
            Parent.Path = "WinNT:"
            For Each child In Parent.Children

                Select Case child.SchemaClassName

                    Case "Domain"

                        Dim ParentDomain As New TreeNode(child.Name)
                        Dim SubChild As DirectoryEntry
                        Dim SubParent As New DirectoryEntry()
                        SubParent.Path = "WinNT://" & child.Name

                        For Each SubChild In SubParent.Children

                            Select Case SubChild.SchemaClassName
                                Case "Computer"
                                    Dim ipadd As String = GetIPAddress(SubChild.Name)
                                    CreateDT(child.Name, SubChild.Name, ipadd)
                            End Select
                        Next
                End Select

            Next

        Catch Excep As Exception
            MsgBox("Error While Reading Directories")
        Finally
            Parent = Nothing
        End Try
    End Sub



    Private Sub CreateDT(ByVal Domain As String, ByVal PCName As String, ByVal IpAddress As String)
        If dt.Rows.Count = 0 Then
            dt.Columns.Add("domain")
            dt.Columns.Add("pcname")
            dt.Columns.Add("ipaddress")
        End If

        Dim dr As DataRow
        dr = dt.NewRow()
        dr("domain") = Domain
        dr("pcname") = PCName
        dr("ipaddress") = IpAddress
        dt.Rows.Add(dr)
    End Sub

  Function GetIPAddress(ByVal CompName As String) As String
        Dim oAddr As System.Net.IPAddress
        Dim sAddr As String
        Try
            With System.Net.Dns.GetHostByName(CompName)
                oAddr = New System.Net.IPAddress(.AddressList(0).Address)
                sAddr = oAddr.ToString
            End With

            GetIPAddress = sAddr
        Catch Excep As Exception
            MsgBox(Excep.Message, MsgBoxStyle.OkOnly, "GET IP ADDRESS")
        Finally

        End Try
    End Function 
Sample Code Download Link : http://www.adrive.com/public/406bd58f991bf0afb884cadc2b78da6a28417082c16938c0bedd505ac224afbd.html

Monday, October 17, 2011

Read from XML file in VB.Net

This is a Sample XML file which we want to read from code :
 

Write below function to read data from file :


Public Sub ReadFromXML()

        Dim _FilePath As String
        Dim _dir As New DirectoryInfo(Application.StartupPath + "\DownloadXML")

        For Each _file As FileInfo In _dir.GetFiles("*.*")
            _FilePath = Application.StartupPath + "\DownloadXML\" + _file.Name.ToString

            Dim m_xmld As XmlDocument
            Dim m_nodelist As XmlNodeList
  

            m_xmld = New XmlDocument()
            'Load the Xml file
            m_xmld.Load(_FilePath)
            'Get the list of name nodes
            m_nodelist = m_xmld.SelectNodes("/XMLInfo/Info")
            For Each m_node As XmlNode In m_nodelist
                Dim Name As String = m_node.Attributes.GetNamedItem("Name").Value
                Dim Address As String = m_node.Attributes.GetNamedItem("Address").Value
            Next
        Next

    End Sub

Upload and Download files from FTP in VB.Net

For Upload and download files from FTP Server you need FTP Detail such as 'FTP Server name', 'User Name' and 'Password'.

First import below namespace in your project

Imports Utilities.FTP

Declare Variables for FTP Detail :

    Dim _HostName As String = "ftp.yourwebsite.com"
    Dim _UserName As String = "yourftpusername"
    Dim _Password As String = "password"
 

Use below function for upload files to FTP :

  Public Sub UploadFileToFTP()
        Dim _FromPath As String
        Dim _ToPath As String
        Dim Dt As New DataTable
        Dim flag As Boolean = False      

        'Create a link to an FtpServer
        Dim ftp As New FTPclient(_HostName, _UserName, _Password)
        Dim _dir As New DirectoryInfo(Application.StartupPath + "\Upload")
      ' Upload multiple files
        For Each _file As FileInfo In _dir.GetFiles("*.*")
            _FromPath = Application.StartupPath + "\Upload\" + _file.Name
            _ToPath = "/UploadedData /" + _file.Name
            'upload a file
            flag = ftp.Upload(_FromPath, _ToPath)
            '' file uploaded then delete

            If flag Then
                _file.Delete()
            End If
        Next
    End Sub


Use below function for Download files from FTP :

Public Sub DownloadXML()
        Try         

            Dim _FromPath As String
            Dim _ToPath As String 
                _FromPath = "/UploadedData /" 

            'Create a link to an FtpServer
            Dim ftp As New FTPclient(_HostName, _UserName, _Password)
            'Get the detailed directory listing of /pub/
            Dim dirList As FTPdirectory = ftp.ListDirectoryDetail(_FromPath)
            'filter out only the files in the list
            Dim filesOnly As FTPdirectory = dirList.GetFiles()
            'download these files
            For Each file As FTPfileInfo In filesOnly
                _ToPath = Application.StartupPath + "\Download\" + file.Filename
                ftp.Download(file, _ToPath, True)
           'Delete file after download
                ftp.FtpDelete(_FromPath + file.Filename)
            Next

        Catch ex As Exception
        End Try

    End Sub