【发布时间】:2020-12-23 07:57:41
【问题描述】:
我正在处理 winforms,我有 2 个代码,用于同一件事,它们只是查询差异相同,但一个工作完全正常,另一个有问题,因为它放错了图像。 问题是每次我运行代码时,图像都在错误的位置。 正确的功能代码:
int c2 = -1;
List<string> searchpath =new List<string>();
List<string> searchtitle = new List<string>();
listView2.Clear();
homerecipe.Clear();
searchtitle.Clear();
searchpath.Clear();
imageList3.Images.Clear();
var text = textBox1.Text;
char[] separator = { ' ' };
string[] words = null;
words = text.Split(separator);
foreach (string word in words)
{
try
{
cmd = new SqlCommand($"select Title, Thumbnail,RecipeName from RecipeInfo where RecipeName like '%{word}%'", con);
con.Open();
SqlDataReader read = cmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
// if (homerecipe.Any(item => item == read[2].ToString())) continue;
searchtitle.Add(read[0].ToString());
searchpath.Add($@"{read[1].ToString()}");
homerecipe.Add(read[2].ToString());
}
read.Close();
//con.Close();
foreach (string ipath in searchpath)
{
ListViewItem img = listView2.FindItemWithText(ipath);
if (img == null)
{
imageList3.Images.Add(Image.FromFile(ipath));
}
}
listView2.LargeImageList = imageList3;
foreach (string hometitle in searchtitle)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
c2++;
listView2.Items.Add(hometitle, c2);
}
}
}
con.Close();
}
catch (SqlException)
{
MessageBox.Show("masla");
con.Close();
//continue;
}
有问题的代码:
int ccc = -1;
hometitles.Clear();
homepaths.Clear();
homerecipe.Clear();
imageList2.Images.Clear();
try
{
cmd = new SqlCommand("select Title, Thumbnail,RecipeName from RecipeInfo order by newid()", con); //generating random from sql
con.Open();
SqlDataReader reader1 = cmd.ExecuteReader();
if (reader1.HasRows)
{
while (reader1.Read())
{
hometitles.Add(reader1[0].ToString());
homepaths.Add($@"{reader1[1].ToString()}");
homerecipe.Add(reader1[2].ToString());
}
}
reader1.Close();
//con.Close();
foreach (string imagepath in homepaths)
{
ListViewItem img = listView2.FindItemWithText(imagepath);
if (img == null)
{
imageList2.Images.Add(Image.FromFile(imagepath));
}
}
listView2.LargeImageList = imageList2;
foreach (string hometitle in hometitles)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
ccc++;
listView2.Items.Add(hometitle,ccc);
}
}
con.Close();
}
catch(SqlException)
{
MessageBox.Show("error");
}
我尝试使用 homerecipe 元素作为图像键,因为它们是主键,但我不知道如何在 foreach 中给出一个条件,即如果输入了一个名称,那么相同的名称不会出现两次。 为此,我正在尝试这个
foreach (string imagepath in homepaths)
{
foreach(string name in homerecipe) //name a primary key
{
ListViewItem img = listView2.FindItemWithText(imagepath);
if (img == null)
{
MessageBox.Show(name);
imageList2.Images.Add(name,Image.FromFile(imagepath));
}
}
}
listView2.LargeImageList = imageList2;
foreach (string hometitle in hometitles)
{
foreach (string name in homerecipe)
{
ListViewItem list = listView2.FindItemWithText(hometitle);
if (list == null)
{
ccc++;
listView2.Items.Add(hometitle, name);
}
}
}
我最近 3 天都在尝试这个,请帮我纠正它。请我在我的项目的最后期限内,但这个问题没有解决。我是编程新手,请解决这个问题。
有时,仅图像随机播放,有时文本和图像同时播放,但位置不正确
【问题讨论】:
-
有人请帮我解决这个问题,如果这个问题仍然存在,我的整个项目将是空的
标签: c# sql-server winforms listview