在WINCE中的一些VB.NET2005通用方法

前端之家收集整理的这篇文章主要介绍了在WINCE中的一些VB.NET2005通用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.将全角数字转换成半角数字

2.字符串长度计算(针对全半角混合字符串)

3.动态给控件追加事件(使用递归调用实现全部控件的遍历)

4.时间格式(11:12)控件的自动输入实现

5.动态控制光标在TEXTBox控件中的移动
6.使用API函数播放WAV声音.
7.使用API函数实现文件的创建,移动,删除.


1.将全角数字转换成半角数字

Public Function ZenkakuNumToHankakuNum(ByRef strNum As String) As Boolean
Dim dblValue As Double = 0.0
ZenkakuNumToHankakuNum = True
For cnt As Integer = 0 To strNum.Length - 1
dblValue = Char.GetNumericValue(strNum.Chars(cnt))
If dblValue < 0 Then
ZenkakuNumToHankakuNum = False
Continue For
End If
strNum = strNum.Replace(strNum.Chars(cnt),CType(dblValue,Integer))
Next
End Function

2.字符串长度计算(针对全半角混合字符串)

Public Function TextBoxDataLengthChk(ByVal textBox As TextBox) As Boolean
Dim strData As String = textBox.Text.Trim()
Dim cntLength As Integer = 0
For cnt As Integer = 0 To strData.Length - 1
'半角文字、全角文字の長さ計算する
cntLength = cntLength + System.Text.Encoding.Default.GetByteCount(strData.Chars(cnt))
Next
If cntLength > textBox.MaxLength Then
Return False
End If
Return True
End Function

3.动态给控件追加事件(使用递归调用实现全部控件的遍历)

Private Sub AddEventToControls(ByVal con As Control)
Dim subCon As Control
For Each subCon In con.Controls
If TypeOf subCon Is Panel Then
AddEventToControls(subCon)
End If
If TypeOf subCon Is TextBox Then
AddHandler subCon.GotFocus,AddressOf SoftKeyBoardForm_GotFocus
End If
Next
End Sub

4.时间格式(11:12)控件的自动输入实现

Protected Sub txtTimeControl_KeyDown(ByVal sender As System.Object,ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
Dim textBox As TextBox
Dim intSelectionStart As Integer = 0
Dim strTextBox As String = ""
If TypeOf sender Is TextBox Then
textBox = CType(sender,TextBox)
intSelectionStart = textBox.SelectionStart
strTextBox = textBox.Text
'キーの範囲チェック(0-9)
If e.KeyValue >= 48 And e.KeyValue <= 57 Then
If intSelectionStart = 2 Then
textBox.SelectionStart = 3
ElseIf textBox.Text.Length = 5 And intSelectionStart < textBox.MaxLength Then
textBox.Text = strTextBox.Substring(0,intSelectionStart) & strTextBox.Substring(intSelectionStart + 1,textBox.Text.Length - intSelectionStart - 1)
textBox.SelectionStart = intSelectionStart
End If
Else
textBox.SelectionLength = 0
End If
End If
End Sub

5.动态控制光标在TEXTBox控件中的移动

Public Sub FocusMoveInControl(ByVal moveDirection As String)
Dim txtTextBox As TextBox
txtTextBox = CType(GetCurrentControlByIndex(frmCurrentActiveForm,intCurrentControlIndex),TextBox)
' 左へ移行する
If moveDirection.Equals("left") Then
If txtTextBox.SelectionStart > 0 Then
txtTextBox.SelectionStart = txtTextBox.SelectionStart - 1
End If
' 右へ移行する
ElseIf moveDirection.Equals("right") Then
If txtTextBox.SelectionStart < txtTextBox.Text.Length Then
txtTextBox.SelectionStart = txtTextBox.SelectionStart + 1
End If
End If
txtTextBox.Focus()
End Sub

'根据控件的索引遍历得到控件

Private Function GetCurrentControlByIndex(ByVal con As Control,ByVal intControlIndex As Integer) As Control
Dim control As Control = Nothing
For Each control In con.Controls
If intControlIndex = control.TabIndex Then
conValide = control
Exit For
End If
If TypeOf control Is Panel Then
GetCurrentControlByIndex(control,intControlIndex)
End If
Next
Return conValide
End Function

6.使用API函数播放WAV声音.
Private Declare Function WCE_PlaySound Lib "CoreDll.dll" Alias "PlaySound" _
(ByVal szSound As String,ByVal hMod As IntPtr,ByVal flags As Integer) As Integer
Public Sub PlaySound(ByVal strSoundFile As String)
Try
WCE_PlaySound(strSoundFile,2)
Catch ex As Exception

End Try
End Sub

7.使用API函数实现文件的创建,删除.
#Region "常数定义"
Private GENERIC_READ As Integer = &H80000000
Private GENERIC_WRITE As Integer = &H40000000
Private OPEN_EXISTING As Integer = 3
Private OPEN_NO_EXISTING As Integer = 1
Private FILE_FLAG_OVERLAPPED As Integer = &H40000000
Private iMode As Integer = Convert.ToInt32(FILE_FLAG_OVERLAPPED)
#End Region
#Region "STRUCT"
' This is the OverLapped structure used by the calls to the Windows API.
Private Structure OVERLAPPED
Private Internal As Integer
Private InternalHigh As Integer
Private Offset As Integer
Private OffsetHigh As Integer
Private hEvent As Integer
End Structure
#End Region
#Region "API"
Private Declare Function CreateFile Lib "CoreDll.dll" ( _
ByVal lpFileName As String,_
ByVal dwDesiredAccess As Integer,ByVal dwShareMode As Integer,_
ByVal lpSecurityAttributes As Integer,_
ByVal dwCreationDisposition As Integer,_
ByVal dwFlagsAndAttributes As Integer,_
ByVal hTemplateFile As Integer) As Integer

Private Declare Function WriteFile Lib "CoreDll.dll" ( _
ByVal hFile As Integer,ByVal Buffer As Byte(),_
ByVal nNumberOfBytesToWrite As Integer,_
ByRef lpNumberOfBytesWritten As Integer,_
ByRef lpOverlapped As OVERLAPPED) As Integer

Private Declare Function CloseHandle Lib "CoreDll.dll" ( _
ByVal Handle As Integer) As Int32

Private Declare Function MoveFileW Lib "CoreDll.dll" Alias "MoveFile" ( _
ByVal src As String,ByVal dst As String) As Boolean
Private Declare Function CeDeleteFile Lib "CoreDll.dll" Alias "DeleteFile" ( _
ByVal strFileName As String) As Boolean
#End Region

Public Sub CreateFile(ByVal strFileName As String,ByVal bytText() As Byte)
Dim hFile As Integer = 0
Dim overLapped1 As OVERLAPPED = New OVERLAPPED()

Try
hFile = CreateFile(strFileName,GENERIC_READ Or GENERIC_WRITE,_
OPEN_NO_EXISTING,iMode,0)
WriteFile(hFile,bytText,bytText.Length,overLapped1)
CloseHandle(hFile)
Catch ex As Exception

End Try
End Sub


Public Sub MoveFile(ByVal strFileNameS As String,ByVal strFileNameE As String)
Try
MoveFileW(strFileNameS,strFileNameE)
Catch ex As Exception

End Try
End Sub

Public Sub DeleteFile(ByVal strFileName As String) Try CeDeleteFile(strFileName) Catch ex As Exception WriteErrLog("DeleteFile:",ex.Message) End Try End Sub

原文链接:https://www.f2er.com/vb/262385.html

猜你在找的VB相关文章