如何加载数组列表中的所有对象

时间:2015-11-17 22:55:15

标签: c# arrays

我试图找出如何通过数组对象列表加载。我能够检索数组中的最后一个对象,但在此之后它不会让我加载任何其他对象。这是我的代码的一部分。正如您所看到的,它将对象保存到List中,但是当我单击loadLastBtn时,它将仅加载最新的条目,如果我再次点击它,则不会加载任何内容。

    List<Members> lstMembers = new List<Members>();
    private int hold;
    private void submitBtn_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(firstNameTxt.Text) || string.IsNullOrEmpty(lastNameTxt.Text)
           || string.IsNullOrEmpty(userNameTxt.Text) ||
           string.IsNullOrEmpty(passwordTxt.Text) || string.IsNullOrEmpty(confPassTxt.Text)
           || string.IsNullOrEmpty(majorBox.Text) || string.IsNullOrEmpty(specialtyBox.Text))
        {
            MessageBox.Show("You must enter in all fields before moving forward");
        }
        else
        {
            Members m1 = new Members(firstNameTxt.Text, lastNameTxt.Text, userNameTxt.Text,
                                        passwordTxt.Text, confPassTxt.Text, majorBox.Text,
                                        specialtyBox.Text);
            lstMembers.Add(m1);
        }
    }


   private void loadLastBtn_Click(object sender, EventArgs e)
    {
        firstNameTxt.Text = lstMembers[hold].FirstName;
        lastNameTxt.Text = lstMembers[hold].LastName;
        userNameTxt.Text = lstMembers[hold].UserName;
        passwordTxt.Text = lstMembers[hold].Password;
        confPassTxt.Text = lstMembers[hold].ConfPassword;
        majorBox.Text = lstMembers[hold].Major;
        specialtyBox.Text = lstMembers[hold].Specialty;

        hold++;
    }

我已经编辑了我的答案。现在,每次我点击loadLastBtn时,这将逐个打印每个用户,但它确实从第一个用户到最后一个用户显示它们,我需要它将最后一个用户淋到第一个用户。

2 个答案:

答案 0 :(得分:0)

private int hold = lstMembers.Count -1;
private void loadLastBtn_Click(object sender, EventArgs e)
{

    firstNameTxt.Text = lstMembers[hold].FirstName;
    lastNameTxt.Text = lstMembers[hold].LastName;
    userNameTxt.Text = lstMembers[hold].UserName;
    passwordTxt.Text = lstMembers[hold].Password;
    confPassTxt.Text = lstMembers[hold].ConfPassword;
    majorBox.Text = lstMembers[hold].Major;
    specialtyBox.Text = lstMembers[hold].Specialty;

    hold--;
}

答案 1 :(得分:0)

你需要的是这样的东西:

// -1 Indicates that you should start at the end of the list
int index = -1; 

private void loadButton_Click(object sender, EventArgs e)
{
    if (members != null && members.Count > 0) // Avoid accessing if list is empty or null
    {
        if (index == -1)
            index = members.Count - 1;

        firstNameTxt.Text = lstMembers[index].FirstName;
        lastNameTxt.Text = lstMembers[index].LastName;
        userNameTxt.Text = lstMembers[index].UserName;
        passwordTxt.Text = lstMembers[index].Password;
        confPassTxt.Text = lstMembers[index].ConfPassword;
        majorBox.Text = lstMembers[index].Major;
        specialtyBox.Text = lstMembers[index].Specialty;

        if (index == 0) // Reached beginning of array
            index = -1; // Indicate that next time the last element must be accessed
        else
            --index;
    }
}