【发布时间】:2018-06-05 06:03:59
【问题描述】:
我正在创建一个宠物列表,并尝试为每只宠物添加照片。我有这个列表,但我以前从未使用过 c# 中的图像,所以我有点卡住了。
到目前为止,我有从超级宠物类继承的品种类(虎斑猫、暹罗猫、哈士奇犬和 chiwawa)。以下是宠物类中的属性:
字符串名称,长芯片,DateTime到达日期,布尔值adoptedStatus, 图片图片
在我的主代码文件中,我创建了一些新宠物,然后将它们添加到宠物列表中:
Tabby newTabby1 = new Tabby("sunshine", 22222222222222222, new DateTime(2016, 2, 24), false, Image.FromFile("images/sunshine.jpg"));
Chiwawa newChi1 = new Chiwawa("tony", 33333333333333333, new DateTime(2016, 2, 24), false, Image.FromFile("images/chi.jpg"));
Siamese newsia1 = new Siamese("felix", 44444444444444444, new DateTime(2016, 3, 11), false, Image.FromFile("images/felix.jpg"));
Husky newHusk1 = new Husky("fluffs", 55555555555555555, new DateTime(2016, 2, 24), false, Image.FromFile("images/husky.jpg"));
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;
我的项目文件中有一个 images 文件夹,其中包含 jpg 格式的宠物照片。我不知道如何让照片显示在我的列表中,旁边是名称等其他属性。我也不确定我在创建宠物时是否正确放置了照片 ex: (Image.FromFile("images/husky.jpg")。
这也是我的宠物班:
public abstract class Pet
{
#region Fields
protected long chip;
protected DateTime ArrivalDate;
public string name;
protected bool AdoptedStatus;
public static int petCount = 0;
public Image image;
#endregion End of Fields
#region Constructors
public Pet()
{
chip = 0;
AdoptedStatus = false;
petCount++;
}
public Pet(string name, long chip, DateTime arrivalDate, Boolean adoptedStatus, Image image)
{
this.chip = chip;
ArrivalDate = arrivalDate;
AdoptedStatus = adoptedStatus;
this.name = name;
petCount++;
this.image = image;
}
#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();
// public override string ToString()
// {
// return $"{Model}\n MRSP:${Mrsp}\n Vin:{Vin}\n Delivered:{DeliveryDate}\n Sold: {soldStatus}\n";
// }
#endregion End of Methods
}
【问题讨论】:
-
如何在表单上显示列表?
-
@PepitoSh 我用 petList.Text 显示它
-
请发布MVCE,这意味着您还应该包括您的
Pet课程和用于显示您的宠物的代码。目前我们甚至不知道您使用的是哪个 GUI-Toolkit(即 WinForms 或 WPF)。