You are here:

C#/Reading from a file

Advertisement


Question
Hello Murat.

Language Visual C#

I am creating a program that read data (integer numbers) from a file, record them using a two dimensional array
and after that, display them in a listbox control. Theses are the data that i want output in a listbox control:

   02 15 18 20 39    
   07 11 14 15 34
   01 31 33 34 50    
   08 18 45 47 51

As you can see my program has three steps:

1) Read data (integer numbers) from a file.
2) Record the data using a two dimensional array.
3) Display the data into a listbox control.

I tried the second and third steps:

int [,] array = new int [4,5] {{ 02, 15, 18, 20, 39 },   
         { 07, 11, 14, 15, 34 },
         { 01, 31, 33, 34, 50 },   
         { 08, 18, 45, 47, 51 }};

listBox1.Items.Clear();

for (int i = 0; i < (array.Length); i++)         
{
    listBox1.Items.Add(i + 1 + " ) ");          
         
    listBox1.Items.Add("\r");

    for (int j = 0; j < 5; j++)
    {
         
        listBox1.Items.Add(lottery_numbers[i, j]);
         
        listBox1.Items.Add("\r");          
    }
}

This works, but the output was weird i mean, in one column.

1) How to fix that, i mean how to display the matrix with its 4 rows and its 5 columns?
2) How to read from a file, and after, to do what i did above?

Thanks.

Answer
Hi there,

This looks like a homework question and I don't solve homework questions. However because you gave a start with some coding, I will try to help you.

For reading from file to 2 dimensional array see following link:
StackOverflow.com

About showing in listbox, edit your code as following:
for (int i = 0; i < (array.Length); i++)         
{
   string row = i + 1 + " ) ";

   for (int j = 0; j < 5; j++)
   {
        
       row+= lottery_numbers[i, j] + " ";
         
   }

   listBox1.Items.Add(row);
}

Since listbox items are shown in different rows, you don't have to add new line manually.

Hope that helps.

Murat

C#

All Answers


Answers by Expert:


Ask Experts

Volunteer


Murat Mehmet

Expertise

I can help with questions about desktop and web programming in C#, including SOAP, XML, database managing, custom controls, security etc.

Experience

I have been developing web and especially desktop applications in C# and VB.Net for almost 5 years. My programming life has begun with VB6 long time ago, so its about 8 years that I am in this business.

Organizations
Was in R & D for 2 years in a popular Turkish technology website: cyber-warrior.com

Education/Credentials
2011 Computer Engineering graduation in University of Trakia in Turkey.

©2016 About.com. All rights reserved.