如何使用Button Winforms c#搜索列表视图的项目

kyxcudwk  于 2022-11-25  发布在  C#
关注(0)|答案(1)|浏览(144)

我正在尝试在我的应用程序中集成一个Visual Studio风格的错误列表。我想知道我如何用按钮过滤我的列表视图,用于我的imageList 1中的每个消息或ID。
感谢您的帮助!

public Form1()
    {
        InitializeComponent();

        InitListView(listView1, imageList1);
    }

    private void InitListView(ListView listView, ImageList imageList)
    {
        listView.SmallImageList = imageList;
    }

    private void Addlog(int imageIndex, string info, string fichier)
    {
        Addlog(listView1, imageList1, imageIndex, info, fichier, 99);
    }

    private void Addlog(ListView listView, ImageList imageList, int imageIndex, string info, string fichier, int maxDisplayItems)
    {
        if (listView.InvokeRequired)
        {
            listView.Invoke(new Action(() =>
            {
                if (listView.Items.Count > maxDisplayItems)
                {
                    listView.Items.RemoveAt(maxDisplayItems);
                }

                ListViewItem lstItem = new ListViewItem(" " + DateTime.Now.ToString("HH:mm:ss", CultureInfo.InvariantCulture), imageIndex);
                lstItem.SubItems.Add(info);
                lstItem.SubItems.Add(fichier);
                listView.Items.Insert(0, lstItem);
            }));
        }
        else
        {
            if (listView.Items.Count > maxDisplayItems)
            {
                listView.Items.RemoveAt(maxDisplayItems);
            }

            ListViewItem lstItem = new ListViewItem(" " + DateTime.Now.ToString("HH:mm:ss", CultureInfo.InvariantCulture), imageIndex);
            lstItem.SubItems.Add(info);
            lstItem.SubItems.Add(fichier);
            listView.Items.Insert(0, lstItem);
        }
    }

    private int count = 0;

    private void btnSendMessage_Click_1(object sender, EventArgs e)
    {
        int index = count % 3;

        switch (index)
        {
            default:
            case 0:
                count_msg = count_msg + 1;
                Addlog(index, "Ceci est un message normal", "C3_BSM_RH");
                Btn_Filtre_Msg.Text = (count_msg) + " Messages";
                break;
            case 1:
                count_avert = count_avert + 1;
                Addlog(index, "Ceci est un message d'avertissement", "C3_BSM_RH");
                Btn_Filtre_Avert.Text = (count_avert) + " Avertissements";
                break;
            case 2:
                count_erreur = count_erreur + 1;
                Addlog(index, "Ceci est un message d'erreur", "C3_BSM_RH");
                Btn_Filtre_Err.Text = (count_erreur) + " Erreurs";
                break;
        }

        count++;

        lbl_Messages.Text = "Messages" + " (" + listView1.Items.Count.ToString() + ")";

    }

    private void Btn_Info_Click(object sender, EventArgs e)
    {
        count_msg = count_msg + 1;

        Addlog(0, "Ceci est un message normal", "C3_BSM_RH");

        lbl_Messages.Text = "Messages" + " (" + listView1.Items.Count.ToString() + ")";

        Btn_Filtre_Msg.Text = (count_msg) + " Messages";

    }

    private void Btn_Avert_Click(object sender, EventArgs e)
    {
        count_avert = count_avert + 1;

        Addlog(1, "Ceci est un message d'avertissement", "C3_BSM_RH");

        lbl_Messages.Text = "Messages" + " (" + listView1.Items.Count.ToString() + ")";

        Btn_Filtre_Avert.Text = (count_avert) + " Avertissements";

    }

    private void Btn_Err_Click(object sender, EventArgs e)
    {
        count_erreur = count_erreur + 1;

        Addlog(2, "Ceci est un message d'erreur", "C3_BSM_RH");

        lbl_Messages.Text = "Messages" + " (" + listView1.Items.Count.ToString() + ")";

        Btn_Filtre_Err.Text = (count_erreur) + " Erreurs";
    }

    private void Btn_Filtre_Err_Click(object sender, EventArgs e)
    {

//如何过滤错误信息?}

private void Btn_Filtre_Avert_Click(object sender, EventArgs e)
    {

//如何过滤警告信息?}

private void Btn_Filtre_Msg_Click(object sender, EventArgs e)
    {

//如何过滤消息?}

}
}

你会发现以上我的代码根据我的需要,但我不能过滤我的视图列表根据按钮。

cfh9epnr

cfh9epnr1#

我会创建虚拟列表并只显示其中一部分。伪代码:

//global list including errors, warnings and messages
List<cMessage> all_messages = new List<cMessage>();

//add some items
all_messages.Add(new cMessage(ERROR, 1));
all_messages.Add(new cMessage(ERROR, 2));
all_messages.Add(new cMessage(ERROR, 3));
all_messages.Add(new cMessage(WARNING, 1));
all_messages.Add(new cMessage(WARNING, 2));
all_messages.Add(new cMessage(MESSAGE, 1));

//display only warnings
gridview.Clear();
foreach(cMessage line in all_messages)
{
    if(line.type is ERROR)
        gridview.display(line);
}

相关问题