473,770 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

vb.net -- Enabling "Search" within a textbox

angelabi264
2 New Member
I am workin on a project of cd library...if we hav to search for a movie...whn we type the name of movie in a textbox...all (possibilities) movies havin tat name shud appear...
for eg : in database thr are movies such as
Hum Dil de chuke sanam
Dil tho pagal hai
Dilagi
Dil hai tumhara

now if i type in textbox,the word "DIL"...all the 4 movie names shud appear within the dropdown of the texbox...

if i add a word such as "HAI" in textbox...(ie"D IL HAI")...movie names containing those 2 words shud b displayed within dropdown of textbox...(in this case Dil tho pagal hai & Dil hai tumhara)

Can somebody plzzz help me out in writtin a code for this in vb.net....

[ i use visual studio 2005 and sql server 2005]
Feb 16 '09 #1
6 7388
OuTCasT
374 Contributor
Expand|Select|Wrap|Line Numbers
  1. select movie from movies where movie = '" & txtMovies.text & "' LIKE '%%'
Feb 17 '09 #2
Aads
48 New Member
Hi there,

(1) Please run the below script in your database which creates a table & inserts the movie names

Expand|Select|Wrap|Line Numbers
  1. Create Table tblMovies
  2. (
  3. idMovie INT Identity Primary Key,
  4. movieName Varchar(100)
  5. )
  6.  
  7. Insert Into tblMovies Values ('Hum Dil de chuke sanam')
  8. Insert Into tblMovies Values ('Dil tho pagal hai')
  9. Insert Into tblMovies Values ('Dilagi')
  10. Insert Into tblMovies Values ('Dil hai tumhara')
  11.  
(2) Now paste the below event handler in your project renaming to the textbox which you have specified

Expand|Select|Wrap|Line Numbers
  1.   Private Sub txtMovieName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtMovieName.TextChanged
  2.         Dim DtMovies As DataTable
  3.         Dim Query As String = "SELECT idMovie, movieName From tblMovies "
  4.         Try
  5.             If (txtMovieName.Text.Trim.Length = 0) Then
  6.                 cmbMovies.DataSource = Nothing
  7.                 Return
  8.             End If
  9.             cmbMovies.DataSource = Nothing
  10.  
  11.             For len As Int32 = 0 To txtMovieName.Text.Split(Space(1)).Length
  12.                 If (len = 0) Then
  13.                     Query &= "Where movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
  14.                 Else
  15.                     If (len = txtMovieName.Text.Split(Space(1)).Length) Then
  16.                         Exit For
  17.                     End If
  18.                     Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Text.Split(Space(1))(len) & "%'"
  19.                 End If
  20.             Next
  21.  
  22.  
  23.             Dim Conn As SqlConnection = New SqlConnection(CONN_STRING)
  24.             Conn.Open()
  25.             Dim sqlCmd As SqlCommand = New SqlCommand
  26.             With sqlCmd
  27.                 .Connection = Conn
  28.                 .CommandText = Query
  29.                 .CommandType = CommandType.Text
  30.             End With
  31.             Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
  32.             DtMovies = New DataTable
  33.             sqlDa.Fill(DtMovies)
  34.             cmbMovies.DataSource = Nothing
  35.             If (DtMovies IsNot Nothing) Then
  36.                 With cmbMovies
  37.                     .ValueMember = "idMovie"
  38.                     .DisplayMember = "movieName"
  39.                     .DataSource = DtMovies
  40.                 End With
  41.             End If
  42.  
  43.         Catch ex As Exception
  44.             MessageBox.Show(ex.Message)
  45.         End Try
  46.     End Sub
  47.  
(3) Finally run your project & you are all done!

Regards,
Aads
Feb 22 '09 #3
Tyecom
5 New Member
Hi Aads,

I'm trying to do the same thing and I'm trying to use the code you provided. However, I'm getting errors messages. The error I'm getting is "MovieSampl e not declared" and "Conn_Strin g not declared". Here is my modified code:

Imports System
Imports System.Configur ation
Imports System.Data.Sql Client

Public Class Form1

Private Sub txtMovieName_Te xtChanged(ByVal sender As Object, ByVal e As System.EventArg s) Handles txtMovieName.Te xtChanged

Dim DtMovies As DataTable
Dim Query As String = "SELECT idMovie, movieName From tblMovies "

Try
If (txtMovieName.T ext.Trim.Length = 0) Then
MovieSample.Dat aSource = Nothing
Return
End If
MovieSample.Dat aSource = Nothing

For len As Int32 = 0 To txtMovieName.Te xt.Split(Space( 1)).Length
If (len = 0) Then
Query &= "Where movieName Like '%" & txtMovieName.Te xt.Split(Space( 1))(len) & "%'"
Else
If (len = txtMovieName.Te xt.Split(Space( 1)).Length) Then
Exit For
End If
Query &= vbCrLf & " AND " & vbCrLf & "movieName Like '%" & txtMovieName.Te xt.Split(Space( 1))(len) & "%'"
End If
Next

Dim Conn As SqlConnection = New SqlConnection(C ONN_STRING)
Conn.Open()
Dim sqlCmd As SqlCommand = New SqlCommand
With sqlCmd
.Connection = Conn
.CommandText = Query
.CommandType = CommandType.Tex t
End With
Dim sqlDa As SqlDataAdapter = New SqlDataAdapter( sqlCmd)
DtMovies = New DataTable
sqlDa.Fill(DtMo vies)
MovieSample.Dat aSource = Nothing
If (DtMovies IsNot Nothing) Then
With MovieSample
.ValueMember = "idMovie"
.DisplayMember = "movieName"
.DataSource = DtMovies
End With
End If

Catch ex As Exception
MessageBox.Show (ex.Message)
End Try
End Sub
End Class
Feb 24 '09 #4
Aads
48 New Member
Hi there,
  • MovieSample is the name of the combobox where it displays all the matched movie names and
  • CONN_STRING is the connection string to connect to the database - you need to declare this variable at the top like this
Expand|Select|Wrap|Line Numbers
  1. Private Const CONN_STRING As String = "Server = localhost;Database = someDbName; Integrated Security = SSPI"
  2.  
Feb 25 '09 #5
Tyecom
5 New Member
Hi Aads,

Thank you for your assistance. I'm trying to do something very similiar to what you described above. I also want to be able to search a combobox, but using the combobox "only". For instance, I want to type in a combobox titles of movies stored in my database. As I type, the movies will appear in automatically in the dropdown menu in alphabetical order. The filter in the above code does not filter the way I would like it to. Example, any title with the letter "T" will appear when you type a "T". Even if the "T" is the last letter of the title. I would like the title to appear in sequence. If I type a "T" all titles that begins with "T" will appear. If I type a "TH" all titles what begins with "TH" will appear and so on.

I tried to modified this code to reflect these changes, but have so far failed to make it work. Any assistance would be greatly appreciated.
Feb 25 '09 #6
Aads
48 New Member
Hi there,
  • First of all add a new form to your project.
  • Drag one Combobox from the toolbox & add it to you form; rename the combobox to cmbMovies.
  • Paste the below code in your form.
  • You will have to change the connection string to point to your database.

Expand|Select|Wrap|Line Numbers
  1.     Private mResetOnClear As Boolean = False
  2.     Public Property ResetOnClear() As Boolean
  3.         Get
  4.             Return mResetOnClear
  5.         End Get
  6.         Set(ByVal Value As Boolean)
  7.             mResetOnClear = Value
  8.         End Set
  9.     End Property
  10.  
  11.     Private Sub cmbMovies_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbMovies.KeyPress
  12.         Try
  13.             Dim intIndex As Integer
  14.             Dim strEntry As String
  15.  
  16.             If Char.IsControl(e.KeyChar) Then
  17.                 If cmbMovies.SelectionStart <= 1 Then
  18.                     If mResetOnClear Then
  19.                         cmbMovies.SelectedIndex = 0
  20.                         cmbMovies.SelectAll()
  21.                     Else
  22.                         cmbMovies.Text = String.Empty
  23.                         cmbMovies.SelectedIndex = -1
  24.                     End If
  25.                     e.Handled = True
  26.                     Exit Sub
  27.                 End If
  28.                 If cmbMovies.SelectionLength = 0 Then
  29.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.Text.Length - 1)
  30.                 Else
  31.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart - 1)
  32.                 End If
  33.                 fetchData(strEntry)
  34.             ElseIf (Not Char.IsLetterOrDigit(e.KeyChar)) And (Not Char.IsWhiteSpace(e.KeyChar)) Then  '< 32 Or KeyAscii > 127 Then
  35.                 Exit Sub
  36.             Else
  37.                 If cmbMovies.SelectionLength = 0 Then
  38.                     strEntry = UCase(cmbMovies.Text & e.KeyChar)
  39.                 Else
  40.                     strEntry = cmbMovies.Text.Substring(0, cmbMovies.SelectionStart) & e.KeyChar
  41.                 End If
  42.                 fetchData(strEntry)
  43.             End If
  44.  
  45.             intIndex = cmbMovies.FindString(strEntry)
  46.  
  47.             If intIndex <> -1 Then
  48.                 cmbMovies.SelectedIndex = intIndex
  49.                 cmbMovies.SelectionStart = strEntry.Length
  50.                 cmbMovies.SelectionLength = cmbMovies.Text.Length - cmbMovies.SelectionStart
  51.             Else
  52.                 cmbMovies.Focus()
  53.                 cmbMovies.Text = strEntry
  54.             End If
  55.             e.Handled = True
  56.             Exit Sub
  57.         Catch ex As Exception
  58.             MessageBox.Show(ex.Message)
  59.         End Try
  60.     End Sub
  61.  
  62.     Private Sub fetchData(ByVal currentEntry As String)
  63.         Dim DtMovies As DataTable
  64.         Dim Query As String = "SELECT idMovie, movieName From tblMovies "
  65.         Try
  66.             cmbMovies.DataSource = Nothing
  67.  
  68.             Query &= "Where movieName Like '" & currentEntry & "%'"
  69.  
  70.             Dim Conn As SqlConnection = New SqlConnection("Server=in-bl-akash-lap\DSI;Database=MyDb;Integrated Security = SSPI")
  71.             Conn.Open()
  72.             Dim sqlCmd As SqlCommand = New SqlCommand
  73.             With sqlCmd
  74.                 .Connection = Conn
  75.                 .CommandText = Query
  76.                 .CommandType = CommandType.Text
  77.             End With
  78.             Dim sqlDa As SqlDataAdapter = New SqlDataAdapter(sqlCmd)
  79.             DtMovies = New DataTable
  80.             sqlDa.Fill(DtMovies)
  81.             cmbMovies.DataSource = Nothing
  82.             If (DtMovies IsNot Nothing) Then
  83.                 If (DtMovies.Rows.Count = 0) Then
  84.                     'MessageBox.Show("Could not find title '" & e)                  
  85.                     Me.Text = currentEntry
  86.                 End If
  87.  
  88.                 cmbMovies.ValueMember = "idMovie"
  89.                 cmbMovies.DisplayMember = "movieName"
  90.                 cmbMovies.DataSource = DtMovies
  91.  
  92.             End If
  93.  
  94.         Catch ex As Exception
  95.             MessageBox.Show(ex.Message)
  96.         End Try
  97.     End Sub
  98.  
  • Finally you can change the code as you like for any custom functionality required - this code "ONLY" demonstrates the required functionality.

Thanks,
Aads
Feb 26 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1897
by: Troels F. Smit | last post by:
Where can I find information on implementing a search function on my site ? -- Mvh. Troels F. Smit
43
5127
by: steve | last post by:
I am quite frustrated with php’s include, as I have spent a ton of time on it already... anyone can tell me why it was designed like this (or something I don’t get)? The path in include is relative NOT to the immediate script that is including it, but is relative to the top-level calling script. In practice, this means that you have to constantly worry and adjust paths in includes, based on the startup scripts that call these...
1
1346
by: Leu | last post by:
I like to search in the forum but I can't find the (in FAQ described) "search" link: "... To access the search feature, click on the "search" link at the top of most pages. ..." Where is the "search" link? Leu --
0
1361
by: Tallgeese | last post by:
We developed an in-process COM object for our own application. A toolbar button is used to activate the COM object. But when the COM object loaded, the HTML help hang when clicking the "List Topics" in "Search" tab. The HTML help work nornally without loading the COM. Can anyone provide some starting points, so that I can look at the problem ? Thanks in advacne.
5
1572
by: Agnes | last post by:
For my own practices. I like to put "Me". e.g IF Me.txtInvoice.textlength = 0 ....... etc Me.txt.....etc However, Is there any difference (without Me) ?? Thanks
0
13756
NeoPa
by: NeoPa | last post by:
Intention : To prepare a WHERE clause for multiple field selection, but to ignore any fields where the selection criteria are not set. ONLY WORKS WITH TEXT FIELD SELECTIONS. Scenario : You have a table (tblMember) containing information for various people. Table Name=tblMember Field; Type; IndexInfo MemberID; AutoNumber; PK Surname; String
1
1267
by: awigren | last post by:
In my database, I have a special form specifically for searching one field. I am using this database for a 3 step process where each of the steps are on the main menu and each button takes you to a number of fields that eventually all add into the same record. After the first step, you type in the primary field's number to retrieve the second set of fields to fill in. Is there anyway to make a pop-up come up if the # that they typed in...
1
2304
by: Nour469 | last post by:
Hello, I have a problem when I run search, I have fields for: name, clientID and phoneNo. When a client calls I need to find them in the database, they might give me the name and it might not be spelled correctly, I need to search by phoneNo. (the fastest way!) I place the cursor in the appropriate field and click search. when the find window opens up, by default, the Look in: field is: 'Main application form: form' and the 'search field...
0
9453
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7451
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5354
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.