本人在作一个数据库录入界面时候,根据用户的需求ComboBox需要做成在获得焦点,自动弹出下拉列表,用户只用通过数字键,输入数据对应的编号,当达到编号满足的位数时,自动选择编号所对应的数据,然后焦点传过下一个TabIndex,例:在“优势树种”字段中有“红松”“云杉”“白杨”三种数,红松对应的编号为1,云杉对的编号为2,白杨编号为3,当代表“优势树种”的ComboBox获得焦点时,自动开打下拉列表,显示三个树种,这时我键入数字键2,编号的最大位数为1,满足了编号位数,则在ComboBox框中显示云杉,同时传出焦点。功能已基本实现,但问题在于,在送出焦点后,ComboBox框内必不是总会正常按预想的显示“云杉”贴出部份代码,请高手们多多赐教:
1。ComboBox装入数据代码:
'将数据转换显示为对应下列表框信息
Public Sub AddInfo(ByVal aCtl As Control, ByVal aTabInfo As String)
'aTabInfo 为表名,aCtl 为控件名即ComboBox名
Dim rsInfo As New ADODB.Recordset
Dim StrSQL As String
StrSQL = "Select * From " & aTabInfo
Set rsInfo = g_connAdo.Execute(StrSQL)
'rsInfo!Desc为数据名称,即“云杉”,rsInfo!SeqId为数据编号,即2
aCtl.Clear
Do While Not rsInfo.EOF
aCtl.AddItem Trim(rsInfo!Desc)
aCtl.ItemData(aCtl.ListCount - 1) = rsInfo!SeqId
rsInfo.MoveNext
Loop
2。获得焦点后,自动打开下拉列表,在调用的API函数
'代码打开CBO下拉框
Public Const CB_SHOWDROPDOWN = &H14F
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
' 如果想打开下拉列表,使用:
' SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, CLng(0)
' 如果想关闭下拉列表,使用:
' SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 0, CLng(0)
在上一个控件失去后调用GotFocus事件
Private Sub cboTreeDegB_GotFocus()
SendMessage ActiveControl.hwnd, CB_SHOWDROPDOWN, 1, CLng(0)
End Sub
3。记录输入数据函数
在KeyPress事件中调用
Private Sub cboTreeDegB_KeyPress(KeyAscii As Integer)
speedinput ActiveControl, KeyAscii, 1
End Sub
'利用数字键快速输入
Public Sub speedinput(ByVal aCtl As Control, KeyAscii As Integer, Optional Length As Integer)
'm_intSel 为模块级变量,用以记录输入数据
Dim intCnt As Integer
If KeyAscii >= 48 And KeyAscii <= 57 And m_intSel < 999 Then
m_intSel = CLng(CStr(m_intSel) & CStr(Int(Chr(KeyAscii))))
End If
With aCtl
'当输入回车时,显示输入的编号对应值,焦点传到下一个TabIndex
If KeyAscii = 13 Then
For intCnt = 0 To .ListCount - 1
If Int(m_intSel) = .ItemData(intCnt) Then
.ListIndex = intCnt
m_intSel = 0
Exit For
End If
Next
m_intSel = 0
End If
If Len(CStr(m_intSel)) = Length Then
For intCnt = 0 To .ListCount - 1
If Int(m_intSel) = .ItemData(intCnt) Then
.ListIndex = intCnt
m_intSel = 0
Exit For
End If
Next
m_intSel = 0
SendKeys "{TAB}"
End If
End With
KeyAscii = 0
End Sub我现在考虑是否是因为调用了API函数,而倒置数据发生了冲突,而产生无法正常显示
请各位多多指教
1。ComboBox装入数据代码:
'将数据转换显示为对应下列表框信息
Public Sub AddInfo(ByVal aCtl As Control, ByVal aTabInfo As String)
'aTabInfo 为表名,aCtl 为控件名即ComboBox名
Dim rsInfo As New ADODB.Recordset
Dim StrSQL As String
StrSQL = "Select * From " & aTabInfo
Set rsInfo = g_connAdo.Execute(StrSQL)
'rsInfo!Desc为数据名称,即“云杉”,rsInfo!SeqId为数据编号,即2
aCtl.Clear
Do While Not rsInfo.EOF
aCtl.AddItem Trim(rsInfo!Desc)
aCtl.ItemData(aCtl.ListCount - 1) = rsInfo!SeqId
rsInfo.MoveNext
Loop
2。获得焦点后,自动打开下拉列表,在调用的API函数
'代码打开CBO下拉框
Public Const CB_SHOWDROPDOWN = &H14F
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
' 如果想打开下拉列表,使用:
' SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 1, CLng(0)
' 如果想关闭下拉列表,使用:
' SendMessage Combo1.hwnd, CB_SHOWDROPDOWN, 0, CLng(0)
在上一个控件失去后调用GotFocus事件
Private Sub cboTreeDegB_GotFocus()
SendMessage ActiveControl.hwnd, CB_SHOWDROPDOWN, 1, CLng(0)
End Sub
3。记录输入数据函数
在KeyPress事件中调用
Private Sub cboTreeDegB_KeyPress(KeyAscii As Integer)
speedinput ActiveControl, KeyAscii, 1
End Sub
'利用数字键快速输入
Public Sub speedinput(ByVal aCtl As Control, KeyAscii As Integer, Optional Length As Integer)
'm_intSel 为模块级变量,用以记录输入数据
Dim intCnt As Integer
If KeyAscii >= 48 And KeyAscii <= 57 And m_intSel < 999 Then
m_intSel = CLng(CStr(m_intSel) & CStr(Int(Chr(KeyAscii))))
End If
With aCtl
'当输入回车时,显示输入的编号对应值,焦点传到下一个TabIndex
If KeyAscii = 13 Then
For intCnt = 0 To .ListCount - 1
If Int(m_intSel) = .ItemData(intCnt) Then
.ListIndex = intCnt
m_intSel = 0
Exit For
End If
Next
m_intSel = 0
End If
If Len(CStr(m_intSel)) = Length Then
For intCnt = 0 To .ListCount - 1
If Int(m_intSel) = .ItemData(intCnt) Then
.ListIndex = intCnt
m_intSel = 0
Exit For
End If
Next
m_intSel = 0
SendKeys "{TAB}"
End If
End With
KeyAscii = 0
End Sub我现在考虑是否是因为调用了API函数,而倒置数据发生了冲突,而产生无法正常显示
请各位多多指教
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货