Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Umar

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

ValidateForm()

// ValidateForm()

    Private Function ValidateForm() As FormValidity

        Dim MyReturn As New FormValidity
        Dim ReturnString As New StringBuilder
        Dim IsValidForm As Boolean = True

        If (txtFirstName.Text = "") Then
            ReturnString.Append("First Name")
            txtFirstName.Attributes.Add("style", "background-color:#EBECED")
            txtFirstName.Focus()
            IsValidForm = False
        End If

        If (txtLastName.Text = "") Then
            ReturnString.Append(", Last Name")
            txtLastName.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtLastName.Focus()
            IsValidForm = False
        End If

        Dim InputEmail As String = txtEmail.Text
        Dim EmailExpression As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$"
        Dim Re As New Regex(EmailExpression)

        If (Not Re.IsMatch(InputEmail)) Then
            ReturnString.Append(", E-mail Address")
            txtEmail.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtEmail.Focus()
            IsValidForm = False
        End If

        If (txtAdd1.Text = "") Then
            ReturnString.Append(", Address 1")
            txtAdd1.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtAdd1.Focus()
            IsValidForm = False
        End If

        If (txtCity.Text = "") Then
            ReturnString.Append(", City")
            txtCity.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtCity.Focus()
            IsValidForm = False
        End If

        If (ddlState.Text = "") Then
            If (txtStateOther.Text = "") Then
                ReturnString.Append(", State")
                ddlState.Attributes.Add("style", "background-color:#EBECED")
                If (IsValidForm) Then ddlState.Focus()
                IsValidForm = False
            End If
        End If

        If (txtZip.Text = "") Then
            ReturnString.Append(", Zip")
            txtZip.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtZip.Focus()
            IsValidForm = False
        End If

        If (ddlCountry.Text = "") Then
            ReturnString.Append(", Country")
            ddlCountry.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then ddlCountry.Focus()
            IsValidForm = False
        End If

        If (txtPhone.Text = "") Then
            ReturnString.Append(", Phone")
            txtPhone.Attributes.Add("style", "background-color:#EBECED")
            If (IsValidForm) Then txtPhone.Focus()
            IsValidForm = False
        End If

        MyReturn.IsValid = IsValidForm

        If (ReturnString.ToString.StartsWith(",")) Then
            MyReturn.FormFields = ReturnString.ToString.Substring(1)
        Else
            MyReturn.FormFields = ReturnString.ToString
        End If

        Return MyReturn

    End Function

How to get all processes running on a machine

// Take a look at Process.GetProcesses(Environment.MachineName)

   Private Function KillAcrobat() As Boolean
      Dim pAcrobat As Process
      For Each pAcrobat In Process.GetProcesses(Environment.MachineName)
         If pAcrobat.ProcessName = "Acrobat" Then pAcrobat.Kill()
      Next
   End Function

Connection String entry in the web config file

// description of your code here

<connectionStrings>
 <add name="SqlConnectionString"
  connectionString="Data Source=localhost\BAND;Initial Catalog=Players;User ID=band;Password=letmein" />
</connectionStrings>

Adding an entry at runtime in a DropDownList

// description of your code here

DropDownList1.Items.Insert(0, new ListItem("-- All Manufacturers --", "0"));

How to read stuff from file

// description of your code here

public class Readtextfile
{
 public static int Main(string[] args)
 {
   StreamReader re = File.OpenText("Arungg.txt");
   string input = null;
   while ((input = re.ReadLine()) != null)
   {
     Console.WriteLine(input);
   }
   re.close;
   return 0;
 }
}

Walks all files and files in subfolders of the current folder recursively

// Walks all files and files in subfolders of the current folder

   Private Sub WalkAllFiles(ByVal strPath As String, Optional ByVal IgnoreDupes As Boolean = False, Optional ByVal bOnlyPDFs As Boolean = True)
        Dim arDirs() As DirectoryInfo
        Dim oDir As New DirectoryInfo(strPath)
        Dim arFiles(), oFile As FileInfo

        'Get a list of files for the current directory'

        Try
            If bOnlyPDFs Then
                arFiles = oDir.GetFiles("*.pdf")
            Else
                arFiles = oDir.GetFiles("*.*")
            End If
            For Each oFile In arFiles
                sbpMain.Text = "Adding " + oFile.FullName
                Try
                    m_colWalkFiles.Add(oFile.FullName)
                Catch ex As System.Exception
                    If Not IgnoreDupes Then Throw New ArgumentException("Duplicate File.")
                End Try
            Next
        Catch
            //Discard the error
        End Try
        /*Process all files in subdirectories (recurse)*/
        arDirs = oDir.GetDirectories()
        For Each oDir In arDirs
            WalkAllFiles(oDir.FullName, IgnoreDupes, bOnlyPDFs)
        Next
    End Sub
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS