我想遍历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
}