在循环中创建图像列表对象
本文关键字:列表 对象 图像 创建 循环 | 更新日期: 2023-09-27 17:56:44
我有非常简单的列表视图项目创建逻辑。我将 dataGridView 中的所有 col 标头名称存储到 ColNamesForm1 数组中,然后仅比较最后 5 个字符是否与 (num) 或 (cat) 字符串匹配。对于这两个选项,我将不同的图片附加到列表视图lvMoveFrom中使用存储在静态类OtherFunctions中的填充函数。
但是我的代码有问题,因为在上次迭代后,它从最后一列附加图像 - 如果第一列是 (num) 并且最后一列是 (cat),则 listview 中的图像都是相同的 - 来自 cat 图像。
如何解决此问题?我想知道为每一列创建新的 ImageList 对象,但我不知道,如何动态地做到这一点,例如使用循环索引 i。
拜托,你能帮忙解决我的问题吗?
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < ColNamesForm1.Length; i++)
{
if (ColNamesForm1[i].ToString().Substring(0, 4).ToUpper() != "COL_" && Regex.IsMatch(ColNamesForm1[i].ToString().Substring(4, 1), @"^'d+$") == false)
{
if (ColNamesForm1[i].ToString().Substring(ColNamesForm1[i].ToString().Length - 5) == "(num)")
{
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), @"C:'pictures'num.png");
}
else
{
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), @"C:'pictures'cat.png");
}
}
}
}
public static void Populate(ListView lv, string itemName, string pathToPicture)
{
ImageList img = new ImageList();
img.ImageSize = new Size(30, 30);
img.Images.Add(Image.FromFile(pathToPicture));
lv.SmallImageList = img;
lv.Items.Add(itemName, 0);
}
问题
所以基本上是这样的:
ImageList img = new ImageList();
img.ImageSize = new Size(30, 30);
img.Images.Add(Image.FromFile(pathToPicture));
lv.SmallImageList = img;
lv.Items.Add(itemName, 0);
正在向ListView
添加新的图像列表。每次都传递它的相同ListView
(因此您有效地覆盖了它)。其次,台词:
lv.Items.Add(itemName, 0);
第二个参数是图像列表中的索引(您分配给ListView
)。因此,给它0
会要求ListView
基本上从lv.SmallImageList[0]
(伪代码)中选择图像。
解决方案
为了消除覆盖,我将图像设置逻辑从Populate
中提取出来,并将其放回main方法中。我将只分解设置逻辑:
ImageList img = new ImageList();
img.ImageSize = new Size(30, 30);
var paths = new List<string> { @"C:'pictures'num.png", @"C:'pictures'cat.png" };
paths.ForEach(path => img.Images.Add(MediaTypeNames.Image.FromFile(path)));
lvMoveFrom.SmallImageList = img;
我将所有图像路径放入一个List<string>
中,然后仅使用LINQ
ForEach
遍历每个图像路径,并将其添加到ImageList img
中。与您的原始代码没有区别,除了我将所有图像添加到ListView
并且我只执行一次。
因此,为了使您的代码更容易理解,我做了一些简单的重构。
首先是反转 if 语句:
if (ColNamesForm1[i].ToString().Substring(0, 4).ToUpper() != "COL_" && Regex.IsMatch(ColNamesForm1[i].ToString().Substring(4, 1), @"^'d+$") == false)
自:
if (ColNamesForm1[i].ToString().Substring(0, 4).ToUpper() == "COL_"
|| Regex.IsMatch(ColNamesForm1[i].ToString().Substring(4, 1), @"^'d+$"))
{
continue;
}
这几乎就像一个保护条款,它说如果我们不满足这些最低条件,那么就转到下一项。
然后,我通过减少重复来简化您的方法执行:
if (ColNamesForm1[i].ToString().Substring(ColNamesForm1[i].ToString().Length - 5) == "(num)")
{
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), @"C:'pictures'num.png");
}
else
{
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), @"C:'pictures'cat.png");
}
自:
var image = ColNamesForm1[i].ToString().EndsWith("(num)")
? 0 // corresponds with the position of the image in the ImageList
: 1;
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), image);
最后你会看到我改变了你的Populate
方法。首先,我们用图像预填充您的ListView
,然后使用该三元运算符选择要显示的图像索引。
代码合在一起是:
private void Form1_Load(object sender, EventArgs e)
{
ImageList img = new ImageList();
img.ImageSize = new Size(30, 30);
var paths = new List<string> { @"C:'pictures'num.png", @"C:'pictures'cat.png" };
paths.ForEach(path => img.Images.Add(Image.FromFile(path)));
lvMoveFrom.SmallImageList = img;
for (int i = 0; i < ColNamesForm1.Length; i++)
{
if (ColNamesForm1[i].ToString().Substring(0, 4).ToUpper() == "COL_"
|| Regex.IsMatch(ColNamesForm1[i].ToString().Substring(4, 1), @"^'d+$"))
{
continue;
}
var image = ColNamesForm1[i].ToString().EndsWith("(num)")
? 0 // corresponds with the position of the image in the ImageList
: 1;
OtherFunctions.Populate(lvMoveFrom, ColNamesForm1[i].ToString(), image);
}
}
public static void Populate(ListView lv, string itemName, int imageIndex)
{
lv.Items.Add(itemName, imageIndex);
}
现在,您可以进一步简化:
private void Form1_Load(object sender, EventArgs e)
{
ImageList img = new ImageList();
img.ImageSize = new Size(30, 30);
var paths = new List<string> { @"C:'pictures'num.png", @"C:'pictures'cat.png" };
paths.ForEach(path => img.Images.Add(Image.FromFile(path)));
lvMoveFrom.SmallImageList = img;
for (int i = 0; i < ColNamesForm1.Length; i++)
{
if (ColNamesForm1[i].ToString().Substring(0, 4).ToUpper() == "COL_"
|| Regex.IsMatch(ColNamesForm1[i].ToString().Substring(4, 1), @"^'d+$"))
{
continue;
}
var image = ColNamesForm1[i].ToString().EndsWith("(num)")
? 0 // corresponds with the position of the image in the ImageList
: 1;
lvMoveFrom.Items.Add(ColNamesForm1[i].ToString(), image);
}
}