Friday, June 27, 2008

URL validation (vb.net)

'URL valid in textbox..


Private Sub txtURL_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtURL.Validated
If txtURL.Text.Trim() <> "" Then

Dim url As String = txtURL.Text.ToString.Trim()
Dim validURL As Boolean = UrlIsValid(url)
If validURL = False Then
MsgBox("Please, Enter Valid URL")
txtURL.Focus()
End If
End If
End Sub

Public Function UrlIsValid(ByVal url As String) As Boolean
If url.ToLower().StartsWith("www.") Then url = "http://" & url
Dim webResponse As Net.HttpWebResponse = Nothing
Try
Dim webRequest As Net.HttpWebRequest = Net.HttpWebRequest.Create(url)
webResponse = DirectCast(webRequest.GetResponse(), Net.HttpWebResponse)
Return True
Catch
Return False
Finally
If webResponse IsNot Nothing Then webResponse.Close()
End Try
End Function

Email Validation in TextBox (vb.net)

Private Sub txtEmailID_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtEmailID.Validated
If txtEmailID.Text.Trim() <> "" Then
Dim emailvl As Boolean = EmailValid(txtEmailID.Text.ToString.Trim())
If emailvl = False Then
MsgBox("Please, Enter Valid Email")
txtEmailID.Focus()
End If
End If
End Sub


Public Function EmailValid(ByVal email As String) As Boolean
' The regular expression rule
Dim Expression As New System.Text.RegularExpressions.Regex("\S+@\S+\.\S+")
' If the email matches the regular expression
If Expression.IsMatch(email) Then
' MessageBox.Show("The email address is valid.")
Return True
Else
' MessageBox.Show("The email address is NOT valid.")
Return False
End If
End Function

Only numeric validation ( vb.net )

'Only numeric enter in textbox
Private Sub txtPhoneNumber_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPhoneNumber.KeyPress
Call KeyPressEventTFNum(e.KeyChar, e)
End Sub


Public Sub KeyPressEventTFNum(ByVal ekeychar As String, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'Dim e2 As System.Windows.Forms.KeyPressEventArgs
Dim valid As Int32 = Reg.NumValid(ekeychar)
If valid = 0 Then
e.Handled = False
End If
If valid = 1 Then
e.Handled = True
End If
End Sub


Public Function NumValid(ByVal key_char As String) As Int32
If (Microsoft.VisualBasic.Asc(key_char) <> 57) Then
'e.Handled = True
handel1 = True
End If
If (Microsoft.VisualBasic.Asc(key_char) = 8) Then
'e.Handled = False
handel1 = False
End If
If handel1 = True Then
Return 1
Else
Return 0
End If
End Function

Text Validation (vb.net)

'for valid text only in textbox
Private Sub txtCountry_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCountry.KeyPress
Call KeyPressEventTF(e.KeyChar, e)
End Sub


Public Sub KeyPressEventTF(ByVal ekeychar As String, ByVal e As System.Windows.Forms.KeyPressEventArgs)

Dim valid As Int32 = CharValid(ekeychar)
If valid = 0 Then
e.Handled = False
End If
If valid = 1 Then
e.Handled = True
End If
End Sub


Public Function CharValid(ByVal key_char As String) As Int32
If (Microsoft.VisualBasic.Asc(key_char) <> 90) _
And (Microsoft.VisualBasic.Asc(key_char) <> 122) Then
'Allowed space
If (Microsoft.VisualBasic.Asc(key_char) <> 32) Then
'e.Handled = True
handel1 = True
End If
End If
' Allowed backspace
If (Microsoft.VisualBasic.Asc(key_char) = 8) Then
'e.Handled = False
handel1 = False
End If
If handel1 = True Then
Return 1
Else
Return 0
End If
End Function

Thursday, June 26, 2008

Create Image by code ( vb.net)

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
''Dim objGraphics As Graphics = System.Drawing.Graphics.FromImage(objBitmap)
Dim objBitmap As New Bitmap(99, 99)

DivisionLogo1.Image = Division.NoImage()
' ''DivisionLogo1.BackColor = Color.White
End Sub


' Load for Noimage in Division image box..
Public Function NoImage() As Bitmap
Dim Text As String = " NoImage"
Dim FontColor As Color = Color.Blue
Dim BackColor As Color = Color.White
Dim FontName As String = "Times New Roman"
Dim FontSize As Integer = 10
Dim Height As Integer = 99
Dim Width As Integer = 99
Dim FileName As String = "MyImage"
Dim objBitmap As New Bitmap(Width, Height)
''Step 5. Create a Graphics object using this Bitmap object.
Dim objGraphics As Graphics = Graphics.FromImage(objBitmap)
''Step 6. Create Color, Font, and PointF objects.
'Dim objColor As Color
Dim objFont As New Font(FontName, FontSize)
'Following PointF object defines where the text will be displayed in the
'specified area of the image
Dim objPoint As New PointF(5.0F, 5.0F)
''Step 7. Create two SolidBrush type objects.
Dim objBrushForeColor As New SolidBrush(FontColor)
Dim objBrushBackColor As New SolidBrush(BackColor)
''Step 8. Draw rectangle using Graphics object and fill it with BackColor.
objGraphics.FillRectangle(objBrushBackColor, 0, 0, Width, Height)
''Step 9. Draw Text string on the specified rectangle using Graphics object.
objGraphics.DrawString(Text, objFont, objBrushForeColor, objPoint)
Return objBitmap
End Function

Image save in SQL server ( vb.net)

'This code is generate Binary data from path of image..
'Programmer can Save image into PC
'And this code can decrease resolution of image ( for Logo)
Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click

OpenFileDialog1.ShowDialog()
dim path as string = OpenFileDialog1.FileName()
If path = "OpenFileDialog1" Then
MsgBox("Select Image As Division Logo")
End If
Dim ext As String = System.IO.Path.GetExtension(path)
retunSTR = Division.ImageLoad(ext, path)
If retunSTR = "f" Then
MsgBox("Please, Select *.jpg , *.jpeg , *bmp , *.png ,*.gif files ")
Else
DivisionLogo1.Load(retunSTR)
End If
End Sub


Public Function ImageLoad(ByVal ext As String, ByVal path As String) As String
Dim ExtFalse As String = ""
If (ext = ".jpg" Or ext = ".jpeg" Or ext = ".bmp" Or ext = ".png" Or ext = ".gif") Then
If path <> "OpenFileDialog1" Then
Dim Imgwidth As Integer = 0
Dim imgHeight As Integer = 0
Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(path)
Imgwidth = curImg.Width
imgHeight = curImg.Height
Dim fnlW As Integer = 100 'Image size and extension validation
Dim fnlH As Integer = 100
Dim tumW As Integer = 100
Dim tumH As Integer = 100
'Dim curFile As String
Dim oldW, oldH, newW, newH, defW, defH, alow, aloh As Integer
Dim kla, klb As Double
'Dim curImg As System.Drawing.Image = System.Drawing.Image.FromFile(curFile)
oldW = Imgwidth
oldH = imgHeight
If oldW > fnlW And oldH > fnlH Then
Dim thumb As System.Drawing.Image
Dim ext1 = curImg.RawFormat
Dim inp As New IntPtr()
kla = oldW / fnlW
klb = oldH / kla
If klb <= fnlH Then
kla = oldH / fnlH
End If
newW = oldW / kla
newH = oldH / kla
Dim imgRszd As New Bitmap(curImg, newW, newH)
curImg.Dispose()
defW = newW - fnlW
defH = newH - fnlH
If defW < 0 Then defW = 0
If defH < 0 Then defH = 0
Dim imgCrpd As New Bitmap(fnlW, fnlH)
Dim myGraphic = Graphics.FromImage(imgCrpd)
alow = defW / 2
aloh = defH / 2
myGraphic.DrawImage(imgRszd, New Rectangle(0, 0, fnlW, fnlH), alow, aloh, fnlW, fnlH, GraphicsUnit.Pixel)
imgCrpd.Save(System.IO.Path.GetFileName(path), ext1)
thumb = imgCrpd.GetThumbnailImage(tumW, tumH, Nothing, inp)
thumb.Save("th_" & System.IO.Path.GetFileName(path), ext1)
Dim path_oldfilenm As String = System.IO.Path.GetFileName(path)
Dim path_newfilenm As String = "th_" & path_oldfilenm
Dim path2 As New StringBuilder(path)
path2.Replace(path_oldfilenm, path_newfilenm)
path = path2.ToString()
imgRszd.Dispose()
myGraphic.dispose()
imgCrpd.Dispose()
curImg.Dispose()
Return path
Else
curImg.Dispose()
Return path
End If
End If
Else
ExtFalse = "f"
Return ExtFalse ' Select extension
End If
Return 1
End Function

For Dotnet Developer

Important site:-
http://www.eggheadcafe.com/