0

2 次元配列と、テキスト ボックスからリスト ボックスに値を追加するための追加ボタンと、リスト ボックスと配列から選択した値を削除するための削除ボタンがあります。

2 次元配列であるため、このコード行は機能しません。

名前[lstindex] = null;

    string[,] names = new string[10,3];

    const int STUDENT_NAME = 0;
    const int STUDENT_ID = 1;
    const int MAJOR = 2;
    private void btnExit_Click(object sender, EventArgs e)
    {

        Close();
    }
    private void  AssignArray(int Row)
    {
        names[Row,STUDENT_NAME] = txtStudentName.Text;
        names[Row, STUDENT_ID] = txtStdBox.Text;
        names[Row, MAJOR] = txtMJbox.Text;
    }
    private string BuildStudent(int Row)
    {
        string answser = "";
        int UpperLimit = names.GetUpperBound(1);
        for (int column = 0; column <= UpperLimit; column++)
        {
            answser += names[Row, column] + "";
        }
        return answser;

    }

     private void btnAdd_Click(object sender, EventArgs e)
    {
        //find location to use array
        int position = lstStudents.Items.Count;
        AssignArray(position);

        lstStudents.Items.Add(BuildStudent(position));
        names[position,MAJOR] = txtMJbox.Text;
        //find last valid subscript

            int maxIndex = names.GetUpperBound(0);
        //if using last valid  subscript disable add button
            if (position == maxIndex)
            {
                btnAdd.Enabled = false;
            }
    }

    private void btnUpdate_Click(object sender, EventArgs e)
    {
        int index = lstStudents.SelectedIndex;
        if (index != -1)
        {  
            //update array
            AssignArray(index);
            //names[index, STUDENT_NAME] = txtStudentName.Text;
            //remove old entry
            lstStudents.Items.RemoveAt(index);
            //put a new entry in that spot
                lstStudents.Items.Insert(index,names[index, STUDENT_NAME]);
            //highlight entry for user
            lstStudents.SelectedIndex = index;
        }
        else
        {
        MessageBox.Show("seleect student, then click update","error");
        }
    }
    private void btnDelete_Click(object sender, EventArgs e)
    {


        int lstindex = lstStudents.SelectedIndex;
        //Delete the data for a student in the array
        //and listbox, and keep the array and listbox synchronized.
       names[lstindex] = null;

        lstStudents.Items.RemoveAt(lstindex);

    }
4

3 に答える 3

1

配列からアイテムを「削除」するには、そのインデックスより上のすべてのインデックスを 1 つ下にシフトしてから、最後のスロットが破棄されるように配列を縮小する必要があります。*インデックスを削除済みとして「マーク」する遅延削除を使用して、安全に使用できることを確認することもできます (ただし、コードが少し複雑になります)。

多次元配列を使用する必要がありますか? 各生徒を代表するクラスに切り替えると、生活がとても楽になります!...

編集:

    private void btnDelete_Click(object sender, EventArgs e)
    {
        int lstindex = lstStudents.SelectedIndex;
        if (lstindex != -1)
        {
            //Delete the data for a student in the array
            //and listbox, and keep the array and listbox synchronized.
            //names[lstindex] = null;

            lstStudents.Items.RemoveAt(lstindex);

            // starting at the index to remove, copy the value from the next index up, then iterate
            // this will shift everything down to replace the item being deleted
            for (int i = lstindex; i < names.GetUpperBound(0) - 1; i++)
            {
                names[i, STUDENT_NAME] = names[i + 1, STUDENT_NAME];
                names[i, STUDENT_ID] = names[i + 1, STUDENT_ID];
                names[i, MAJOR] = names[i + 1, MAJOR];
            }
            //clear out the last entry:
            names[names.GetUpperBound(0), STUDENT_NAME] = "";
            names[names.GetUpperBound(0), STUDENT_ID] = "";
            names[names.GetUpperBound(0), MAJOR] = "";
        }
    }
于 2013-05-13T18:35:42.370 に答える