0

[給与の計算]ボタンをクリックして従業員オブジェクト間を移動し、各オブジェクトの給与を表示できるようにしたいと思います。問題は、最初のものを超えて移動しないことです。答えが明らかな場合は申し訳ありません。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private const int SIZE = 4; // the size of the array
        // create array to hold employee references
        Employee[] employees = new Employee[SIZE];

        public Form1()
        {
            InitializeComponent();

            // Create some employee objects
            employees[0] = 
                new Hourly(1, "H. Potter", 
                           "Privet Drive", "201-9090", 40, 12.00);
            employees[1] = 
                new Salaried(2, "A. Dumbledore", 
                             "Hogewarts", "803-1230", 1200);
            employees[2] = 
                new Hourly(3, "R. Weasley", 
                           "The Burrow", "892-2000", 40, 10.00);
            employees[3] = 
                new Salaried(4, "R. Hagrid", 
                             "Hogwarts", "910-8765", 1000);


        }

        private void buttonCalcPay_Click(object sender, EventArgs e)
        {
            int index = 0;
            string ostring = ("Fluffshuffle Electronics check no.");
            ostring += string.Format("{0}", index);
            ostring += Environment.NewLine;
            ostring += Environment.NewLine;
            ostring += "       pay to the order of";
            ostring += employees[index].Name;
            ostring += Environment.NewLine;
            ostring += string.Format("{0:C}", employees[index].CalcPay());
            ostring += Environment.NewLine;
            ostring += Environment.NewLine;
            ostring += "             First National Bank";
            textBoxCheck.Text = ostring;

            textBoxName.Text = employees[index].Name;
            textBoxAddress.Text = employees[index].Address;
            textBoxPhone.Text = employees[index].PhoneNum;
            textBoxEmpNum.Text = string.Format("{0}", employees[index].EmpNum);
            index++;

            //see if object is hourly
            Hourly someEmp1 = employees[index] as Hourly;
            if (someEmp1 != null)
            {
                textBoxHours.Text = 
                    string.Format("{0:F2}", someEmp1.HoursWorked);
                textBoxWage.Text = 
                    string.Format("{0:F2}", someEmp1.HourlyWage);
                textBoxSalary.Clear();

            }
            //not hourly, must be salary
            Salaried someEmp2 = employees[index] as Salaried;
            if (someEmp2 != null)
            {
                textBoxHours.Clear();
                textBoxWage.Clear();
                textBoxSalary.Text = string.Format("{0:F2}", someEmp2.Salary);

            }

            else
            {
                buttonCalcPay.Enabled = false;
                textBoxName.Clear();
                textBoxAddress.Clear();
                textBoxEmpNum.Clear();
                textBoxPhone.Clear();
                textBoxHours.Clear();
                textBoxWage.Clear();
                textBoxSalary.Clear();
            }

        }



}
}
4

2 に答える 2

1

クラス変数としてインデックスを作成し、buttonCalcPay_Click の最後でインクリメントします。そのインデックスをループしたい場合は、インクリメント中にそれが 3 より大きいかどうかを確認します。そうであれば、もう一度 0 に設定します。

于 2012-09-18T04:25:34.140 に答える
0

常に「buttonCalcPay」ボタンをクリックすると、インデックスをゼロに設定します。これは、常に最初の従業員を獲得する理由です。SIZE変数を次のように宣言した場所で宣言します。

private const int SIZE = 4; // the size of the array
private int index = 0;

次に、ボタンクリックイベントに次のコードを記述します。

    private void buttonCalcPay_Click(object sender, EventArgs e)
    {
        if(index < SIZE)
        {
             //Your code goes here
        }
        index++;
    }

そして、文字列の代わりにSystem.Text.StringBuilderを使用する必要があると思います。使用する

    System.Text.StringBuilder sBuilder = new StringBuilder();
    sBuilder.Append("Your text goes here");
    sBuilder.Append("\nAnother text");

テキストを追加します。次に、以下を使用する必要があるときに文字列に変換します。

     sBuilder.ToString();

それが役に立てば幸い。

于 2012-09-18T05:57:05.217 に答える