TextBox AutoComplete无法正常工作 [英] TextBox AutoComplete not working

查看:94
本文介绍了TextBox AutoComplete无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我遇到的问题是,无论我做什么,我都无法工作。



这是我的代码,它每次按下一个键时都会尝试搜索数据库,当文本框的长度大于3时,它会正常工作,直到撤消数据的部分但它从不在文本框下方显示结果,我做错了什么?,这是
应该是一件容易的事。我正在使用VS2008和.Net 3.5

 Public Class autocompleteTextbox 
Private SearchStr As String
Private DataCollection As New AutoCompleteStringCollection

Private Sub autocompleteTextbox_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load
TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub

Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection)
Dim connetionString As String = Nothing
Dim connection As SqlConnection
Dim command作为SqlCommand
Dim adapter As New SqlDataAdapter()
Dim dt As New DataTable
connetionString =" server = #######; initial catalog = #####; TrustServerCertificate = yes;加密=是;连接超时= 30;用户= #####;密码= #####"
Dim sql As String =" SELECT cve_cve +' - '+ cve_desc as cve_desc FROM CATS where [cve_desc] like'%" &安培; TextBox1.Text.Trim& "%""
connection = New SqlConnection(connetionString)
尝试
connection.Open()
command = New SqlCommand(sql,connection)
adapter.SelectCommand = command
adapter.Fill(dt)
adapter.Dispose()
command.Dispose()
connection.Close()
dataCollection.Clear()
For Each row As DataRow In dt.Rows
dataCollection.Add(row(0).ToString())
Next
Catch ex As Exception
MessageBox.Show("无法打开连接) !)
End Try
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles TextBox1.TextChanged
如果TextBox1.Text.Length> = 3那么
getData(DataCollection)
TextBox1.AutoCompleteCustomSource = DataCollection
结束如果

结束次级
结束等级




G.Waters

解决方案

我有
MSDN代码示例
你可以查看哪些使用MS-Access但是相同的逻辑适用于SQL-作为OleDb和SqlClient的服务器数据提供者具有相同的对象和方法,例如SqlDataAdapter与OleDbDataAdapter等动物相同。


代码示例中的片段,我使用绝对最小代码对TextBox进行自动完成。



----



在旁注,诸如TextBox之类的控件的自动完成方面是一个封闭的系统,所以如果你想查看当前的搜索/过滤器,你就不能。



Hi,

I have the problem that I just can't make it work, no matter what I do.

This is my code, it tries to search into database everytime a key is pressed and when the textbox's lenght is greater than 3, it works ok until the part that retreives the data but it never shows the results below the textbox, what am I doing wrong?, this was supposed to be an easy task. I am using VS2008 and .Net 3.5

Public Class autocompleteTextbox
    Private SearchStr As String
    Private DataCollection As New AutoCompleteStringCollection

    Private Sub autocompleteTextbox_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TextBox1.AutoCompleteMode = AutoCompleteMode.Suggest
        TextBox1.AutoCompleteSource = AutoCompleteSource.CustomSource
    End Sub

    Private Sub getData(ByVal dataCollection As AutoCompleteStringCollection)
        Dim connetionString As String = Nothing
        Dim connection As SqlConnection
        Dim command As SqlCommand
        Dim adapter As New SqlDataAdapter()
        Dim dt As New DataTable
        connetionString = "server=#######;initial catalog =#####;TrustServerCertificate=yes;Encrypt=yes;Connection Timeout=30;User=#####;Password=#####"
        Dim sql As String = "SELECT cve_cve+' - '+cve_desc as cve_desc FROM CATS where [cve_desc] like '%" & TextBox1.Text.Trim & "%'"
        connection = New SqlConnection(connetionString)
        Try
            connection.Open()
            command = New SqlCommand(sql, connection)
            adapter.SelectCommand = command
            adapter.Fill(dt)
            adapter.Dispose()
            command.Dispose()
            connection.Close()
            dataCollection.Clear()
            For Each row As DataRow In dt.Rows
                dataCollection.Add(row(0).ToString())
            Next
        Catch ex As Exception
            MessageBox.Show("Can not open connection ! ")
        End Try
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If TextBox1.Text.Length >= 3 Then
            getData(DataCollection)
            TextBox1.AutoCompleteCustomSource = DataCollection
        End If

    End Sub
End Class


G.Waters

解决方案

I have a MSDN code sample you can look at which uses MS-Access but the same logic works for SQL-Server as both OleDb and SqlClient Data providers have the same objects and methods e.g. SqlDataAdapter is the same animal as OleDbDataAdapter etc.

Snippets from the code sample where I use the absolute minimal code to do the auto-complete for a TextBox.

----

On a side note, the auto-complete aspect of a control such as a TextBox is a closed system so if you want to peek at a the current search/filter you can't.


这篇关于TextBox AutoComplete无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆