使用C#在visual studio中的GUI标签中打印数组

时间:2014-07-07 05:00:50

标签: c# winforms user-interface

所以我想弄清楚如何在我的程序中打印一个保龄球名称和分数列表。使用控制台时很简单。我想弄清楚如何将数组打印到GUI上的标签?有什么建议?我试过if / else if语句,但显然它不起作用。

这是我的表单代码:

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

namespace Project_9
{
public partial class Form1 : Form
{
    //Declare a reference variable to the class
    private BowlingTeam myBowlingTeam;

    const int MAX = 20;
    const int TWO = 2;
    int size = 0;
    int count = 0;

    //Array to store the names and scores so they can be printed
    string[] nameAndScoreArray = new string[MAX];

    public Form1()
    {
        InitializeComponent();
        myBowlingTeam = new BowlingTeam();//Create a BowlingTeam object with the default constructor
    }

    //ExitToolStripMenuItem1_Click
    //Purpose: To close the application when clicked
    //Parameters: The sending object and the event arguments
    //Returns: nothing
    private void exitToolStripMenuItem_Click(object sender, EventArgs e)
    {
        this.Close();
    }



    //Enter Button Clicked
    //Purpose: To store the info in an array when the button is pressed
    //Parameters: The sending object and the event arguments
    //Returns: nothing
    private void button1_Click(object sender, EventArgs e)
    {

        //Get info from the text box and store it in a variable
        string aNameAndScore = nameTextBox.Text;
        //Set the name and score 
        myBowlingTeam.SetNameAndScore(aNameAndScore);

        //Get the information from the text box and store it in a variable
        string nameAndScore = myBowlingTeam.GetNameAndScore();

        if (nameAndScore != "")
        {
            string printName = "";
            string printScore = "";

            //Split the string into two separte pieces of data
            myBowlingTeam.SplitAndDisperseArray();

            //Print the name of the bowler in the score box
            string name = myBowlingTeam.GetAName();

            //Change the int into a string then
            //print the score of the bowler in the score box
            int score = myBowlingTeam.GetAScore();
            string strScore = String.Format("{0:d}", score);


            //Store the name and score in an array to use them to print out the 
            //names and scores later
            if (size < MAX)
            {
                nameAndScoreArray[size] = name;
                nameAndScoreArray[size + 1] = strScore;
                printName = nameAndScoreArray[size];
                printScore = nameAndScoreArray[size + 1];
            }

            string strNameAndScore = printName + " " + printScore;

            if (scoreLabel.Text == "")
            {
                scoreLabel.Text = strNameAndScore;
            }
            else if(scoreLabelTwo.Text == "")
            {
                scoreLabelTwo.Text = strNameAndScore;
            }
            else if(scoreLabelThree.Text == "")
            {
                scoreLabelThree.Text = strNameAndScore;
            }
            else if(scoreLabelFour.Text == "")
            {
                scoreLabelFour.Text = strNameAndScore;
            }
            else if(scoreLabelFive.Text == "")
            {
                scoreLabelFive.Text = strNameAndScore;
            }
            else if(scoreLabelSix.Text == "")
            {
                scoreLabelSix.Text = strNameAndScore;
            }
            else if(scoreLabelSeven.Text == "")
            {
                scoreLabelSeven.Text = strNameAndScore;
            }
            else if(scoreLabelEight.Text == "")
            {
                scoreLabelEight.Text = strNameAndScore;
            }
            else if(scoreLabelNine.Text == "")
            {
                scoreLabelNine.Text = strNameAndScore;
            }
            else if(scoreLabelTen.Text == "")
            {
                scoreLabelTen.Text = strNameAndScore;
            }

            //Clear the text box
            nameTextBox.Clear();
        }
        else//If there is nothing in the text box
        {
            //Calculate the highest score and get the bowler's name
            int highestScore = myBowlingTeam.CalcHighestScore();
            string highestName = myBowlingTeam.GetHighestBowlerName();

            //Calaculate the lowest score and get the bowler's name
            int lowestScore = myBowlingTeam.CalcLowestScore();
            string lowestName = myBowlingTeam.GetLowestBowlerName();

            //Calculate the avg acore
            double avgScore = myBowlingTeam.CalcAvgScore();

            //Convert the numbers into strings
            string strHighScore = String.Format("{0:d}", highestScore);
            string strLowScore = String.Format("{0:d}", lowestScore);
            string strAvgScore = String.Format("{0:f2}", avgScore);

            //Display the information in the appropriate labels
            highScoreNameLabel.Text = highestName;
            highScoreLabel.Text = strHighScore;

            lowScoreNameLabel.Text = lowestName;
            lowScoreLabel.Text = strLowScore;

            avgScoreLabel.Text = strAvgScore;

        }
    }

    //Print Scores Method
    //Purpose: To print the names and scores
    //Parameters: An Array
    //Returns: Nothing
    public void PrintNamesAndScores(ref string[] anArray)
    {
        for(int i = 0; i < size; i++)
        {
            Console.WriteLine("{0} {1:d}", anArray[i], anArray[i + 1]);
            i++;
        }
    }

    //Nothing
    private void nameTextBox_TextChanged(object sender, EventArgs e)
    {

    }

    // nothing
    private void nameTextBox_KeyDown(object sender, KeyEventArgs e)
    {

    }


    //Nothing
    private void scoreTextBox_KeyDown(object sender, KeyEventArgs e)
    {

    }
    //Nothing
    private void label10_Click(object sender, EventArgs e)
    {

    }
    //nothing
    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


}

}

这是我的班级代码:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;

 namespace Project_9
{

class BowlingTeam
{
     const int MAX = 10;

    //local variables
     int sizeOfName = 0;
     int sizeOfScore = 0;

    //Declare Private Data Members
    private string nameAndScore = "";
    private string name = "";
    private int score = 0;
    private string[] nameArray = new string[MAX];
    private int[] scoreArray = new int[MAX];
    private string[] nameAndScoreArray = new string[MAX];

    //Default Constructor
    //Purpose: To set the initial values of an object
    //Parameters: None
    //Returns: Nothing
    public BowlingTeam()
    {
        nameAndScore = "";
        name = "";
        score = 0;
        for(int i = 0; i < MAX; i++)
        {
            nameArray[i] = "";
        }
        for(int i = 0; i < MAX; i++)
        {
            scoreArray[i] = 0;
        }
        for (int i = 0; i < MAX; i++)
        {
            nameAndScoreArray[i] = "";
        }
    }

    //Parameterized Constructor
    //Purpose: To set the values of an object
    //Parameters: None
    //Returns: Nothing
    public BowlingTeam(string aString)
    {
        nameAndScore = aString;
        name = "";
        score = 0;
        for (int i = 0; i < MAX; i++)
        {
            nameArray[i] = "";
        }
        for (int i = 0; i < MAX; i++)
        {
            scoreArray[i] = 0;
        }
        for (int i = 0; i < MAX; i++)
        {
            nameAndScoreArray[i] = "";
        }
    }

    //Split the Input Method
    //Purpose: To Split up the data in the array
    //Parameters: An array of strings
    //Returns: Nothing
    public void SplitAndDisperseArray()
    {
            nameAndScoreArray = nameAndScore.Split();
            name = nameAndScoreArray[0];
            score = int.Parse(nameAndScoreArray[1]); 

        //Place the name and the score in their one arrays
            PlaceInNameArray(name);
            PlaceInScoreArray(score);
    }

    //Find Highest Score Method
    //Purpose: To find the highest score
    //Parameters: An array of int
    //Returns: An int
    public int CalcHighestScore()
    {
        int highestScore = scoreArray[0];

        for (int i = 1; i < sizeOfScore; i++ )
        {
            if (highestScore > scoreArray[i])
            {
                highestScore = highestScore;
            }
            else
            {
                highestScore = scoreArray[i];
            }
        }
        return highestScore;
    }

    //Find Lowest Score Method
    //Purpose: To find the lowest score
    //Parameters: An array of int
    //Returns: An int
    public int CalcLowestScore()
    {
        int lowestScore = scoreArray[0];

        for (int i = 1; i < sizeOfScore; i++)
        {
            if (lowestScore < scoreArray[i])
            {
                lowestScore = lowestScore;
            }
            else
            {
                lowestScore = scoreArray[i];
            }
        }
        return lowestScore;
    }

    //Calulate Avg. Score Method
    //Purpose: To calculate the avg score
    //Parameters: An array of int
    //Returns: An double
    public double CalcAvgScore()
    {
        int sum = 0;
        double avg = sizeOfScore;
        //Add up all of the elements in the array
        for (int i = 0; i < sizeOfScore; i++)
        {
            sum += scoreArray[i];
        }
        //Divide the sum by the size of the array
        return avg = sum / avg;
    }

    //Set Score Array Method
    //Purpose: To put scores in the score array
    //Parameters: An int
    //Returns: Nothing
    public void PlaceInScoreArray(int aScore)
    {
        scoreArray[sizeOfScore] = score;
        sizeOfScore++;
    }


    //Set Name Array Method
    //Purpose: To put names in the names array
    //Parameters: A string
    //Returns: Nothing
    public void PlaceInNameArray(string aName)
    {
        nameArray[sizeOfName] = name;
        sizeOfName++;
    }

    //Get Name and Score Method
    //Purpose: To get the name and score 
    //Parameters: None
    //Returns: A string
    public string GetNameAndScore()
    {
        return nameAndScore;
    }

    //Set Name and Score Method
    //Purpose: To set the name and score 
    //Parameters: A String
    //Returns: None
    public void SetNameAndScore(string aNameAndScore)
    {
        nameAndScore = aNameAndScore;
    }

    //Get Bowler Name Of Highest Score Method
    //Purpose: To get the name of the bowler with the highest score
    //Parameters: None
    //Returns: A String
    public string GetHighestBowlerName()
    {
         int highestScore = scoreArray[0];
         string bowlersName = nameArray[0];

         for (int i = 1; i < sizeOfScore; i++)
         {
             if (highestScore > scoreArray[i])
             {
                 highestScore = highestScore;
                 bowlersName = bowlersName;
             }
             else
             {
                 highestScore = scoreArray[i];
                 bowlersName = nameArray[i];
             }
         }
         return bowlersName;
    }

    //Get Bowler Name Of Lowest Score Method
    //Purpose: To get the name of the bowler with the lowest score
    //Parameters: None
    //Returns: A String
    public string GetLowestBowlerName()
    {
        int lowestScore = scoreArray[0];
        string bowlersName = nameArray[0];

        for (int i = 1; i < sizeOfScore; i++)
        {
            if (lowestScore < scoreArray[i])
            {
                lowestScore = lowestScore;
                bowlersName = bowlersName;
            }
            else
            {
                lowestScore = scoreArray[i];
                bowlersName = nameArray[i];
            }
        }
        return bowlersName;
    }

    //Print Bowler's Names
    //Purpose: To print the names the bowlers
    //Parameters: None
    //Returns: A String
    public string GetAName()
    {
        string name = "";
        for (int i = 0; i < sizeOfName; i++)
        {
            name = nameArray[i];
        }
            return name;
    }

    //Print Bowler's Scores
    //Purpose: To print the names the bowlers
    //Parameters: None
    //Returns: A String
    public int GetAScore()
    {
        int score = 0;
        for (int i = 0; i < sizeOfScore; i++)
        {
            score = scoreArray[i];
        }
            return score;
    }
}

}

1 个答案:

答案 0 :(得分:0)

作为一般解决方案,我建议使用LINQ输出每个项目一个String的列表,然后使用String.Join将它们合并为一个String最简洁的选择,例如

myLabel.Text = string.Join(Environment.NewLine, myArray.Select(obj => string.Format("{0}, {1}", obj.Property1, obj.Property2)));