Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1799067
  • 博文数量: 600
  • 博客积分: 10581
  • 博客等级: 上将
  • 技术积分: 6205
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-06 10:13
文章分类
文章存档

2016年(2)

2015年(9)

2014年(8)

2013年(5)

2012年(8)

2011年(36)

2010年(34)

2009年(451)

2008年(47)

分类:

2009-10-11 10:45:13

Window环境文字的输入(如textbox)都是Insert Mode而不管实际KeyBoard 的状态是
Ins/Repl,以下程式可放在TextBox/ComboBox等Control的Keypress Event中,使得
该Control的文字输入具有OverWrite的功能,而且其最大的文字输入个数不再以Word
为准,而改以Byte为准,符合中文要求。

'以下在form中
Private Sub Text1_KeyPress(KeyAscii As Integer)
Call KeyPress(KeyAscii)
End Sub

Private Sub Combo1_KeyPress(KeyAscii As Integer)
Call KeyPress(KeyAscii, 10)
End Sub

'以下在.bas module中
Declare Function GetKeyState Lib "user32" (ByVal nVirtKey As Long) As Integer

'第一个叁数来自KeyPress Event的KeyAscii
'第二个叁数表示文字最多可输入几个byte,若省略,且文字输入的Control是textbox
'则最多输textbox中Maxlength的个数(by Byte),若为0表不限定输入字数(by Byte)
'第三个叁数用在Control项有与DatasSource 做mapping时,其文字输入的最大长度
'变成table中栏位的最大长度
'传回传1代表keyascii的值被改过(如:超过字串长),0表一切正常
Public Function KeyPress(KeyAscii As Integer, Optional Maxlen As Variant,
Optional dbsource As Control) As Integer
    Dim insmd As Integer, CarStart As Integer
    Dim tmpstr As String
    Dim ctl As Control
     Set ctl = Screen.ActiveControl
    If TypeOf ctl Is TextBox Then
       If ctl.MultiLine Then
          If KeyAscii = vbKeyReturn Then
             KeyPress = 0
             Exit Function
          End If
       End If
    End If
    If KeyAscii = vbKeyBack Then
       KeyPress = 0
       Exit Function
    End If
    If IsMissing(Maxlen) Then
       If TypeOf ctl Is TextBox Then
          If dbsource Is Nothing Or Screen.ActiveControl.DataField = "" Then
             Maxlen = ctl.MaxLength
          Else
             Maxlen = dbsource.Recordset.Fields(Screen.ActiveControl.DataField).Size
          End If
       Else
          Maxlen = 0
       End If
    End If
    insmd = GetKeyState(vbKeyInsert) And vbShiftMask
    If insmd = 0 Then
       If ctl.SelLength = 0 Then
          CarStart = ctl.SelStart
          tmpstr = Mid(ctl.Text, 1, ctl.SelStart)
          tmpstr = tmpstr + Chr(KeyAscii) + Mid(ctl.Text, ctl.SelStart + 2)
          ctl.Text = tmpstr
          KeyAscii = 0
          If strlen(ctl.Text) > Maxlen And Maxlen <> 0 Then
             ctl.Text = SubStr(ctl.Text, 1, Maxlen)
             If strlen(ctl.Text) < Maxlen Then
                ctl.Text = ctl.Text + " "
             End If
             KeyPress = 1
          Else
             KeyPress = 0
          End If
          ctl.SelStart = CarStart + 1
       Else
          KeyPress = 0
       End If
    Else
      If strlen(ctl.Text + Chr(KeyAscii)) > Maxlen And Maxlen <> 0 Then
         KeyAscii = 0
         KeyPress = 1
      Else
         KeyPress = 0
      End If
    End If
End Function
 
阅读(510) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~