FlowLayoutPanel 中的搜索控件 [英] Search controls in a FlowLayoutPanel

查看:57
本文介绍了FlowLayoutPanel 中的搜索控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Windows 窗体应用程序中有一个 FlowLayoutPanel,用于存储电影粉丝和标题,现在我正在寻找一种在 FlowLayoutPanel 中搜索现有项目的方法.这是我的图形用户界面:

I have a FlowLayoutPanel in my Windows Forms Application, which I use for storing movie fanarts and titles, now what I am looking for is a method to search for existing items in the FlowLayoutPanel. This is my GUI:

Search movies... 框中,我希望 TextChanged 事件仅显示带有与搜索相关的电影标签的电影输入.

In the Search movies... box, I want the TextChanged event to show only movies with the movie labels related to the search input.

推荐答案

foreach (Control c in myFlowLayoutPanel.Controls)
{
    if (!c.Property.Text.ToLower().Contains(searchBox.Text.ToLower()))
    {
        myFlowLayoutPanel.Controls.Remove(c);
    }
}

这将遍历面板的子项 - 您可以根据搜索词检查某些属性,如果它不包含搜索词,则删除子项.

This will loop through the children of the panel - you can check some property against the search term and remove the child if it doesn't contain the search term.

根据您的评论,您似乎只在标签上获得匹配项,因此您的图片将消失.我将采用的方法是创建一个包含图像和标签的 UserControl(右键单击您的项目 - 添加 - UserControl - 为其命名).使用设计器添加图片框和标签(或您已经使用的任何控件).您后面的 UserControl 代码如下所示:

Based on your comment, it looks like you are getting matches only on your labels, so your pictures are going away. The way that I would approach that is to create a UserControl that holds both the image and a label (Right-click your project - Add - UserControl - give it a name). Use the designer to add a PictureBox and a Label (or whatever controls you are already using). Your code behind for the UserControl would look something like this:

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.现在,您可以根据 Title 属性是否匹配来保留或删除整个 UserControl.您的 foreach 循环更改为:

For each movie, you can create an instance of your new custom UserControl by passing in the thumbnail image and the title and then add the entire UserControl to your FlowLayoutPanel. Now you can keep or remove the entire UserControl depending on whether the Title property matches. Your foreach loop changes to this:

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
        }
    }
}

这篇关于FlowLayoutPanel 中的搜索控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆