Hello who knows how to create a search box in Visual Basic? I want the search box based on streamreader. In proper terms, when I will input a text, it has to read on a text file in drice C. I know the streamreader code but don't know the code to do a search.

To read from a text file, try the following -

Private Sub Reading_Click() 
Dim variable1 As String 
Open "c:\My Documents\sample.txt" For Input As #1 
Input #1, variable1 
Text1.Text = variable1 
Close #1 
End Sub

At Text1.Text you can now check if a word exists etc. The above is a sample, you need to change it to suit your needs.:)

To write to a file, try the following -

Private Sub create_Click() 
Dim intMsg As String 
Dim StudentName As String
Open "c:\My Documents\sample.txt" For Output As #1 
intMsg = MsgBox("File sample.txt opened") 
StudentName = InputBox("Enter the student Name") 
Print #1, StudentName 
intMsg = MsgBox("Writing a" & StudentName & " to sample.txt ")
Close #1 
intMsg = MsgBox("File sample.txt closed") 
End Sub

Again, just a sample...

the 'Text1' is the textbox to input text in the search or the text file stored in the drive C?

Sorry, you are looking for a search term, my bad. Write something like -

Variable 1 is holding the string from the text file...

If varaible1 = Text1.Text Then
'Do here what you need to...
msgbox "Text found"
End If

Yes but the text1 you mentioned, is it the textbox name where the user will include the text to search or the text file in the drive c?

No, its a text box where the user will add the search criteria, hence Text1.Text. You can change Text1 to whatever naming convention you like - txtSearch...

Right now I have not yet done the form for the search box. Im having a small problem, when i fill my form quickly to save on the text file in drive C, it says "C:/text.txt" is being used by another.. But when I fill the form less quickly to save, it does not give me this. Why?

I will do the form, then I will provide you a picture of it, if I have difficulties, i will post it here. I have to put a multiline textbox as the result of the search must appear on the form itself!

Well I am designing the 'Search' form! In the meantime, I will explain it to you clearly below:

Normally, I must do 3 categories of search on one form! The first search category will be based only on product, the second category of search will be based on all like products, size and cost and the final category of search will be based on criterias like the name of the product, price, a size (big or small).

The form will have a search box for the first and second categories of search. The user will have the ability to select whether the first or the second or the third category of search on a combobox. If the user selects the first category, he will only get the results based on the first category and if he selects the second category, he will only get results of the second category.

Concerning the third category, if the user select it on the combobox, then it will enable.

All the search categories will be based on the streamreader concept, I mean it has to read on the text file located in the drive C.

And yes, all the results will be obtained on thesame multiline textbox on the form itself.

The problems im having are only these as follows:

1/ How to do a code depends on the category of search?
2/ How to make the results appear on the multiline textbox on the same form?
3/ Where to place the code for the search, on the search button or the search box?

I will successfully do the form coding, but only the three problems above I dont know how to do even by researching a lot on internet.

We can't use it as this project will be submitted! Many of my friends are getting the same problems on this 'Search' feature. Well, they are creating three forms of serch while me the three on one!

I'm poor in programming, I have successfully created the first interface by my own except the problem of combobox and masktextbox which you helped me, but now the search function I will be dead. What I must know is only the coding for the search, where to place the coding (on search button or textbox) and how to make the results appear on the rich text box on the same form. That's all!

Then use the following UNDER YOUR TEXTBOX LOST FOCUS EVENT.

Text1_LostFocus() 

'The code I gave you to open the file here....
While Not EOF(1)
    Line Input #1, Textline
    If Instr(variable1, Textline) > 0 then
        'String found
    End If
Wend
close #1
End Sub

I will design the form first which Im doing, then I will ask you some questions. This search feature should be good as if the user will misspell a word, he can still get the results.

Well, I will do the form, then I will come to you, Thank you for helping me!

Ok I have already do the form.

Now I will describe you my object's properties, my text files and the search.

In the text files located in my Drive C, which I used the aspect of StreamWriter to do it, look like this:

Product: Cement Size: Big Cost: 256 Type: Construction
Etc...

On my search form, I have a combobox which has two types of search: By Product and By All Categories.

If a user selects "By product", the results of the search must appear on the rich text box on form itself. And the user can do misspell of words, so the search feature should be ready to tackle that.

My questions are:

1/What code must I use to do this? (A code where only on the type of product, for example if the type is Construction, so the results should be all construction's products, costs and product's size only)
2/ Where to place the code? (In the search button or in the search text box)
3/ The results must appear on the rich text box

I think I have described it well, but if you need information, tell me. Please help, only the search part I don't know anything! Even on other sites, youtube Im having difficulties. I don't know which method you will suggest to me, StreamReader or whatever but the query must be good.

Thank you

What code must I use to do this?

I already gave you the code!

Where to place the code?

In a command button OR the search textbox lost focus event, as I have already mentioned above!

I forgot, must I include a textbox for the results or a rich text box?

Depends on the size of the returned data. If large, use RTF, else textbox.:)

Well, I have changed strategies. I have decided to not put the three categories of search on one form. What I am going to do is to make forms where aech of the catedory of search will be placed.

I think it will be more easy for me because frankly saying, just search a code on "Search" on internet for visual basic, there is not!

So I think you will have to help on only two categories not the tree as a whole because I will do one by my own.

I will reexplain it to you if you don't mind so that you can understand well as my previous explanation seems confused. I will begin on the first form. Now, imagine there is search box (textbox), and below there are a 'search' button with a rich text box.

My text file located in the drive C is like this:

Product: Cement Size: Big: Cost: 245 Type: Construction
Product: Iron Size: Big Cost: 123 Type Consruction
Product: Glass Size: Small Cost: 23 Type Non-Construction
Product: Chocolate Size: Small Cost: 10 Type: Food


I want to search only two types of items in my text file located in drive C, namely: Construction and Non-construction. When the user will type a product name, OR size, OR whatever, he must get results only for the Construction or Non-construction. The user must not get results for the Food. And the user can make misspelling of words in the search, so the Search must tackle it. And yes, the results should appear on the rich text box on the form itself.

My questions will be again:

1/ You mentioned 'LostFocus' above, what is that? Sorry, I have never heard about it, I'm new in VB, how to get that?

2/ Your code also I see there is a place where you write 'Textline and Variables1" what are they? What must I replace on the code?

3/ What is the code to put the results on the rich text box?

I'm sorry I gave you the explanation again, but I think you have understood now on this explanation. Sorry. Thank you for your time!

You mentioned 'LostFocus' above

If you add a text box control to your form, one of its events (like click) is LostFocus.

place where you write 'Textline and Variables1"

They are string variables that will hold the text that you are searching for from txtSearch. Lets say that you are searching for "bunnyalli2011", Textline will be bunnyalli2011, variable1 will be the text returned if bunnyalli was found.

What is the code to put the results on the rich text box

Add a rich textbox control to your form -

RTF1.Text = variable1

I am really confused with the code you gave me above, can you write them properly? As you gave me the code in two parts.

To facilitate you, here are some information on my form:

My search button: btnSearch
My search text box: txtSearch
My rich text box: rtbResults
My text files located in the drive C: c:\Items.txt

The information on my text file located in the Drive C are as follows:

Product: Cement Size: Big: Cost: 245 Type: Construction
Product: Iron Size: Big Cost: 123 Type Construction
Product: Glass Size: Small Cost: 23 Type Non-Construction
Product: Chocolate Size: Small Cost: 10 Type: Food


The search must be based only on Construction and Non-Construction only. I mean results must be from Construction and Non construction NOT the Food.

Sorry again, but believe me, once I did it, I will stop disturbing you...I'm sorry

:)

I'll see if I can squeeze some time into writing this quickly. I'll paste the code when its done.

Sorry again, but believe me, once I did it, I will stop disturbing you...I'm sorry

:)

Its all fine, thats why the site is here for. It just depends on time, I am quite busy at work, so we'll see...;)

Ok, I will wait...in the mean time, I am searching on MSDN and I am correcting some mistakes on the first form! THANK YOU DEAR...YOU ARE A GOD!

The following code works 100% -

Private Sub Command7_Click()

Dim sLineIn As String, temp As String

Open "c:\Template.txt" For Input As #1 ' Open file for reading, change as required...
    Open "c:\Template_tmp.txt" For Output As #2 ' Open temp file for output, does not have to be in your directory, only a temp file...
        Do Until EOF(1) ' Read file until End Of File
            Line Input #1, sLineIn ' Read current line into variable sLineIn
            temp = InStr(1, sLineIn, txtSearch.Text, vbTextCompare) ' Search for text
            If temp = 0 Then ' If text is NOT found, print line to second file
                Print #2, sLineIn
            Else
                Print #2, txtNewString.Text ' If text IS found, print this instead
            End If
        Loop ' Next line in file
    Close #2
Close #1

Kill "c:\Template.txt" ' Delete original file
Name "c:\Template_tmp.txt" As "c:\Template.txt" ' Rename temp file to original name
End Sub

Ok, I made what you said and got some errors. The words which are underlined by the blue colour in the events are in bold below and I included also the problem in brackets below the codes.


Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click

Dim sLineIn As String, temp As String
Open ("c:\Song File.txt") For Input As #1
Open ("c:\Template_tmp.txt") For Output As #2
Do Until EOF(1)
Line Input #1, sLineIn
temp = InStr(1, sLineIn, txtSearch.Text, vbTextCompare)
If temp = 0 Then
Print #2, sLineIn
Else
Print #2, txtResults.Text
End If
Loop
Close #2
Close #1
Kill("c:\Template.txt")
Name ("c:\Template_tmp.txt") As ("c:\Template.txt")

End Sub


The errors below:

Open, For: 'Open' is not declared. File I/O functionality is available in Microsoft Visual Basic namespace. 'For' commas or valid expression continuation expected....

Line Input #: 'Line' statements are no longer supported file I/O, 'Input' overload resolution failed because no accessible input...., '#' commas or a valid expression continuation expected

#2: Expression expected

Name ("c:\Template_tmp.txt") As ("c:\Template.txt"): Propertty access must assign to the property or use its value

After all these posts, I see you are using vb.net, hence the errors. I will have to see if I can change this to .net, not sure if I will be able to.:)

See why we always ask for code first.;)

Also, make sure that all the file names has been changed. Template needs to be changed to Song File in all lines where Template exist, even Template_tmp.txt

Im using Visual Basic not VB.net. well whats the difference by the way lol? sorry im a newbie in visual basic, it has been only 2 weeks im doing that class and already an assignment which is making me mad. :)

After all these posts, I see you are using vb.net

If one goes through this thread, it is quiet clear that the OP has never said what exactly he has really done. But he always pretended to do a lot while he actually did nothing. Nicely he framed his questions and made our expert do all the coding. Once the code is delivered he comes back complaining that it is not working.

Now the expert realises all his assumption was wrong (OP has posted some code from some where)and the actual coding was to be done in .NET not in VB6.

May be the expert now will start writing code again in .net and supply. But i am sure that will again not work for OP. Because s/he never understood the code (never bothered to do that), it is clear from previous posts.

Edited 5 Years Ago by debasisdas: n/a

This question has already been answered. Start a new discussion instead.