[英]Merge an array with a ListView
我一直在努力完成我的一個項目,目前正在研究 C#,但遇到了一個問題。
任務是創造生病或健康的動物,並將它們分成不同的 arrays。
一切正常,直到我嘗試將數組合並到ListView
。
我想做的是使用數組的信息並將其插入到列表的列中,我不知道該怎么做。 它的目的是讓每個數組與列表進行通信,當您從任何數組中添加或刪除動物時,它會從列表中刪除特定的動物,添加動物也是如此。
這是我一直在嘗試做的事情,但這不是一件好事。 我需要從數組而不是文本框中收集信息。
private void btnAddStack_Click(object sender, EventArgs e)
{
if (txtCode.Text == "Example: 1234")
{
MessageBox.Show("Fill in animal's code in order to proceed", "Error");
txtCode.Focus();
return;
}
if (txtName.Text == "Example: Lion")
{
MessageBox.Show("Fill in animal's name in order to proceed", "Error");
txtName.Focus();
return;
}
if (txtWeight.Text == "Example: 100")
{
MessageBox.Show("Fill in animal's weight in order to proceed", "Error");
txtWeight.Focus();
return;
}
if (rbFemale.Checked == false && rbMale.Checked == false)
{
MessageBox.Show("Fill in animal's gender in order to proceed", "Error");
return;
}
if (rbNoMarine.Checked == false && rbYesMarine.Checked == false)
{
MessageBox.Show("Choose if the animal is marine or not in order to proceed", "Error");
return;
}
btnTakeCareSt.Enabled = true;
service.addNewAnimal(int.Parse(txtCode.Text), txtName.Text, float.Parse(txtWeight.Text), CheckMarine(), CheckUrgent(), CheckGender());
lvStack.Items.Add(new ListViewItem(new[] {txtName.Text, txtCode.Text, txtWeight.Text, BoolToString(), CheckMarine().ToString()}));
}
Array 的大小是固定的,你不能動態地添加或刪除它的項目。 也許使用 List 是更好的選擇。
這里我有一個class的Student
來測試,你可以定義一個List來存儲它的實例。
/// <summary>
/// Student class for test
/// </summary>
class Student
{
public string Name { get; set; }
public int Age { get; set; }
}
在表格 class 中定義一個列表:
// Define a list to store students
List<Student> students = new List<Student>() { new Student { Name = "A", Age = 10 },
new Student { Name = "B", Age = 9 },
new Student { Name = "C", Age = 14 } };
然后你可以參考下面的代碼將列表中的項目存儲到ListView中。
private void Form1_Load(object sender, EventArgs e)
{
listView1.View = View.Details;
this.listView1.Columns.Add("Name", 120, HorizontalAlignment.Left);
this.listView1.Columns.Add("Age", 120, HorizontalAlignment.Left);
this.listView1.BeginUpdate();
for (int i = 0; i < students.Count; i++)
{
ListViewItem lvi = new ListViewItem();
lvi.Text = students[i].Name;
lvi.SubItems.Add(students[i].Age.ToString());
this.listView1.Items.Add(lvi);
}
this.listView1.EndUpdate();
}
添加新項目,請參考:
private void btnAdd_Click(object sender, EventArgs e)
{
// Add a new student named "D" and age is 17;
string name = "D";
int age = 17;
// Add new student to list and ListView
Student student = new Student() { Name = name, Age = age };
students.Add(student);
ListViewItem lvi = new ListViewItem();
lvi.Text = student.Name;
lvi.SubItems.Add(student.Age.ToString());
this.listView1.Items.Add(lvi);
}
至於刪除特定項目,您可以使用:
private void btnRemove_Click(object sender, EventArgs e)
{
// Remove the student whose name is "B"
string name = "B";
// Remove the data in list
var found = students.Find(x => x.Name == name);
if (found != null) students.Remove(found);
// Remove the item in ListView
ListViewItem foundItem = this.listView1.FindItemWithText(name, true, 0);
if (foundItem != null)
{
listView1.Items.Remove(foundItem);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.