学习问题参考/解决/保留

自我控制应当成为生活的基调, 美好生活是理性来指导的并且紧紧抓住崇高的理想, 人的一生,就是进行尝试,尝试得越多,生活就越美好!
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

combobx自动填充完整的功能

Posted on 2006-09-05 11:41  下载软件  阅读(314)  评论(0)    收藏  举报

Private Sub LossComboBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles LossComboBox.KeyPress
       Me.AutoComplete(Me.LossComboBox, e, True)
    End Sub

    ' AutoComplete
    Public Sub AutoComplete(ByRef cb As ComboBox, _
        ByVal e As System.Windows.Forms.KeyPressEventArgs, _
        Optional ByVal blnLimitToList As Boolean = False)

        Dim strFindStr As String

        If e.KeyChar = Chr(8) Then 'Backspace
            If cb.SelectionStart <= 1 Then
                cb.Text = ""
                Exit Sub
            End If
            If cb.SelectionLength = 0 Then
                strFindStr = cb.Text.Substring(0, cb.Text.Length - 1)
            Else
                strFindStr = cb.Text.Substring(0, cb.SelectionStart - 1)
            End If
        Else
            If cb.SelectionLength = 0 Then
                strFindStr = cb.Text & e.KeyChar
            Else
                strFindStr = cb.Text.Substring(0, cb.SelectionStart) & e.KeyChar
            End If
        End If

        Dim intIdx As Integer = -1

        ' Search the string in the Combo Box List.
        intIdx = cb.FindString(strFindStr)

        If intIdx <> -1 Then ' String found in the List.
            cb.SelectedText = ""
            cb.SelectedIndex = intIdx
            cb.SelectionStart = strFindStr.Length
            cb.SelectionLength = cb.Text.Length
            e.Handled = True
        Else
            If blnLimitToList = True Then
                e.Handled = True
            Else
                e.Handled = False
            End If
        End If
    End Sub