Reputation: 371
I have a list of a class, how do I find edit the class at a particular list location.
I am trying to update a List<Class>
, but I have no idea how to pass the values back to the class.
i.e. I do not know how to call the list at location 2 changed the class values 1,2,3,4,5 to 6,7,8,9,0
the class is a form and the method I want to use will essentially be:
public FormStudent(int a, string b, int c, double d, char f)
{
textBoxID.Text = a.ToString();
textBoxName.Text = b;
textBoxCredits.Text = c.ToString();
textBoxTuition.Text = d.ToString();
if (f == 'R')
radioButtonResident.Checked = true;
else
adioButtonNonResident.Checked = true;
}
My list is:
private List<Student> studentList = new List<Student>();
oh! and to make it even more difficult I am getting the list value from a listView via
private void buttonUpdate_Click(object sender, EventArgs e)
{
Student stu = new Student();
ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;
int count = selectedItems.Count;
for (int i = 0; i < count; i++)
{
// I NEED THE UPDATE HERE TO CALL \/
FormStudent stuInfoForm = new FormStudent(stu.Id, stu.Name, stu.Credits, stu.Tuition, stu.Residency);
studentList.RemoveAt(i);
stuInfoForm.Owner = this;
stuInfoForm.ShowDialog();
}
refreshList();
}
Upvotes: 0
Views: 6695
Reputation: 882
I only use the Id member of student to pass but you can pass it all.
Basically in your main form you had:
private List<Student> studentList = new List<Student>();
private void listView1_DoubleClick(object sender, EventArgs e) {
ListView.SelectedListViewItemCollection selectedItems = listView1.SelectedItems;
if (selectedItems != null && selectedItems.Count > 0) {
ListViewItem item = selectedItems[0];
Form2 form = new Form2(item.Text);
form.Owner = this;
form.ShowDialog();
// Now get the values from the form.
Student updateStudent = studentList.Find(o => o.Id == form.Student.Id);
if (updateStudent != null) {
updateStudent.Id = form.Student.Id;
// Update the rest of the members.
}
// Re-populate your list using the updated student list.
}
}
Now in your second form student form:
private Student _student = new Student();
public Form2(string id) {
InitializeComponent();
textBox1.Text = id;
}
public Student Student {
get {
return _student;
}
}
private void button1_Click(object sender, EventArgs e) {
_student.Id = Convert.ToInt32(textBox1.Text);
this.Close();
}
Upvotes: 0
Reputation: 23462
Maybe you are missing a databind? It is hard to actually understand what you are doing. It seems like you are removing students at the same time as you are creating them, seems weird.
Upvotes: 0
Reputation: 9075
just index it:
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
...
list.Add(5);
list[2] = 7;
Upvotes: 2