VB.NET中的文本框AutoCompleteSource [英] Textbox AutoCompleteSource in VB.NET

查看:153
本文介绍了VB.NET中的文本框AutoCompleteSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码来自一个类:

Below is the code comes from a Class:

Public Function GetVendorNames() As AutoCompleteStringCollection
        Dim X As New AutoCompleteStringCollection
        MyBase.ConnectToDB()
        Dim cmd As New SqlClient.SqlCommand("select distinct vendor from product", Cn)
        Dim dr As SqlClient.SqlDataReader
        dr = cmd.ExecuteReader
        While dr.Read
            X.Add(dr.Item(0).ToString)
        End While
        MyBase.DisconnectFromDB()
        cmd.Dispose()
        cmd = Nothing
        Return X
    End Function


以下是来自Form的代码:


Below is the code comes from Form:

Private Sub Autopopulate(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtVendor.GotFocus
        Dim p As New Product
        txtVendor.AutoCompleteSource = AutoCompleteSource.CustomSource
        txtVendor.AutoCompleteCustomSource = p.GetVendorNames
        txtVendor.AutoCompleteMode = AutoCompleteMode.Suggest
        p = Nothing
    End Sub



当我将光标放在txtVendor文本框中时,我希望自动完成功能会生成供应商列表.但是,出乎意料的是,当我进入文本框时,我立即收到错误消息"ERROR CREATING WINDOW HANDLE".错误显示在txtVendor.AutoCompleteMode = AutocompleteMode.Suggest语句上.

有人可以帮忙吗?



When I place my cursor in the txtVendor Textbox, I expect autocomplete feature should generate the list of vendors. But, unexpectedly I am given an error as "ERROR CREATING WINDOW HANDLE" instantly when I enter into the text box. The error is showing on txtVendor.AutoCompleteMode=AutocompleteMode.Suggest statement.

Could someone help on this?

推荐答案

尝试下面的代码.
它的工作方式和更改文本框属性(自动完成模式和自动完成源)
Try below code.
Its working and to change the textbox properties(autocompletemode and autocompletesource)
 Cn.Open()
Dim dt As New DataTable
Dim da As New OleDbDataAdapter("select ...........", Cn)
da.Fill(dt)
Dim row As DataRow
txtvendor.AutoCompleteCustomSource.Clear()
For Each row In dt.Rows
   txtvendor.AutoCompleteCustomSource.Add(row.Item(0).ToString())
Next


我不认为AutoComplete旨在按照您描述的方式工作.通常,它可以在用户在文本框中键入内容时起作用,而不会立即提供焦点,甚至可以自动为您处理.您可以在表单的Load事件中一次性设置源,它将在用户输入时处理显示建议.将所需的代码移到表单的Load事件,然后尝试一下.
I don''t believe AutoComplete is intended to work the way you''ve described it. Typically it works as the user types in the textbox, not as soon as the focus is given, and even that is automatically handled for you. You can set the source up one time in the Load event of the form and it will handle showing the suggestions as the user types. Move the code you have to the Load event of the form and then try it out.


这篇关于VB.NET中的文本框AutoCompleteSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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