0

我有一个二维数组和一个添加按钮,用于将文本框中的值添加到列表框中,我有一个删除按钮,用于从列表框和数组中删除选定的值。

这行代码不起作用,因为它是一个二维数组。

名称[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

要从数组中“删除”一个项目,您必须将该索引上方的所有索引向下移动一个,然后缩小数组,以便丢弃最后一个插槽。*您也可以使用延迟删除,将索引“标记”为已删除,这样您就知道使用它是安全的(但这会使代码更加复杂)。

您是否需要使用多维数组?切换到一个班级来代表每个学生将使生活变得更加轻松!...

编辑:

    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 回答
0

那这个呢:

names[lstindex] = new string[3];

不确定它是否有效,但如果我错了,我会删除我的帖子。

于 2013-05-13T18:41:12.247 回答
0

您的 btnDelete_click 应该通过创建一个新数组和Array.Copy您需要保留的项目从数组中“删除”一个项目。就像这个片段显示的那样,可以作为删除方法的一个插件。

// create a new array, one less than source
string[,] namesNew = new string[
    names.GetLength(0)-1,
    names.GetLength(1)];

int lstindex = lstStudents.SelectedIndex;
// calc offset because Array.Copy does not do 
// multi-dimensional arrays
int offset = names.GetUpperBound(1)+1;
// copy from source up to index 
Array.Copy(names, 
            0, 
            namesNew, 
            0, 
            lstindex * offset);
// copy remainder from source after index
int itemsLeft = names.GetLength(0)-lstindex-1;
Array.Copy(names,
            (lstindex+1) * offset,
            namesNew,
            lstindex * offset,
            itemsLeft * offset);
// dereferecne old array 
// and hold on to the new array
names = namesNew;

lstStudents.Items.RemoveAt(lstindex);
于 2013-05-14T18:37:09.613 回答