简体   繁体   中英

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.)

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?

Looking at your code, I realized that you want to display same number enteted in txtInput text repeatedly up to as many times as a number entered in a txtArrayValues.Text. So for example txtArrayValues. Text = "5" and txtInput.Text="2", your code will yield result "2,2,2,2,2". If that is what you want then the following code will achieve that.

using System.Linq;
    namespace Running_Total
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void btnDisplay_Click(object sender, EventArgs e)
            {
                int len, num;
                if (int.TryParse(txtArrayValues.Text, out len) &&
int.TryParse(txtInput.Text, out num))
                {
                    lblDisplay.Text = string.Join(",", new string[len].Select(x => txtInput.Text));
                }
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM