Bloggging about .NET tips, tricks, and so much more...!

Thursday, November 22, 2007

VB.NET Code to Send Mail

Private Sub SendEmail_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SendEmail.Click


Dim mailman As New Chilkat.MailMan() ' Any string passed to


UnlockComponent begins a 30-day trial.
mailman.UnlockComponent("anything")
mailman.SmtpHost = "smtp.earthlink.net"

Dim email As New Chilkat.Email()
email.Subject = "This is the subject"
email.Body = "This is the body"
email.AddTo("Chilkat Support", "support@chilkatsoft.com")
email.From = "John Smith john.smith@chilkatsoft.com"

If (Not mailman.SendEmail(email)) Then
MsgBox(mailman.LastErrorXml)
Else
MsgBox("Mail Sent!")
End If

End Sub

Control Array using VB.NET 2005

Public Class clsControlArray
Public Shared Function getControlArray(ByVal frm As Windows.Forms.Form, ByVal controlName As String) As System.Array
Dim i As Short
Dim startOfIndex As Short
Dim alist As New ArrayList
Dim controlType As System.Type

Dim ctl As System.Windows.Forms.Control
Dim strSuffix As String
Dim maxIndex As Short = -1 'Default

'Loop through all controls, looking for controls with the matching name pattern
'Find the highest indexed control
Dim allControls As ArrayList = GatherAllControls(frm)

For Each ctl In allControls
startOfIndex = ctl.Name.ToLower.IndexOf(controlName.ToLower)
If startOfIndex = 0 Then
strSuffix = ctl.Name.Substring(controlName.Length)
If IsInteger(strSuffix) Then 'Check that the suffix is an integer (index of the array)
If Val(strSuffix) > maxIndex Then maxIndex = Val(strSuffix) 'Find the highest indexed Element
End If
End If
Next ctl

Return alist.ToArray(GetType(Control))
End Function

Private Shared Function getControlFromName(ByRef frm As Windows.Forms.Form, ByVal controlName As String, ByVal index As Short) As System.Windows.Forms.Control
controlName = controlName & index
For Each ctl As Control In frm.Controls
If String.Compare(ctl.Name, controlName, True) = 0 Then
Return ctl
End If
Next ctl

Return Nothing
End Function

Private Shared Function getControlFromName(ByVal allControls As ArrayList, ByVal controlName As String, ByVal index As Short) As System.Windows.Forms.Control

controlName = controlName & index
For Each ctl As Control In allControls
If String.Compare(ctl.Name, controlName, True) = 0 Then
Return ctl
End If
Next ctl

Return Nothing
End Function

Private Shared Function IsInteger(ByVal Value As String) As Boolean
If Value = "" Then Return False

For Each chr As Char In Value
If Not Char.IsDigit(chr) Then
Return False
End If
Next
Return True
End Function

End Class

Wednesday, November 21, 2007

Module modKeys: Easy way to manipulate key event using this class module in VB .NET 2005

Module modKeys
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Integer, ByVal dwExtraInfo As Integer)
Public Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer

Public Const KEYEVENTF_KEYDOWN As Short = &H0S
Public Const KEYEVENTF_KEYUP As Short = &H2S

Public Enum EnumCharType
vbAlphaOnly = 0 'type for Characters only
vbNumericOnly = 1 'type for numeric only
vbAlphanumeric = 2 'Allow Alphanumeric
vbOnListOnly = 3
End Enum

Public Enum EnumKeyExec
vbEnterOnly = 0 'this would only allow enter key
vbBackSpaceOnly = 1 'allow backspace only
vbSpaceOnly = 2 'allow space only
vbEnterBackSpace = 3 'allow both backspace and enter
vbEnterSpace = 4 'allow enter and space
vbSpaceBackSpace = 5 'allow space and backspace
vbDefaultKeys = 6 'allow space,enter and backspace
End Enum

Public Sub AutoShiftTab()
keybd_event(System.Windows.Forms.Keys.ShiftKey, MapVirtualKey(System.Windows.Forms.Keys.ShiftKey, 0), KEYEVENTF_KEYDOWN, 0)
keybd_event(System.Windows.Forms.Keys.Tab, MapVirtualKey(System.Windows.Forms.Keys.Tab, 0), KEYEVENTF_KEYDOWN, 0)
keybd_event(System.Windows.Forms.Keys.ShiftKey, MapVirtualKey(System.Windows.Forms.Keys.ShiftKey, 0), KEYEVENTF_KEYUP, 0)
End Sub

End Module



SAMPLE CODE:
Public Class Form1

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
'//-ALPHA NUMERIC with special characer " .?/@#"
e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbAlphanumeric, EnumKeyExec.vbEnterBackSpace, True, "[ .?/_]"))
Select Case AscW(e.KeyChar)
Case System.Windows.Forms.Keys.Return
AutoTab() '//-focused on next tab order
End Select
End Sub

Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
Select Case e.KeyCode
Case System.Windows.Forms.Keys.Down
AutoTab() '//-focused on next tab order
Case System.Windows.Forms.Keys.Up
AutoShiftTab() '//-focused on previous tab order
End Select
End Sub

Private Sub TextBox4_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox4.KeyPress
'//-SELECTED CHARACTER
e.KeyChar = ChrW(modKeys.Keyed(AscW(e.KeyChar), EnumCharType.vbOnListOnly, EnumKeyExec.vbEnterBackSpace, True, "[RrUuBbEeNn]"))
End Sub

End Class

PRC Board Exam Results

Feeds from Jehzlau Concepts