Click here to Skip to main content
14,665,452 members
Rate this:
Please Sign up or sign in to vote.
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

I found this answer -

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;
}
}
}

from Salman622.

But I am tired. Could anyone show me where is wrong? code does error.

Thanks in advanced
Posted

Rate this:
Please Sign up or sign in to vote.

Solution 1

Please analyze the following code sample

string searchWord = "abc"; //search work actually it comes from user input
            string filePath = @"D:\\Data\abc.txt"; //file location
            IEnumerable<string> dataList = File.ReadLines(filePath); //read all fine contain and store it to a list.
            IEnumerable<string> result = dataList.Where(d => d.StartsWith(searchWord));</string></string>


here searchWord comes from user input. Use linq where function and with StartWith function. Based on the searchWord the result will be populated from your file. It will return appropriate data. If just put the above code to your application event handler.
   
v2
Comments
ridoy 26-Oct-13 16:47pm
   
good suggestion,my 5.
Richard MacCutchan 27-Oct-13 8:19am
   
Don't be abusive or you will find your account terminated. You do not pay for this service so be a little more grateful for what you have been offered.
Programmer Pavel 27-Oct-13 9:02am
   
Oh I am really sorry. Actually I was very angry on him.
Sorry for my language.

Thanks for your advice
ridoy 27-Oct-13 13:27pm
   
Oh really! glad to find anyone who is angry with mine.Ok, let me explain,if i really just comment or answer only for collecting points then you will not see me till now in CP,right?Now come to current topic, it's not really need all time to test the code by creating a new project and then create the environment just like your question and then examine code is right or wrong.It is all about logic behind an answer which is mainly voted, not the case that the answerer will give the whole code and you will just copy paste it.As Richard said earlier you are not going to pay for solving your question. And if you look carefully at above code,you will find the logic that would solve the problem no matter if you are an average programmer.
And last word is, you need a bit careful about your words as your account is a little bit away from being banned.Btw, happy coding!
Rate this:
Please Sign up or sign in to vote.

Solution 2

Solution 1 which i provide was an idea you need to use it with little modication. You claim that that is not working but not provide detail error information which you found. However i will provide full code which i tested and found working with .NET framework 4.5 with Windows forms application.
I just add a sample WOrdFile.txt file in my solution with line break of every word.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private IEnumerable<string> _wordList; 

        public Form1()
        {
            InitializeComponent();
            PopulateWordList();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string searchWord = textBox1.Text;
            //search and populate the result
            IEnumerable<string> result = _wordList.Where(d => d.StartsWith(searchWord));

            //display the result in a listbox
            foreach (string word in result)
            {
                listBox1.Items.Add(word);
            }
        }

        //create word list from a source text file there word is stored with line break
        private void PopulateWordList()
        {
            string filePath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) , "WOrdFile.txt");
            //do some defencive programming
            if (File.Exists(filePath))
            {
                _wordList = File.ReadLines(filePath);
            }
        }
    }
}


I created a Windows Form app and a new windows form there i put one textbox, one listbox and one button. The code is self explanatory. Let me know anything you do not understand in code.
   
v4
Comments
Programmer Pavel 27-Oct-13 12:08pm
   
ERROR
IEnumerable< string >
result = _wordList.Where(d =>
d.StartsWith(searchWord));

on here

anyway I send you a mail. check out
and reply me with source project kindly if possible.
S. M. Ahasan Habib 27-Oct-13 12:19pm
   
Please write your mail address here. I will send my project.
Programmer Pavel 27-Oct-13 14:18pm
   
Thanks Ahsan Habib.You are too helpful for me.
I was collect your mail from http://www.ahabibsblog.blogspot.com/

anyway send mail to - scriptickinc@gmail.com
Programmer Pavel 27-Oct-13 14:28pm
   
http://www.codeproject.com/Questions/674579/Filter-listBox1-and-show-result-listBox2

take a look.

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100