วันอังคารที่ 22 ธันวาคม พ.ศ. 2552

visual basic 6 - code Hilight, Autotab

'Set TAB auto when press key UP, DOWN, RETURN
Public Sub AutoTab(ByVal KeyCode As Integer)
If KeyCode = vbKeyDown Or KeyCode = vbKeyReturn Then
SendKeys "{TAB}", True
ElseIf KeyCode = vbKeyUp Then
SendKeys "+{TAB}", True
End If
End Sub

'Create hilight when textbox are focused
Public Sub HiLight(ByRef txt As Control)
If txt.Enabled = True And txt.Visible = True And (TypeOf txt Is TextBox Or TypeOf txt Is MaskEdBox Or TypeOf txt Is ComboBox) Then
txt.SelStart = 0
txt.SelLength = Len(txt.Text)
End If
End Sub

การใช้งาน

Private Sub Text1_GotFocus()
HiLight Me.ActiveControl
End Sub

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
'จำเป็นต้องจัดลำดับ TabIndex ของ Textbox แต่ละตัวด้วย
AutoTab KeyCode
End Sub

visual basic 6 - code การหาวันที่สิ้นเดือน

Private Sub Command1_Click()
Dim xDate As Date

xDate = DateSerial(Year(Now), Month(Now) + 1, 0)
MsgBox xDate
End Sub

visual basic 6 - code การแปลงจำนวนเงินเป็นตัวอักษร

Private Sub Command1_Click()
MsgBox BahtText(12021.75)
End Sub

Private Function BahtText(ByVal dNumber As Currency) As String
Dim xSatang As String
Dim xBaht As String
Dim ret As String, xNum As String
Dim n1() As String
ret = ""
xNum = Format(dNumber, "0.00")
n1 = Split(xNum, ".")
xBaht = Trim(n1(0))
xSatang = Trim(n1(1))

If Val(xBaht) > 0 Then
ret = NumberToThaiText(xBaht) + "บาท"
End If
If Val(xSatang) > 0 Then
ret = ret & NumberToThaiText(xSatang) + "สตางค์"
Else
ret = ret + "ถ้วน"
End If

BahtText = ret
End Function

Private Function NumberToThaiText(ByVal pNum As String)
Dim arrayUnit() As String, arrayNum() As String
Dim xNumLen As Integer
Dim ch As String, ch_old As String, xNum As String, xUnit As String
Dim ret As String
Dim i As Integer

ret = ""
arrayUnit = Split("แสน,,สิบ,ร้อย,พัน,หมื่น", ",")
arrayNum = Split("ศูนย์,หนึ่ง,สอง,สาม,สี่,ห้า,หก,เจ็ด,แปด,เก้า", ",")
xNumLen = Len(pNum)
For i = xNumLen To 1 Step -1
ch_old = ch
ch = Mid(pNum, xNumLen - i + 1, 1)
xNum = Trim(arrayNum(Val(ch)))
xUnit = Trim(arrayUnit(i Mod 6))
xUnit = IIf(xUnit = "" And i > 6, "ล้าน", xUnit)
If xUnit = "สิบ" And ch = "1" Then
xNum = ""
ElseIf xUnit = "สิบ" And ch = "2" Then
xNum = "ยี่"
ElseIf xUnit = "" And ch = "1" And xNumLen > i And ch_old > "0" Then
xNum = "เอ็ด"
End If
If xNum <> "ศูนย์" Or xUnit = "ล้าน" Then
ret = ret & IIf(ch = "0", "", xNum) & xUnit
End If
Next i

NumberToThaiText = ret
End Function