2012-09-20 3 views
0

배열의 내용을 가져 와서 단추를 클릭 할 때 메시지 상자에 넣어야합니다. 숫자는 사용자가 추가 버튼을 누를 때 배열에로드되고 다른 함수는 잘됩니다. 디스플레이 버튼을 클릭하면 그러나, 메시지 상자를 팝업하지만 그냥 여기에 0을 읽는 코드입니다 :배열의 내용을 메시지 상자에 넣기

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Testscores.UI 
{ 
    public partial class frmTestscores : Form 
    { 
     //creates the variables needed for the calculations 
     int scores; 
     double total = 0; 
     int count = 0; 
     int counts = 0; 
     double average = 0; 
     int[] sArray; 


     public frmTestscores() 
     { 
      InitializeComponent(); 
     } 
     //creates the exit button click event 
     private void btnExit_Click(object sender, EventArgs e) 
     { 
      //exits the application 
      Application.Exit(); 
     } 
     //creates the clear button click event 
     private void btnClear_Click(object sender, EventArgs e) 
     { 
      //clears all text fields and variables 
      txtAverage.Text = ""; 
      txtCount.Text = ""; 
      txtScore.Text = ""; 
      txtTotal.Text = ""; 
      scores = 0; 
      total = 0; 
      counts = 0; 
      average = 0; 

     } 
     //creates the add button click event 
     private void btnAdd_Click(object sender, EventArgs e) 
     { 
      //creates the try and catch statements 
      try 
      { 
       //does the calculations and outputs it into the text fields 
       scores = int.Parse(txtScore.Text); 

       counts += 1; 
       total = total + scores; 

       average = total/counts; 
        txtAverage.Text = average.ToString(); 
        txtTotal.Text = total.ToString(); 
       txtCount.Text = counts.ToString(); 
       //initializes the array(this is where i get confused) 
       int SIZE = counts; 
       Array.Resize(ref sArray, SIZE); 
       for (int count = 1; count > SIZE; count++) 
       { 

        sArray[count] = scores; 
        //outputs the count to the text box 
        txtCount.Text = count.ToString(); 




       } 
      } 
       //catch statement 
      catch (Exception ex) 
      { 
       //outsputs a message to the user 
       MessageBox.Show("Please enter a valid number,"); 

      } 
     } 
     //creates the display button click event 
     private void btnDisplay_Click(object sender, EventArgs e) 
     { 
      //supposed to output the array to a message box 
      MessageBox.Show(sArray[].ToString()); 

     } 
    } 
} 
+1

당신은 정말 오히려 당신이 항목을 추가 매번 크기를 조정 배열보다, 숫자를 유지하기 위해'목록 을'사용해야합니다. 배열의 크기를 변경하면 완전히 새로운 것을 만들고 모든 단일 요소를 복사하는 것을 의미합니다. – Servy

+1

줄'MessageBox.Show (sArray []. ToString());'는 컴파일하지 말아야합니다 –

답변

5

당신은에서와 같이 (하나의 문자열로 배열에서 각각의 문자열을 결합 할 수 있습니다 다음 string.Join 방법)과는 연결된 문자열을 표시 :

private void btnDisplay_Click(object sender, EventArgs e) 
{ 
    string output = string.Empty; 
    foreach (var item in sArray) 
    { 
     output += item + " "; 
    } 

    //supposed to output the array to a message box 
    MessageBox.Show(output); 
} 
+0

+1,'string.Join (...)'을 좋아합니다. – Gromer

0
가 디스플레이 클릭 이벤트 처리기를 변경

. ToString()에 대한 설명서를 읽으십시오. 서브 클래스에서 오버라이드 (override)되지 않는 한, 일반적으로 객체의 typename을 출력합니다.

이 일의 폭력은 매우 간단합니다 :

string output = new string(); 

for(int i = 0; i < sArray.Length; i++) 
{ 
    output += sArray[i] // plus any delimiters or formating. 
} 

MessageBox.Show(output); 
0

예,이 출력되지 않게 전체 배열 :

string toDisplay = string.Join(Environment.NewLine, sArray); 
MessageBox.Show(toDisplay);