简体   繁体   English

每次单击按钮都会增加一个数组

[英]Having a button increment an array each time it is clicked

I'm tasked with having a button that will set a value entered to an array. 我的任务是有一个将设置输入数组值的按钮。 The user will enter a value press the button, once the button is pressed the value the user entered is stored into an array. 用户将按下按钮输入一个值,一旦按下按钮,用户输入的值将被存储到数组中。 My teacher (yes this is a homework question) said that he wants it to do only one value at a time. 我的老师(是的,这是一个家庭作业问题)说,他希望它一次只做一个有价值的事情。

The problem i'm running into is, I just don't know what to write in order for this to happen. 我遇到的问题是,我只是不知道要写些什么才能使这种情况发生。 I've tried looking at what all I can do in the event but that has gotten me nowhere, unless the answer was there and I just completely missed it. 我尝试查看事件中我能做些什么,但这使我无处可去,除非答案在那里,而我完全错过了它。

Any suggestions on where to look, or an idea of what to write would be awesome. 任何关于在哪里看的建议,或关于写什么的想法都很棒。

private void addToArray_Click(object sender, EventArgs e)
{
    Button bclick = (Button) sender;

    string variables = addArrayTextBox.Text;
    int []vars = new int[5];
    vars = parseVariableString(variables);
    int numberIndex = 0;

    for (int i = 0; i < vars.Length; i++)
    {
        int indexNumber = vars.Length;
        numberIndex = indexNumber;
    }
    integerTextBox.Text = numberIndex.ToString();
}

Is what I currently have typed up. 是我目前输入的内容。

to get you started 让你开始

let's get the graphical designer stuff out of the way first: 让我们先从图形设计师的角度出发:

  1. make your winforms project 做你的winforms项目
  2. drag and drop a button 拖放按钮
  3. drag and drop a text box 拖放文本框
  4. double-click on the button to create a button_click event handler 双击按钮以创建一个button_click事件处理程序

next, you'll probably want your array to stay in scope, the simplest way to do that is to declare it as a field of your Form1 instance, and then instantiate and/or initialize it in the `Form1 Constructor. 接下来,您可能希望数组保持作用域,最简单的方法是将其声明为Form1实例的字段,然后在Form1构造函数中实例化和/或初始化它。

Then you can access it from your event handler 然后您可以从事件处理程序中访问它

example: 例:

public partial class Form1 : Form
{
    int[] vars;
    int intergersEntered;
    public Form1()
    {
        InitializeComponent();

        vars = new int[5];
        intergersEntered = 0;
        // insert other initialization here
    }

    private void button1_Click(object sender, EventArgs e)
    {
       vars[0] = int.Parse(textBox1.Text);
       intergersEntered++;
       textBox2.Text = intergersEntered.ToString();
    }
...

I'm not sure I get your question based on your code. 我不确定我是否根据您的代码收到您的问题。 Paraphrasing, you want to increase an array length by 1 when a button is pressed, yes? 释义,您想在按下按钮时将数组长度增加1,是吗?

public partial class Form1 : Form
{
    private int[] vars;

    public Form1()
    {
        InitializeComponent();
        vars = new int[5];
    }

    private void button1_Click(object sender, EventArgs e)
    {
        int[] tmp = new int[vars.Length + 1];
        vars.CopyTo(tmp, 0);
        vars = tmp;
    }
}

It seems to me that you need to just resize the array to one higher each time the "Add To Array" button is clicked: 在我看来,每次单击“添加到阵列”按钮时,只需要将阵列的大小调整为更高的大小即可:

private void addToArray_Click(object sender, EventArgs e)
{

    //Calculate the new size of the array
    int newLength = arrayOfIntegers.Length + 1;

    //Resize the array
    Array.Resize(ref arrayOfIntegers, newLength);

    //Add the new value to the array
    //Note that this will fail if the textbox does not contain a valid integer.  
    //You can use the Integer.TryParse method to handle this
    arrayOfIntegers[newLength] = Integer.Parse(addArrayTextBox.Text);  

    //Update the text box with the new count
    integerTextBox.Text = newLength.ToString();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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