Click here to Skip to main content
15,905,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a data in a file called data.txt they look like that e.x:

ronado portugal [3]
mesi argentina [4]
mesu ozil germany [5]

Now I want to call this file with windows app and then those data to put in a Listbox... I am using this kind of code:

C#
OpenFileDialog dialogu = new OpenFileDialog();
            dialogu.Filter = "*.txt|*.txt";
            if (dialogu.ShowDialog() == DialogResult.OK)
            {
                listBox1.DataSource = File.ReadAllText(dialogu.FileName, System.Text.Encoding.Default);
                
            }


it doesn't work !!!
Posted
Updated 23-May-12 3:48am
v2

Try:
C#
listBox1.DataSource = File.ReadAllLines(dialogu.FileName, System.Text.Encoding.Default);
 
Share this answer
 
Comments
h5h5 23-May-12 9:52am    
hmmm I used it but doesn't work ??
Hi
Check this
C#
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog dialogu = new OpenFileDialog();
    dialogu.Filter = "*.txt|*.txt";
    if (dialogu.ShowDialog() == DialogResult.OK)
    {
        string[] all = File.ReadAllLines(dialogu.FileName, System.Text.Encoding.Default);
        listBox1.DataSource = all;

    }
}


Best Regards
M.Mitwalli
 
Share this answer
 
Comments
h5h5 23-May-12 10:01am    
Thnx a lot, it works
:)
h5h5 23-May-12 10:26am    
now I want to sort those data like that:
mesu ozil germany [5]
mesi argentina [4]
ronado portugal [3]

so the high value in bracket to be first :
I am using this code but they are not sorting

tring[] sort= listBox1.Items.Cast().ToArray();
List count= (
sort.GroupBy(name => name).OrderByDescending(g => g.Count())
.Select(group => string.Format("{0} [{1}]", group.Key, group.Count())).ToList());
listBox2.DataSource = count
Mohamed Mitwalli 23-May-12 14:46pm    
I already post answer in your post on this link
http://www.codeproject.com/Questions/390393/sorting-in-List-box-trouble check it
You need to use Itemsource for List box control. Datasource will not work in WPF

C#
lstBox.ItemsSource = System.IO.File.ReadAllLines(dialogu.FileName, System.Text.Encoding.Default);
 
Share this answer
 
v3
Comments
h5h5 23-May-12 9:53am    
ItemSource Doesn't exist man ???
What shuold I do ?
Jim Jos 23-May-12 9:55am    
You are using winforms is it?
h5h5 23-May-12 9:58am    
Yes men, Framework 3.5
Jim Jos 23-May-12 10:00am    
Then use Datasource instead. I have just crated a listbox in winforms and its working fine.. What about the file? Did you check File.Readalllines using immediate window? Please check if the data is returned properly from File.ReadAlllines

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