我的Windows应用程序中有一个FlowLayoutPanel,用于存储电影的扇子和标题,现在我要寻找的是在FlowLayoutPanel中搜索现有项目的方法。这是我的GUI:

在Search movies...框中,我希望TextChanged事件只显示带有的电影(与搜索输入相关的电影标签)。
发布于 2016-10-14 20:52:11
foreach (Control c in myFlowLayoutPanel.Controls)
{
if (!c.Property.Text.ToLower().Contains(searchBox.Text.ToLower()))
{
myFlowLayoutPanel.Controls.Remove(c);
}
}这将遍历面板的子部分--您可以对照搜索项检查一些属性,如果不包含搜索项,则删除子属性。
编辑:根据你的评论,看起来你只在标签上得到匹配,所以你的照片就会消失。我所采用的方法是创建一个同时保存图像和标签的UserControl (右键单击您的项目---给它命名)。使用设计器可以添加PictureBox和标签(或任何您已经使用的控件)。UserControl后面的代码如下所示:
public partial class Movie : UserControl
{
public string Title { get; set; } // for easy matching
public Movie()
{
InitializeComponent();
}
public Movie(Image thumbnail, string title) // use this constructor to make your movie tiles
{
InitializeComponent();
pictureBox1.Image = thumbnail;
label1.Text = title;
Title = title;
}
}对于每部电影,您可以通过传入缩略图和标题来创建新的自定义UserControl的实例,然后将整个UserControl添加到FlowLayoutPanel中。现在,您可以保留或删除整个UserControl,这取决于标题属性是否匹配。您的foreach循环更改为:
foreach (Control c in flp.Controls)
{
// Check if the control is one of your custom UserControls:
if (c.GetType() == typeof(Movie))
{
// since it is a "Movie", it is safe to cast it:
Movie movie = (Movie)c;
if (!movie.Title.ToLower().Contains(searchBox.Text.ToLower()))
{
flp.Controls.Remove(c); // remove or keep both the image and text
}
}
}发布于 2021-02-06 11:04:24
你好;
foreach (Control item in flowLayoutPanel1.Controls)
{
if (item is Button)
{
item.Enabled = false;
}
}
MessageBox.Show("You lost loser!");它是working.Check It out!;#按钮#流布局#启用
https://stackoverflow.com/questions/40050302
复制相似问题