【问题标题】:Having a button increment an array each time it is clicked每次单击按钮时都会增加一个数组
【发布时间】:2013-03-05 19:01:47
【问题描述】:

我的任务是使用一个按钮来设置输入到数组中的值。用户将按下按钮输入一个值,一旦按下按钮,用户输入的值就会存储到一个数组中。我的老师(是的,这是一道作业题)说他希望它一次只做一个值。

我遇到的问题是,我只是不知道要写什么才能让这种情况发生。我试着看看我在活动中能做些什么,但这并没有让我取得任何进展,除非答案就在那里,而我只是完全错过了它。

任何关于在哪里看的建议,或者关于写什么的想法都会很棒。

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();
}

是我目前输入的内容。

【问题讨论】:

  • 您的要求不清楚。请您仔细阅读您的问题,然后更清楚地描述您必须做什么?
  • parseVariableString() 是做什么的?
  • 如果是家庭作业,你有没有书面定义你的作业?
  • @Samiam 我已经编辑了这个问题。希望它更清楚。
  • @Fuex: parseVariableString() 是我为解析输入的变量而设置的方法。我意识到不需要我负责的问题。

标签: c# arrays winforms button


【解决方案1】:

让你开始

让我们先把图形设计师的东西排除在外:

  1. 制作您的 winforms 项目
  2. 拖放按钮
  3. 拖放一个文本框
  4. 双击按钮创建button_click事件处理程序

接下来,您可能希望您的数组保持在范围内,最简单的方法是将其声明为 Form1 实例的字段,然后在 `Form1 构造函数中实例化和/或初始化它.

然后你可以从你的事件处理程序中访问它

示例:

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();
    }
...

【讨论】:

  • 非常感谢。我肯定是在考虑手头的问题。
  • 这个方法不是每次都会覆盖数组的第一个索引吗?我对作业的解读是“将值添加到数组中”。因此,每次数组的大小都会增加 1,并将新输入的值添加到该新索引中。
  • @ChrisDunaway 这不是一个完整的项目。它只是为 OP 提供他需要的基本框架。我还不如写\\do something with the array here
【解决方案2】:

我不确定我是否根据您的代码得到了您的问题。换句话说,当按下按钮时,您想将数组长度增加 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;
    }
}

【讨论】:

    【解决方案3】:

    在我看来,每次单击“添加到数组”按钮时,您只需将数组大小调整为大一:

    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();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-28
      • 1970-01-01
      • 2013-02-16
      • 2019-05-12
      相关资源
      最近更新 更多