C#的WinForms ListView的项目计数更改事件 [英] C# WinForms ListView Item Count Change Event

查看:175
本文介绍了C#的WinForms ListView的项目计数更改事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在Win形式的事件可以触发的ListView变化的项目当计数?我试着大小和文本 - 奇怪的是他们八九不离十的工作,但并不总是...

Is there an event in Win Forms that can fire when the count of items in ListView change? I tried Size and Text - oddly enough they "sorta" worked but not always...

我试着去触发标签与列表视图的项目数更新,因为它没有改变人工手动操作,在一百年的方法。

Im trying to trigger a label to update with the count of the listview items as it changes without manually doing that in a hundred methods.

推荐答案

如果你不使用绑定的数据源,你可以围绕ListView控件的包装,并添加一个方法和事件触发了一个事件上增加一个项目您的ListView收藏。

If you are not using a bound datasource you can create a wrapper around the ListView Control and add a Method and an Event to fire off an event on adding an item to your ListView Collection.

自定义的ListView

public class customListView : ListView
{
    public event EventHandler<CustomEventArgs> UpdateListViewCounts;
    public void UpdateList(string data)
    {
        // You may have to modify this depending on the
        // Complexity of your Items
        this.Items.Add(new ListViewItem(data));
        CustomEventArgs e = new CustomEventArgs(Items.Count);
        UpdateListViewCounts(this, e);
    }
}
public class CustomEventArgs : EventArgs
{
    private int _count;
    public CustomEventArgs(int count)
    {
        _count = count;
    }
    public int Count
    {
        get { return _count; }
    }
}

示例Usuage

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();

        customListView1.UpdateListViewCounts+=customListView1_UpdateListViewCounts;
    }

    private void customListView1_UpdateListViewCounts(object sender, CustomEventArgs e)
    {
        //You can check for the originating Listview if 
        //you have multiple ones and want to implement
        //Multiple Labels
        label1.Text = e.Count.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        customListView1.UpdateList("Hello");
    }


}

这篇关于C#的WinForms ListView的项目计数更改事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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