0

I am making a Windows Form app in C#. I need to print an array with random numbers on the screen.

enter image description here

There are 20 numbers in total (0 till 19). But it only prints the 19th element. Can anyone help me out? (See image) Don't worry about the comparison number. I just want to print all arrays in the 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}";
        }
    }
}
4
  • 1
    You repeatedly assign values to beforeLoop.Text. Since your computer is probably quite fast, shouldn't it be obvious that you will only see the result of the final assignment? Commented Oct 6, 2022 at 15:30
  • You're currently reassigning the Text Property which is overwriting any previously set values. Try using "+=" instead of just "=" and see if that works. You may also need a "new line" character (try System.Environment.NewLine) at the end to put each "Element" on its own line. Commented Oct 6, 2022 at 15:34
  • For future questions please re-read minimal reproducible example guidance on posting code. There is really no need to all "generate random" part, or whole compare_Click method. Ideally even removing for loop and replacing it with two assignments like string s = "a"; s = "b"; and question like "where 'a' goes after the second assignment". Commented Oct 6, 2022 at 15:43
  • You are just assigning one value at a time to the textbox wuth beforeLoop.Text = $"Element {i} = {numbers[i]}";. Did you want to use a multiline textbox, or a listbox or some other control to display multiple lines of text. Commented Oct 6, 2022 at 16:40

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.