Wednesday, 5 August 2015

sort string as alphabetical in vb.net

Function sortalphabet(ByVal wrd As String)
        sortalphabet = ""
        Dim myarr() As String
        Dim Length As Integer = Len(wrd)
        ReDim myarr(Length)
        For i = 1 To Length
            Dim charac As String = Mid(wrd, i, 1)
            myarr(i - 1) = charac
        Next
        Dim Sorted As Boolean = False
        Do While Not Sorted
            Sorted = True
            For x = 0 To UBound(myarr) - 1
                If myarr(x) > myarr(x + 1) Then
                    Dim temp As String = myarr(x + 1)
                    myarr(x + 1) = myarr(x)
                    myarr(x) = temp
                    Sorted = False
                End If
            Next
        Loop
        sortalphabet = Join(myarr, Space(0))
    End Function

Saturday, 1 August 2015

Email Send Class in Vb.net

Imports System.Net.Mail
Public Class Emailclass
    Public Function SendEmail(ByVal Recipient As String, _
                      ByVal CopyRecipients As List(Of String), _
                      ByVal FromAddress As String, _
                      ByVal Subject As String, _
                      ByVal Body As String, _
                      ByVal UserName As String, _
                      ByVal Password As String, _
                      ByVal ishtml As Boolean, _
                      ByVal ssl As Boolean, _
                      Optional ByVal Server As String = "smtp.gmail.com", _
                      Optional ByVal Port As Integer = 587, _
                      Optional ByVal Attachments As List(Of String) = Nothing, _
                      Optional ByVal ImgCnts As List(Of String) = Nothing) As Boolean

        Dim Email As New MailMessage()

        Try
            Dim SMTPServer As New SmtpClient
            For Each Attachment As String In Attachments
                Email.Attachments.Add(New Attachment(Attachment))
            Next

            Email.From = New MailAddress(FromAddress)

            Email.To.Add(Recipient)


            For Each CopyRecipient As String In CopyRecipients
                If CopyRecipient <> "" Then
                    Email.CC.Add(CopyRecipient)
                End If
            Next
            Email.Body = Body
            If ishtml = True Then
                For Each ImgCnt As String In ImgCnts
                    If ImgCnt <> "" Then
                        Dim alternateView As AlternateView = alternateView.CreateAlternateViewFromString(Body, Nothing, "text/html")
                        Dim imgpath As String = Trim(Strings.Left(ImgCnt, InStr(ImgCnt, "|") - 1))
                        'Create the LinkedResource here
                        Dim logo As New LinkedResource(imgpath, "image/jpeg")  'Content Type is set as image/jpeg
                        logo.ContentId = Trim(Mid(ImgCnt, InStr(ImgCnt, "|") + 1))
                        logo.TransferEncoding = Net.Mime.TransferEncoding.Base64

                        alternateView.LinkedResources.Add(logo)
                        Email.AlternateViews.Add(alternateView)
                    End If
                Next
            End If
            Email.Subject = Subject
            Email.IsBodyHtml = ishtml
            SMTPServer.Host = Server
            SMTPServer.Port = Port

            SMTPServer.Credentials = New System.Net.NetworkCredential(UserName, Password)

            SMTPServer.EnableSsl = ssl
            SMTPServer.Send(Email)
            MessageBox.Show("email sent successfully")
            Email.Dispose()

            Return True

        Catch ex As SmtpException

            Email.Dispose()
            Return False

        Catch ex As ArgumentOutOfRangeException

            Email.Dispose()
            Return False

        Catch Ex As InvalidOperationException

            Email.Dispose()
            Return False
        End Try
    End Function

End Class

Validate Email in Vb.net

Public Function ValidateEmail(ByVal strEmail As String) As Boolean
        Dim strtmp As String, n As Long, sExt As String
        ValidateEmail = True

        If strEmail = "" Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("error") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("eror") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("websit") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("webside") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("webste") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("webite") Then
            ValidateEmail = False
            Exit Function


        ElseIf strEmail.ToLower.Contains("wqebsite") Then
            ValidateEmail = False
            Exit Function


        ElseIf strEmail.ToLower.Contains("ebsite") Then
            ValidateEmail = False
            Exit Function

        ElseIf strEmail.ToLower.Contains("wesid") Then
            ValidateEmail = False
            Exit Function

        ElseIf strEmail.ToLower.Contains("wewbsite") Then
            ValidateEmail = False
            Exit Function

        ElseIf strEmail.ToLower.Contains("websitw") Then
            ValidateEmail = False
            Exit Function

        ElseIf strEmail.ToLower.Contains("websive") Then
            ValidateEmail = False
            Exit Function


        ElseIf strEmail.ToLower.Contains("websitw") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("webit") Then
            ValidateEmail = False
            Exit Function
        ElseIf strEmail.ToLower.Contains("websi") Then
            ValidateEmail = False
            Exit Function


        ElseIf InStr(1, strEmail, "@") = 0 Then
            ValidateEmail = False
            Exit Function
        ElseIf InStr(1, strEmail, "@") = 1 Then
            ValidateEmail = False
            Exit Function
        ElseIf InStr(1, strEmail, "@") = Len(strEmail) Then
            ValidateEmail = False
            Exit Function
        ElseIf InStr(InStr(1, strEmail, "@") + 1, strEmail, "@") > 0 Then
            ValidateEmail = False
            Exit Function
        ElseIf InStr(1, strEmail, "@.") > 0 Then
            ValidateEmail = False
            Exit Function
        ElseIf Len(strEmail) < 6 Then
            ValidateEmail = False
            Exit Function
        End If
        sExt = ""
        If InStr(InStr(1, strEmail, "@"), strEmail, ".") <> 0 Then
            sExt = Right(strEmail, Len(strEmail) - InStr(InStr(1, strEmail, "@"), strEmail, "."))
        End If
        If sExt = "" Then
            ValidateEmail = False
            Exit Function
        End If

        strtmp = strEmail
        While InStr(1, strtmp, "@") <> 0
            n = 1
            strtmp = Right(strtmp, Len(strtmp) - InStr(1, strtmp, "@"))
        End While
        If n > 1 Then
            ValidateEmail = False
        End If
    End Function

Email Validation check from regex code in vb.net

Imports System.Text.RegularExpressions ' add name space
    Function EmailAddressCheck(ByVal emailAddress As String) As Boolean
        Dim pattern 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 emailAddressMatch As Match = Regex.Match(emailAddress, pattern)
        If emailAddressMatch.Success Then
            EmailAddressCheck = True

        Else
            EmailAddressCheck = False

        End If
    End Function

Monday, 27 July 2015

How to insert Only Numeric Value in Datagridview in vb.net

 Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As         System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
 End Sub
 Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
        If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True
        If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True
        If e.KeyChar = Convert.ToChar(Keys.Back) Then e.Handled = False ' Check back space key
  End Sub

how to find number from string in vb.net

'add name space in vb.net code
Imports System.Text.RegularExpressions  

' Code for find number from string
 Dim x As String = "123a123&*^*&^*&^*&^   a sdsdfsdf"
        MsgBox(Integer.Parse(Regex.Replace(x, "[^\d]", "")))

How to find text change in data grid view and calculate in data grid view in vb.net

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

        With table.Columns
            .Add("Column1", GetType(Integer))
            .Add("Column2", GetType(Integer))
            .Add("Sum", GetType(Integer))
        End With
        Me.DataGridView1.DataSource = table
    End Sub
    Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
        AddHandler e.Control.TextChanged, AddressOf CellTextChanged
    End Sub

    Private Sub CellTextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim txt As DataGridViewTextBoxEditingControl
        Dim col1 As Integer
        Dim col2 As Integer

        Try
            If DataGridView1.CurrentCell.ColumnIndex = 0 Then
                txt = DirectCast(sender, DataGridViewTextBoxEditingControl)
                If txt.Text.Length > 0 Then
                    col1 = Integer.Parse(txt.Text)
                End If
                If DataGridView1.CurrentRow.Cells("Column2").Value & "" <> "" Then
                    col2 = Integer.Parse(DataGridView1.CurrentRow.Cells("Column2").Value)
                End If
            ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then
                txt = DirectCast(sender, DataGridViewTextBoxEditingControl)
                If DataGridView1.CurrentRow.Cells("Column1").Value & "" <> "" Then
                    col1 = Integer.Parse(DataGridView1.CurrentRow.Cells("Column1").Value)
                End If
                If txt.Text.Length > 0 Then
                    col2 = Integer.Parse(txt.Text)
                End If
            End If
            DataGridView1.CurrentRow.Cells("sum").Value = col1 + col2
        Catch ex As Exception
            MessageBox.Show("Input string was not in a correct format.")
        End Try
    End Sub