2017-10-11 101 views
0

我目前正在努力完成一项我正在努力完成的家庭作业任务。该计划的目标是:C#从窗体窗体文本框输入数组

创建一个程序,允许用户记录和查看最多1000个事件的名称。事件的名称将被存储在一个数组中。名称只能按顺序存储和查看:我,第一次用户按下“Set”存储名称时,它将被放置在数组的索引0中;用户第二次存储将存储在索引1中的名称等。类似地,当用户第一次按下“查看”​​时,在数组的索引0中找到的项目被显示给用户;用户按压“查看”,在所述阵列的索引1中找到的项的第二时间显示等

  • 当用户按下“设置”按钮(btnSet),事件名称被插入如下所述依次到下一个空闲索引的数组中。插入数组的名称取自txtEventName。
  • 当用户按下按钮“查看”(btnView)时,将向用户显示顺序查看的下一个事件的名称(如上所述)。事件名称在txtName中显示给用户。

我目前拥有的代码:

namespace ArrayHW 
{ 
    public partial class Form1 : Form 
    { 

     int[] eventsArray; 
     const int SIZE = 1000; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnSet_Click(object sender, EventArgs e) 
     { 
      eventsArray = new int[SIZE]; 

      for (int index = 0; index < eventsArray.Length - 1; index++) 
      { 
       eventsArray[index] = Convert.ToInt32(txtEventName.Text); 
      } 

     } 

     private void btnView_Click(object sender, EventArgs e) 
     { 
      for (int index = 0; index < eventsArray.Length- 1; index++) 
      { 
       Debug.WriteLine(eventsArray[index]); 
       txtName.Text = Convert.ToString(eventsArray[index]); 
      } 

     } 
    } 
} 

当我运行的形式,我只是得到的结果为指数= 0,1,2,3,等或任何我刚刚输入到阵列中

private void btnSet_Click(object sender, EventArgs e) 
{ 
    eventsArray = new int[SIZE]; 

    for (int index = 0; index < eventsArray.Length - 1; index++) 
    { 
     eventsArray[index] = Convert.ToInt32(txtEventName.Text); 
    } 

} 

而不是将其显示出来以连续的顺序等它应该。任何人都可以告诉我一个更好的方法来解决这个问题,或者帮我找出我做错了什么?非常感谢你。

回答

1

请阅读代码块中的注释。希望这可以帮助你解决你的问题。

public partial class Form1 : Form 
{ 
    const int SIZE = 1000; 
    int[] eventsArray = new int[SIZE]; 

    //as per your requirement, you would need these 
    //to display and set items at proper index in the array. 
    int _setIndex = 0; 
    int _viewIndex = 0; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void btnSet_Click(object sender, EventArgs e) 
    { 
     //this is where your problem is. Every time the Set btn is clicked, 
     //you are creating a new array. Therefore you are only seeing what you added in the 
     //last click. Anything that was added to the array on the prior clicks are lost because 
     //you just created a new array with the line below. 
     //eventsArray = new int[SIZE]; 
     //You would need to comment the line above because this is code block is being 
     //executed on every btnSet click. New up this array only once by moving this to global scope. 

     //for (int index = 0; index < eventsArray.Length - 1; index++) 
     //{ 
     // eventsArray[index] = Convert.ToInt32(txtEventName.Text); 
     //} 

     //Since we have created fields to keep track of _setIndex, all we need to do is: 
     if (_setIndex < SIZE) 
     { 
      eventsArray[_setIndex] = Convert.ToInt32(txtEventName.Text); 
      _setIndex++; 
     } 
    } 

    private void btnView_Click(object sender, EventArgs e) 
    { 
     //this wont be necessary. 
     //for (int index = 0; index < eventsArray.Length - 1; index++) 
     //{ 
     // Debug.WriteLine(eventsArray[index]); 
     // txtName.Text = Convert.ToString(eventsArray[index]); 
     //} 
     if(eventsArray.Length > _viewIndex) 
     { 
      txtName.Text = Convert.ToString(eventsArray[_viewIndex]); 
      //this is to fulfill the requirement below: 
      // Similarly, the first time the user presses “View”, the item found in index 0 
      // of the array is shown to the user; the second time the user presses “View”, 
      // the item found in index 1 of the array is shown, etc. 
      _viewIndex++; 
     } 
    } 
} 
0

试试这个:

int[] eventsArray = new int[SIZE]; 
int index = 0; 
const int SIZE = 1000; 

private void btnSet_Click(object sender, EventArgs e) 
{ 
    eventsArray[index++] = Convert.ToInt32(txtEventName.Text); 
} 
+0

您好,非常感谢您的回复。我用新的代码尝试了你的解决方案,但是现在我的输出始终为0。我是否错误地声明了某些内容? '\t \t int [] eventsArray = new int [SIZE]; \t \t int index = 0; \t \t const int SIZE = 1000; public Form1() { InitializeComponent(); } private void btnSet_Click(object sender,EventArgs e) { eventsArray = new int [SIZE]; eventsArray [index ++] = Convert.ToInt32(txtEventName.Text); Debug.WriteLine(eventsArray [index ++]); }' – Zack

+0

@Zack - 我的代码在'btnSet_Click'方法中没有'eventsArray = new int [SIZE];'。你为什么把它放进去?当你拿出来会发生什么? – Enigmativity

+0

@Zack - 为什么你把'Debug.WriteLine(eventsArray [index ++])'?这是行不通的。 – Enigmativity