2017-10-11 103 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