9 Replies - 3466 Views - Last Post: 07 September 2012 - 04:22 AM Rate Topic: -----

#1 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Auto complete textbox and combo box in vb

Posted 06 September 2012 - 04:15 AM

Hi
I have 2 questions

1)i have a dataset with 2 tables (Customers,Contacts),the company name is on of the field in the customer table now i need help on my design as the company name is a textbox on my form i want it to work as follow:
- the user can just type the very first character in the text box and all the items starting with that character must be display in the dropdown

PS:my data must come from the database ( am using vb.net)
here my code:
 Dim con As New SqlClient.SqlConnection(My.Settings.NewEvapcoDBConnectionString.ToString)
        Dim cmd As New SqlClient.SqlCommand("SELECT comapanyname From customers", con)
        Dim dr As SqlClient.SqlDataReader = cmd.ExecuteReader
        con.Open()
        While (dr.Read())
            CompanyNameTextBox.AutoCompleteCustomSource.Add(dr("companyname").ToString)
        End While
        dr.Close()
        con.Close()
        CompanyNameTextBox.AutoCompleteMode = AutoCompleteMode.Suggest
        CompanyNameTextBox.AutoCompleteSource = AutoCompleteSource.CustomSource


am putting this code in the form load event is that right??????

2)my 2nd question is almst the same but now its not with a textbox but with a combobox

Thanks in advance

Edited by Dogstopper: :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Auto complete textbox and combo box in vb

#2 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 495
  • View blog
  • Posts: 3,300
  • Joined: 12-January 10

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 05:02 AM

try [code] tags
Was This Post Helpful? -1
  • +
  • -

#3 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 05:14 AM

hi daren!!!!

what do you mean by "this code has not been tested and is to be used as an example

please if you understand my question would you give me an answer....
Was This Post Helpful? 0
  • +
  • -

#4 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 495
  • View blog
  • Posts: 3,300
  • Joined: 12-January 10

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 05:24 AM

View Postchristian Mukeba, on 06 September 2012 - 08:14 AM, said:

hi daren!!!!

what do you mean by "this code has not been tested and is to be used as an example

please if you understand my question would you give me an answer....

that is my sig
wrap your code if [_code_] tags
for the combo box have your tried using a for each loop?

like "using code tags"

for each o in object
do something
next

This post has been edited by DarenR: 06 September 2012 - 05:25 AM

Was This Post Helpful? 0
  • +
  • -

#5 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 05:35 AM

would you pleae modifie my code and give me the right one coz i dont rely understand yours...
Was This Post Helpful? -1
  • +
  • -

#6 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 285
  • View blog
  • Posts: 1,503
  • Joined: 07-April 08

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 05:45 AM

We won't do your work for you, sorry.
Also, really the only answer to your question is have you tried it?

What DarenR was trying to say was that when you bind the data to the textbox for use in an auto complete you'll want to iterate through all the records in your dataset. The best way to do this is to use a For Each loop.

I would say that a for each loop is not needed since you're using a data reader to do the same thing. You're code looks like it should work, so try it and then if something doesn't work like you expect, come back and update the thread with an exact question and what steps you tried

This post has been edited by rgfirefly24: 06 September 2012 - 06:13 AM

Was This Post Helpful? 1
  • +
  • -

#7 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 06:05 AM

ayt!!rgfirefly am gonna try to use for each loop then i'l let you know what i get
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 01:53 PM

The code looks right with the AutoCompleteCustomSource but I can tell you that VB will bury some exceptions in the form_load event. Are you getting any exceptions in your immediate window that might be a problem? One in particular is trying to access and Access database in x64. It will appear that no errors occur but you won't retrieve any rows from your query. You can simply move the code you have in the form_load to form_shown and it should throw the exception (if any) and stop the execution if this is indeed happening.

The second part of your question is that the AutoCompleteSource of the combobox can be simply set to ListItems and will then give the desired search while typing as you are describing. This of course is based on your adding the items to the combobox much like you did with the AutoCompleteCustomSource and setting your AutoCompleteMode

I'm sure what you will end of finding is that you are trying to execute a command prior to opening a connection. And since it's in form_load you should see something to the effect of :
A first chance exception of type 'System.InvalidOperationException' occurred in System.Data.dll
in your immediate window.

This post has been edited by CharlieMay: 06 September 2012 - 02:00 PM

Was This Post Helpful? 2
  • +
  • -

#9 christian Mukeba  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 96
  • Joined: 06-September 12

Re: Auto complete textbox and combo box in vb

Posted 06 September 2012 - 11:16 PM

Thank alot CharlieMay you have saved meeeeeeeeeeeeeeeee thanks again.........i had to move my code from the form load event to the form shown and that works perfectly.........you are a STAR
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: Auto complete textbox and combo box in vb

Posted 07 September 2012 - 04:22 AM

No problem. Of course moving the code was to demonstrate how the code you have was fooling you into believing everything was error free when it wasn't. You should really look into enclosing your database code into try...catch blocks which should alleviate the form_load issues.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1