write a loop that prints a number of lines the user entered into the text box, this is using c# windows application

StackOverflow https://stackoverflow.com/questions/19466991

  •  01-07-2022
  •  | 
  •  

Question

write a loop that prints a number of lines the user entered into the text box for example, this is using c# windows application

user inputs 10, then in another textbox counts from 0 to 10 on different lines

Result 0 \r\n 1 \r\n 2 \r\n 3 \r\n 4 \r\n 5 \r\n 6 \r\n 7 \r\n 8 \r\n 9 \r\n 10 \r\n

i have tried to incorporate a for loop and for each but it is only printing out one value, and i have to go into the array and print each iteration of the array like so textboxOutput.text = Array[0] + Array[1] , but i want it to print out all of the list without accessing the single ints in the array

        private void button1_Click(object sender, EventArgs e)
    {
        string input = textBox1.Text;
        int Number;


        bool Anumber = Int32.TryParse(input, out Number);
        int newNumber = Number;
        int[] List = new int[newNumber];

            for (int i = 0; i < List.Length; i++ )
            {

                List[i] = i;
                //textBox2.Text =List[0] + "\r\n" + List[1] + "\r\n" + List[2];
            }
            foreach (int num in List)
            {
                textBox2.Text = num.ToString() ;
            }
}  
}

}

Was it helpful?

Solution

I have managed to figure out my own question

       private void button1_Click(object sender, EventArgs e)
    {
        int OutputNumber;  
        bool number = Int32.TryParse(inputtxt.Text, out OutputNumber);
        string[] array = new string[OutputNumber];
        int put = 0;
        for (int i = 0; i < array.Length; i++)
        {

            array[i] = put.ToString();
            put++;

            string result = ConvertStringArrayToString(array);
            string result1 = result + OutputNumber.ToString();
            outputtxt.Text = result1;
        }}

            static string ConvertStringArrayToString(string[] array)
        {

            StringBuilder buildingstring = new StringBuilder();
            foreach (string value in array)
            {
                buildingstring.Append(value);
                buildingstring.Append("\r\n");
            }
            return buildingstring.ToString();
}

I have gone around a different way, by creating a string array adding each value to the string array and using a separate function to join all of the string array data together and separating each value with a return

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top