c# - 如何在流布局面板中搜索控件?
问题描述
在我的流程布局面板中,它在用户控件中加载图片和名称。
我试试这个,效果很好
foreach (DataRow row in dt.Rows)
{
byte[] data = (byte[])row["Image"];
pic = new PictureBox();
pic.Width = 150;
pic.Height = 150;
pic.BackgroundImageLayout = ImageLayout.Stretch;
pic.BorderStyle = BorderStyle.FixedSingle;
string type = row.Table.Columns.Contains("liquidPriceId") ? "liquidPrice" : "itemMaster";
string tag = row.Table.Columns.Contains("liquidPriceId") ? row["liquidPriceId"].ToString() : row["itemMasterId"].ToString();
MemoryStream ms = new MemoryStream(data);
pic.BackgroundImage = new Bitmap(ms);
Label name = new Label();
name.Text = row["Name"].ToString();
name.BackColor = Color.FromArgb(45, 66, 91);
pic.Controls.Add(name);
flp.Controls.Add(pic);
}
然后在我的搜索文本更改中我尝试这个,我的问题是我不知道如何获取过滤名称
foreach (Control c in flowLayoutPanel3.Controls)
如何进入 我的图片和名称值的c ?
private void txtSearchBox_TextChanged(object sender, EventArgs e)
{
string searchValue = txtSearchBox.Text;
try
{
if (txtSearchBox.Text.Length > 0)
{
string compareTo = String.Concat("*", txtSearchBox.Text.ToLower(), "*");
foreach (Control c in flowLayoutPanel3.Controls)
{
c.Visible =(c.Name.ToLower() == compareTo); // c.Name is empty how can i get name ?
}
}
else
{
foreach (Control c in flowLayoutPanel3.Controls)
{
c.Visible = true;
}
}
}
}
解决方案
从提供的代码和文本中不清楚,但似乎您可能需要设置您在第一个代码片段中创建的控件的 Name 属性。像这样的东西:
//...
pic = new PictureBox();
pic.Name = "My Picture";
// ...
我也会改变这个比较:
c.Visible =(c.Name.ToLower() == compareTo);
对此:
c.Visible = c.Name.StartsWith(compareTo, StringComparison.CurrentCultureIgnoreCase);
推荐阅读
- python - 如何从 Flask 的下拉列表中获取日期并将其作为参数传递给函数?
- google-sheets - 谷歌表格 - 日期到期的条件格式
- authentication - 如何使用 Nessus 执行基于 JSON 的身份验证?
- reactjs - React 测试库:为什么测试的顺序很重要?
- python - python3 正则表达式中的正斜杠似乎否定非贪婪运算符
- java - 使用 exoplayer 播放 m3u8 文件
- hex - webm 和 mp4 十六进制文件头持续时间
- flutter - 如何以当地时间显示UTC时间
- rabbitmq - Rabbitmq 和 UI 队列选项称为移动消息
- java - Java 泛型 - 关于大于对象的方法的快速问题