自动完成的combobox

landlordh 2004-07-22 10:18:45
根据论坛中相关的文章,我得到以下的代码,只能文本自动完成,但是索引不能自动完成,大家来看看吧,(自动完成的combobox)
Public Sub ComboAutoCom(ByVal cob As ComboBox)

Dim sTypedText As String
Dim iFoundIndex As Integer
Dim oFoundItem As Object
Dim sFoundText As String
Dim sAppendText As String

'Get the Typed Text and Find it in the list
sTypedText = cob.Text
iFoundIndex = cob.FindString(sTypedText)

'If we found the Typed Text in the list then Autocomplete
If iFoundIndex >= 0 Then
'Get the Item from the list (Return Type depends if Datasource was bound
' or List Created)
oFoundItem = cob.Items(iFoundIndex)
'Use the ListControl.GetItemText to resolve the Name in case the Combo
' was Data bound
sFoundText = cob.GetItemText(oFoundItem)
'Append then found text to the typed text to preserve case
sAppendText = sFoundText.Substring(sTypedText.Length)
cob.Text = sTypedText.ToString & sAppendText.ToString
'cob.SelectedIndex = iFoundIndex '这句是我加的,好像不起作用
'Select the Appended Text
cob.SelectionStart = sTypedText.Length
cob.SelectionLength = sAppendText.Length
End If
End Sub
...全文
190 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
landlordh 2004-07-23
  • 打赏
  • 举报
回复
我想如果在函数里找到索引时就自动将selectedindex=找到的索引
可是这样同样引发一个问题就是要么它会是 0 或者是 匹配的第一个字的索引号
那么这样就不算是自动完成索引

.......................
public sub autocombox()
...
end sub

private sub 123(...) handles combobox1.textchanged
autocombox()
end sub

private sub 456(...) handles combobox1.gotfoucs,combobox.click
send.key("{f4}")
end sub

private sub 7_click(...) handles button1.click
‘填充数据,需用到combobox1.selectedvalue.tostring
end sub
.......................


???


landlordh 2004-07-23
  • 打赏
  • 举报
回复
我试过,好像还是不行哦
AntingZ 2004-07-23
  • 打赏
  • 举报
回复
要自动完成索引可以在ComboBox的KeyDown事件中把DroppedDown设置成True
e.g.

Private Sub ComboBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ComboBox1.KeyDown, ComboBox2.KeyDown

CType(sender, ComboBox).DroppedDown = True

End Sub
landlordh 2004-07-23
  • 打赏
  • 举报
回复
没有人顶呀???
landlordh 2004-07-23
  • 打赏
  • 举报
回复
自已搞定了!
水如烟 2004-07-22
  • 打赏
  • 举报
回复
好象没多大的作用
landlordh 2004-07-22
  • 打赏
  • 举报
回复
哈哈,我也是试了很久才行的,主要是选好激活事件,
我选的是textchanged事件
得到焦点或点击时自动下拉combobox,
现在发现selectindex不能自动完成,很遗憾这个不能实现
或者,你们发现有更好的办法吗?

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧