Thursday, 8 December 2011

Textbox accepts only Integer Values

Write the codes below on the keyPress event of TextBox  to make it accepts only Integer Values.

       If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) = True Then
                e.Handled = False
            Else
                e.Handled = True
            End If
        End If


Tuesday, 6 December 2011

Send email with attachment through windows application


Imports System.Net
Imports System.Net.Mail


 1. Using your Gmail Account

Dim MyMailMessage As New System.Net.Mail.MailMessage(Sender's email address, Receiver's email address, Subject, msg)
MyMailMessage.IsBodyHtml = False
Dim attach As New System.Net.Mail.Attachment(path of filename)
MyMailMessage.Attachments.Add(attach)
Dim mailAuthentication As New NetworkCredential(Sender's email Address, Sender's Password)
Dim mailClient As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
mailClient.EnableSsl = True
mailClient.UseDefaultCredentials = False
mailClient.Credentials = mailAuthentication
mailClient.Send(MyMailMessage)
MyMailMessage.Attachments.Dispose()

2. From your yahoo Account

Replace the following lines on the above code

Dim mailClient As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
mailClient.EnableSsl = True

with

Dim mailClient As New System.Net.Mail.SmtpClient("smtp.mail.yahoo.com", 25)
mailClient.EnableSsl = False

Monday, 5 December 2011

Numeric values in Textbox

'Declaring integer variable globally
Dim dot As Integer 

'Key Press event of TextBox1 Field 


Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
     
If e.KeyChar <> ChrW(Keys.Back) Then
            If Char.IsNumber(e.KeyChar) = True Or e.KeyChar = "."c Then
                check()
                If dot = 1 And e.KeyChar = "."c Then
                    e.Handled = True
                End If
            Else
                e.Handled = True
            End If
        End If
    End Sub

'Write function named check inside the class

Private Function check()
        Dim str As String = TextBox1.Text
        If str.Contains(".") = True Then
            dot = 1
        Else
            dot = 0
        End If
        Return dot
    End Function