You are here:

C#/Display results in a List Box

Advertisement


Question
QUESTION: Hello Murat,
In the following program, what i did is to check which numbers of a matrix data are

inside and outside of an array of 25 numbers, but i have two problems:

  1) When the results are displaying in different ListBox controls, i want that these

result be displaying 6 numbers in one row and so on. I tried to do it, but without any

luck.
  2) I want to display the row number for each number inside of the array of 26

numbers, i think i have to create an array that records the number and its row number

that it belongs, inside the method isInArray().

Could you help me please, Thanks.

private void button1_Click(object sender, EventArgs e)
       {
         string row = "";
         bool foundTheNumber = false;
         int[] foundList = new int[26];

         string listBoxLine = "";
         string listBoxLineTwo = "";

         int[,] myArray = new int[4, 5] {{ 02, 05, 06, 14, 19 },
         { 07, 10, 12, 15, 16 },
         { 01, 03, 06, 15, 18 },
         { 03, 05, 06, 17, 18 }};

         listBox2.Items.Clear();
         listBox3.Items.Clear();

         for (int i = 0; i < 4; i++)
         {
         row = i + ")";
       
         for (int j = 0; j < 5; j++)
         {
         if (isInArray(ref myArray[i, j], ref myArray) == true)
         {
         foundTheNumber = true;

         //each number in a line is added to the string          
         row += myArray[i, j] + "     ";
         
         foundList[myArray[i, j]] = 1;
         }
         }
         }
         int counterInMatrix = 0;
         int counterOutMatrix = 0;

         if (foundTheNumber)
         {
         for (int i = 1; i < 26; i++)
         {
         if (foundList[i] == 1)
         {
         listBoxLine = listBoxLine + i + "   ";

         if (++counterInMatrix % 6 == 0)
         {
         listBoxLine =  listBoxLine + i + "   " + "
";
         }  
         }
         if (foundList[i] == 0)
         {
         listBoxLineTwo = listBoxLineTwo + i + "   ";

         if (++counterOutMatrix % 6 == 0)
         {
         listBoxLine = listBoxLine + i + "   " + "
";
         }
         }
         }
         }
         listBox2.Items.Add("Numbers of a Matrix Data Inside of the Array of 25 Numbers ");
         listBox2.Items.Add("");
         listBox2.Items.Add(listBoxLine);

         listBox3.Items.Add("Numbers of a Array of 25 Numbers, Outside of the Matrix Data ");
         listBox3.Items.Add("");
         listBox3.Items.Add(listBoxLineTwo);
       }
     
       static bool isInArray(ref int number, ref int[,] theArray)
       {
         for (int row = 0; row < theArray.Length; ++row)
         {
         for (int col = 0; col < 5; ++col)
         {
         if (theArray[row, col] == number) return true;
         }
         }
         return false;
       }
       private void Form1_Load(object sender, EventArgs e)
       {
         int[,] myArray = new int[4, 5] {{ 02, 05, 06, 14, 19 },
         { 07, 10, 12, 15, 16 },
         { 01, 03, 06, 15, 18 },
         { 03, 05, 06, 17, 18 }};   

         string row;
         listBox1.Items.Clear();
         listBox1.Items.Add("Checking which Numbers of this Matrix Data are Inside or Outside of a Array of 25 Numbers ");
         listBox1.Items.Add("");

         for (int i = 0; i < 4; i++)
         {
         row = i + 1 + " )   ";

         for (int j = 0; j < 5; j++)
         {
         row = "   " + row + myArray[i, j] + "     ";    //or  row += myArray[i, j] + "     ";
         }
         listBox1.Items.Add(row);
         }
       }

ANSWER: Hi again,

Since your code is too blended, I cannot help you to fix it.
However I can give you the idea.
To be able to show 6 numbers per each row, you have to either convert array from [4,5] to [4,6], or keep a counter to write on row and when reaches 6, it adds to listbox.
About recording, it's as you said, you need to record array numbers to a new array.

Good luck.

---------- FOLLOW-UP ----------

QUESTION: Hello Murat again,

I am sorry for the code i send you, here i show you where is my problem:

if (foundTheNumber == true)
{
  for (int i = 1; i < 26; i++)
  {
      // This means that, there are not numbers of myArray inside  the
      // foundList array, so, i want to display the numbers of the foundList
      // array that does not matches with the myArray's numbers.
     
      if (foundList[i] == 0)
      {
         listBoxLineTwo = listBoxLineTwo + i + "   ";
         
         // Here is the counter like you said, i used "\r\n" ,
         // so this way ,the rows has to be of six numbers when they are displaying,
         // but it does not works, can you help me to fix this, thanks again.
         
         if (++counterOutMatrix % 6 == 0)
         {
         listBoxLineTwo = listBoxLineTwo + i + "   " + "\r\n";
         }
      }
   }
}
listBox3.Items.Add("Numbers of the foundList[] of 25 Numbers, Outside of the Matrix Data")    
listBox3.Items.Add(listBoxLineTwo);

Answer
As I told back in old questions, \r\n has no effect in listbox. You have to use a multiline textbox to see effect of \r\n.
To work with listbox change your code like this:
First take information lines before these controls:

           listBox3.Items.Add("Numbers of a Array of 25 Numbers, Outside of the Matrix Data ");
           listBox3.Items.Add("");

           if (foundTheNumber)
           {
           ...

Change code with following.

if (foundList[i] == 0)
                   {
                       listBoxLineTwo = listBoxLineTwo + i + "   ";
                       if (++counterOutMatrix % 6 == 0)
                       {
                           listBox3.Items.Add(listBoxLineTwo);
                           listBoxLineTwo = "";
                       }
                   }

In the end check if any numbers left:

if (listBoxLineTwo != "")
           {
               listBox3.Items.Add(listBoxLineTwo);
           }

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.