Home > other >  For loop c# only prints the last element of loop in windows form
For loop c# only prints the last element of loop in windows form

Time:10-07

i am making an windows form app in c#. I need to print an array with random numbers on the screen. There are 20 enter image description herenumbers in total (0 till 19). But it only prints the 19th element. Can anyone help me out? See image. Dont worry about the comparison number. I just want to print all array's in de label in the form. Here is some code:

const int numberOfItems = 20;
   

        private void Form1_Load(object sender, EventArgs e)
        {
            CultureInfo ci = new CultureInfo("en-US");
            Thread.CurrentThread.CurrentUICulture = ci;
            Thread.CurrentThread.CurrentCulture = ci;

            int[] numbers = new int[numberOfItems];

            Random random = new Random();
            double total = 0;
          
            for (int i = 0; i < numbers.Length; i  )
            {
                numbers[i] = random.Next(501);
                
                beforeLoop.Text = $"Element {i} = {numbers[i]}";
        

                total  = numbers[i];

            }
        

        }

        private void compare_Click(object sender, EventArgs e)
        {
        

            int[] numbers = new int[numberOfItems];

            Random random = new Random();
            double total = 0;
            for (int i = 0; i < numbers.Length; i  )
            {
                numbers[i] = random.Next(501);
              
                int numberBigger = numbers[i]   10;
                int numberSmaller = numbers[i] - 5;
                total  = numbers[i];
                int number = int.Parse(numberBox.Text);

                if (number > numbers[i])

                {
                    printLabel.Text = $"Element {i} = {numberBigger}";
                }
                else
                {
                    printLabel.Text = $"Element {i} = {numberSmaller}";
                }

            }

CodePudding user response:

From your question, I understand that you want to concatenate the strings and print them in multiline label control. If so, then try this code:

const int numberOfItems = 20;

private void Form1_Load(object sender, EventArgs e)
{
    CultureInfo ci = new CultureInfo("en-US");
    Thread.CurrentThread.CurrentUICulture = ci;
    Thread.CurrentThread.CurrentCulture = ci;

    int[] numbers = new int[numberOfItems];

    Random random = new Random();
    double total = 0;
   
    var text = "";
    for (int i = 0; i < numbers.Length; i  )
    {
        numbers[i] = random.Next(501);
        
        text  = $"Element {i} = {numbers[i]} \n";
        total  = numbers[i];
    }

    beforeLoop.Text = text;
}

private void compare_Click(object sender, EventArgs e)
{


    int[] numbers = new int[numberOfItems];

    Random random = new Random();
    double total = 0;
    var text = "";
    for (int i = 0; i < numbers.Length; i  )
    {
        numbers[i] = random.Next(501);
        
        int numberBigger = numbers[i]   10;
        int numberSmaller = numbers[i] - 5;
        total  = numbers[i];
        int number = int.Parse(numberBox.Text);

        if (number > numbers[i])
        {
            text  = $"Element {i} = {numberBigger} \n";
        }
        else
        {
            text  =  $"Element {i} = {numberSmaller} \n";
        }
        printLabel.Text = text;
    }
}

The solution here is to add text to a variable in each loop combined by a new line \n. Another solution is to append the text to a c# list. Then join the list with a new line to get a multi-lines string.

  •  Tags:  
  • c#
  • Related