Search code examples
c#arraysvisual-studio-2012displayobject

Microsoft Visual Studio Arrays at Runtime


My instructions are: "Create a form that will display a running total of numbers a user enters." - to do this I've created a form with two text boxes (one for the number of values in the array and the other for the values in the array), a button to display it, and a label for it all to be displayed it. The issue is, is that my values aren't showing up - at all. My code is as below:

(** NOTE: I'm attempting to get the array to display in my label. txtInput is the inputted values and txtArrayValues is the number of elements.)

namespace Running_Total
{
    public partial class frmEnter : Form
    {
        public frmEnter()
    {
        InitializeComponent();
    }

    private void btnDisplay_Click(object sender, EventArgs e)
    {
        int intNumber = Convert.ToInt32(txtArrayValues.Text);

        string[] strArray;
        strArray = new string[intNumber];

        int i;
        string j = "";

        for (i = 0; i < intNumber; i++)
        {
            j = Convert.ToString(txtInput.Text);
            strArray[i] += j;
        }

        lblDisplay.Text = strArray + " ";
    }
}

}

Before, when I'd put lblDisplay.Text += j + " ";, it showed up in the label, but didn't pay any attention to the amount of elements the code was supposed to have. (Edit: this no longer works in my code.) (As is indicated in the title, I'm working with C# through Microsoft Visual Studio.)


Solution

  • It strongly depends on the fashion how the user inputs the numbers.

    1) If he fills the textbox once with numbers and then presses the button to display them in the other box, it would suffice to use a string array catch the input and add it to the textbox or label that displays it. If he deletes the numbers in the input box and types new ones you could just repeat this step

    namespace Running_Total
    {
        public partial class frmEnter : Form
        {
             // declare your Array here
             string [] array = new string[1000];
             int count = 0;
    
             public frmEnter()
             {
                InitializeComponent();
             }
    
             private void btnDisplay_Click(object sender, EventArgs e)
             {
                 // save input
                 array[count] = inputTextBox.Text;
                 count++;
    
                 // display whole input
                 string output = "";
                 for(int i = 0;i < count; i++)
                 {
                     output += array[i];                     
                 }
                 // write it the texbox
                 outputTextBox.Text = output;
    
             }
    }
    

    Does that answer your question or do you have another input pattern in mind?