c# - 遍历列表并为每个列表项提供链接
问题描述
我想遍历 C# 表单应用程序中的列表,用户将在表单上看到该列表并可以单击与每个列表项关联的按钮或链接。例如:我知道使用 ruby 可以使用以下代码实现此目的:
<% @pets.each do |pet| %>
<%= link_to 'Edit', edit_pet_path(pet) %>
<% end %>
我不想打开链接,而是想打开第二个弹出窗口,其中包含单击的单个项目的信息。
现在,我当前循环遍历列表以显示项目,并且还有一个 petsList_Click 方法,该方法在单击列表时会打开第二个弹出窗口。问题是它点击整个列表而不是列表中的单个项目。如果用户单击列表的第一项,我只想将该信息传递给弹出窗口。
这是我的列表和点击方法的主要表单:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Tabby newTabby1 = new Tabby("sunshine", 22222222222222222, new DateTime(2016, 2, 24), false);
Chiwawa newChi1 = new Chiwawa("tony", 33333333333333333, new DateTime(2016, 2, 24), false);
Siamese newsia1 = new Siamese("felix", 44444444444444444, new DateTime(2016, 3, 11), false);
Husky newHusk1 = new Husky("fluffs", 55555555555555555, new DateTime(2016, 2, 24), false);
List<Pet> list = new List<Pet>();
list.Add(newTabby1);
list.Add(newChi1);
list.Add(newsia1);
StringBuilder builder = new StringBuilder();
foreach (var item in list)
{
Console.WriteLine("list item " + item.Chip );
builder.Append(item.name + " " + item.Chip + " " + item.arrivalDate + " status" + item.adoptedStatus).Append("\n");
}
string result = builder.ToString(); // Get string from StringBuilder
petList.Text = result;
petCount.Text = "Pets Available : " + Pet.petCount;
}
private Image ImageUrl(object p)
{
throw new NotImplementedException();
}
private void petList_Click(object sender, EventArgs e)
{
PetInfoForm aPetInfoForm = new PetInfoForm();
aPetInfoForm.Closed += (s, args) => this.Close();
aPetInfoForm.Show();
}
}
这是我的 PetInfoForm
public partial class PetInfoForm : Form
{
public PetInfoForm()
{
InitializeComponent();
}
private void PetInfoForm_Load(object sender, EventArgs e)
{
}
}
以防万一。这是我的宠物类,品种类继承自:公共抽象类宠物{
#region Fields
protected long chip;
protected DateTime ArrivalDate;
public string name;
protected bool AdoptedStatus;
public static int petCount = 0;
#endregion End of Fields
#region Constructors
public Pet()
{
chip = 0;
AdoptedStatus = false;
petCount++;
}
public Pet(string name, long chip, DateTime arrivalDate, Boolean adoptedStatus)
{
this.chip = chip;
ArrivalDate = arrivalDate;
AdoptedStatus = adoptedStatus;
this.name = name;
petCount++;
}
#endregion End of Constructors
#region Properties
public int PetCount
{
get
{
return petCount;
}
}
public long Chip
{
get
{
return chip;
}
set
{
if (value > 0)
chip = value;
else
chip = 0;
}
}
public DateTime arrivalDate { get; set; }
public Boolean adoptedStatus { get; set; }
#endregion End Properties
#region Methods
public bool UpdateStatus() => adoptedStatus = true;
public int UpdateInventory() => petCount = petCount - 1;
public abstract void Noise();
#endregion End of Methods
}
解决方案
推荐阅读
- c++11 - QComboBox currentIndexChanged 在 QGraphicsView 上无法正常工作
- angular - Firestore 中批量事务更新期间的问题
- javascript - Javascript fetch() 故障转移 URL
- api - 如何在单个 docker 容器中托管多个烧瓶应用程序?
- pointers - 由于自动生成的指针,无法在消息中嵌入消息
- apache-spark - 如何通过 Spark 运行 HDFS 文件系统命令?
- python - 在python中记录所有http请求
- node.js - EJS条件导入不显示html
- java - 2.1 企业 bean 上的资源引用标记
- python-3.x - 如何使用 asyncio 和 aiohttp 进行循环而不是 for 循环?