Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have a textfile on C://file.txt

On that text file contains -

CodeProject
Coogle
Code
Cripsy
Cyclone
Crime
Criminal
Dog
Din
Done
Doing
Duty
Desk
Eagle
Ear
Eat
.......

etc

Now my question is -
I have a csharp winform application
Button1
Textbox1
Listbox1

I wanna put a code on button1-
If I write in textbox1= E
On listbox show all Word started with E from file.txt
If I type "Do"
then show
Done
Doing
Dog


How to do this?

Thanks in advanced
Posted

The framework gently provides the String.StartsWith method[^].

In a naive approach you read line by line the file and use the suggested method, if there is match then you add the read word to the list box.

In amore smart approach you could read the whole file once into memory (e.g. in a collection of strings) and then search into such cached info.
 
Share this answer
 
Read the items of textfile and add those items in a List<string>().For rest of the work follow this one:
Keep AutoComplete List Open[^]
 
Share this answer
 
v4
C#
protected void Button_Click(object sender, EventArgs e)
    {
        string fileLoc = Server.MapPath("You Location");
        if (File.Exists(fileLoc))
        {
            String line;
            using (TextReader tr = new StreamReader(fileLoc))
            {
                line = tr.ReadToEnd();
            }
           string[] read=line.StartsWith(textboxName.text);
        for(int i=0;i<read.length;i++)
        {
            //your logic;
        }
        }
    }
 
Share this answer
 
Comments
[no name] 24-Oct-13 6:41am    
Cannot implicitly convert type 'bool' to string
Salman622 how to add these searched items to Listbox1 pls tell.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900