积极答复者
listbox更新item的事件

问题
答案
-
确实没有,你可以通过这个方式:
【1、界面】
【2.代码】
namespace CSharp { public partial class Form1 : Form { /// <summary> /// 自定义类 /// </summary> private class MyBindingList<T> : BindingList<T> { /// <summary> /// 自定义删除事件 /// </summary> public event Action<T> AfterRemoving = null; //重写删除事件 protected override void RemoveItem(int index) { //先保留删除的东西 T t = Items[index]; //删除 base.RemoveItem(index); //删除后触发事件 if (AfterRemoving != null) { AfterRemoving(t); } } protected override void ClearItems() { //在清楚前备份全部记录 T[] tArray = new T[Items.Count]; Items.CopyTo(tArray, 0); //删除全部记录 base.ClearItems(); //引发删除事件 foreach (T item in tArray) { AfterRemoving(item); } } } MyBindingList<string> stringList = null; public Form1() { InitializeComponent(); stringList = new MyBindingList<string>(); stringList.Add("a"); stringList.Add("b"); stringList.Add("c"); //绑定添加事件 stringList.ListChanged += new ListChangedEventHandler(stringList_ListChanged); stringList.AfterRemoving += new Action<string>(stringList_AfterRemoving); listBox1.DataSource = stringList; } void stringList_ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemAdded) { MessageBox.Show(stringList[e.NewIndex]); } } void stringList_AfterRemoving(string obj) { MessageBox.Show("删除了:" + obj); } private void button1_Click(object sender, EventArgs e) { //添加记录 stringList.Add("aa"); } private void button2_Click(object sender, EventArgs e) { //清除全部记录 stringList.Clear(); } private void button3_Click(object sender, EventArgs e) { //删除 stringList.Remove("a"); } } }
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16
-
是否可尝试使用DataSet绑定ListBox,借用BindSource的ListChanged事件做点文章,可以达到增加、减少item的效果,而利用DataTable的TableCleared事件能达到clear的效果。粗略的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace listbox { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.dataSet1.Tables[0].TableCleared += new DataTableClearEventHandler(Form1_TableCleared); } private void Form1_TableCleared(object sender, DataTableClearEventArgs e) { MessageBox.Show("CLR"); } #region ADD_BUTTON private void button1_Click(object sender, EventArgs e) { if (this.textBox1.Text.ToString().Trim() == "") return; DataRow dr = this.dataSet1.Tables[0].NewRow(); dr["COL"] = this.textBox1.Text.ToString(); this.dataSet1.Tables[0].Rows.Add(dr); this.dataSet1.Tables[0].AcceptChanges(); } #endregion private void dataTable1BindingSource_ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemAdded) { MessageBox.Show("ADD"); } if (e.ListChangedType == ListChangedType.ItemDeleted) { MessageBox.Show("DEL"); } } #region DEL_BUTTON private void button2_Click(object sender, EventArgs e) { if (this.dataSet1.Tables[0].Rows.Count == 0) return; this.dataSet1.Tables[0].Rows.RemoveAt(this.dataTable1ListBox.SelectedIndex); this.dataSet1.Tables[0].AcceptChanges(); } #endregion #region CLR_BUTTON private void button3_Click(object sender, EventArgs e) { this.dataSet1.Tables[0].Clear(); } #endregion } }
Become a .net Nanja
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16
-
dear
如果是我,我会用bindingSource
private BindingList<string> _data = new BindingList<string>() { "1", "2", "3", "4" }; private BindingSource _source = new BindingSource(); private void Form1_Load(object sender, EventArgs e) { this._source.DataSource = this._data; this.listBox1.DataSource = this._source; this._source.ListChanged += new ListChangedEventHandler(_source_ListChanged); } private void _source_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) { switch (e.ListChangedType) { case ListChangedType.Reset: break; case ListChangedType.ItemAdded: break; case ListChangedType.ItemDeleted: break; case ListChangedType.ItemMoved: break; case ListChangedType.ItemChanged: break; case ListChangedType.PropertyDescriptorAdded: break; case ListChangedType.PropertyDescriptorDeleted: break; case ListChangedType.PropertyDescriptorChanged: break; default: throw new ArgumentOutOfRangeException(); } }
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已编辑 余小章MVP 2012年6月19日 6:12
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16
全部回复
-
确实没有,你可以通过这个方式:
【1、界面】
【2.代码】
namespace CSharp { public partial class Form1 : Form { /// <summary> /// 自定义类 /// </summary> private class MyBindingList<T> : BindingList<T> { /// <summary> /// 自定义删除事件 /// </summary> public event Action<T> AfterRemoving = null; //重写删除事件 protected override void RemoveItem(int index) { //先保留删除的东西 T t = Items[index]; //删除 base.RemoveItem(index); //删除后触发事件 if (AfterRemoving != null) { AfterRemoving(t); } } protected override void ClearItems() { //在清楚前备份全部记录 T[] tArray = new T[Items.Count]; Items.CopyTo(tArray, 0); //删除全部记录 base.ClearItems(); //引发删除事件 foreach (T item in tArray) { AfterRemoving(item); } } } MyBindingList<string> stringList = null; public Form1() { InitializeComponent(); stringList = new MyBindingList<string>(); stringList.Add("a"); stringList.Add("b"); stringList.Add("c"); //绑定添加事件 stringList.ListChanged += new ListChangedEventHandler(stringList_ListChanged); stringList.AfterRemoving += new Action<string>(stringList_AfterRemoving); listBox1.DataSource = stringList; } void stringList_ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemAdded) { MessageBox.Show(stringList[e.NewIndex]); } } void stringList_AfterRemoving(string obj) { MessageBox.Show("删除了:" + obj); } private void button1_Click(object sender, EventArgs e) { //添加记录 stringList.Add("aa"); } private void button2_Click(object sender, EventArgs e) { //清除全部记录 stringList.Clear(); } private void button3_Click(object sender, EventArgs e) { //删除 stringList.Remove("a"); } } }
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16
-
是否可尝试使用DataSet绑定ListBox,借用BindSource的ListChanged事件做点文章,可以达到增加、减少item的效果,而利用DataTable的TableCleared事件能达到clear的效果。粗略的代码如下:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace listbox { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.dataSet1.Tables[0].TableCleared += new DataTableClearEventHandler(Form1_TableCleared); } private void Form1_TableCleared(object sender, DataTableClearEventArgs e) { MessageBox.Show("CLR"); } #region ADD_BUTTON private void button1_Click(object sender, EventArgs e) { if (this.textBox1.Text.ToString().Trim() == "") return; DataRow dr = this.dataSet1.Tables[0].NewRow(); dr["COL"] = this.textBox1.Text.ToString(); this.dataSet1.Tables[0].Rows.Add(dr); this.dataSet1.Tables[0].AcceptChanges(); } #endregion private void dataTable1BindingSource_ListChanged(object sender, ListChangedEventArgs e) { if (e.ListChangedType == ListChangedType.ItemAdded) { MessageBox.Show("ADD"); } if (e.ListChangedType == ListChangedType.ItemDeleted) { MessageBox.Show("DEL"); } } #region DEL_BUTTON private void button2_Click(object sender, EventArgs e) { if (this.dataSet1.Tables[0].Rows.Count == 0) return; this.dataSet1.Tables[0].Rows.RemoveAt(this.dataTable1ListBox.SelectedIndex); this.dataSet1.Tables[0].AcceptChanges(); } #endregion #region CLR_BUTTON private void button3_Click(object sender, EventArgs e) { this.dataSet1.Tables[0].Clear(); } #endregion } }
Become a .net Nanja
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16
-
dear
如果是我,我会用bindingSource
private BindingList<string> _data = new BindingList<string>() { "1", "2", "3", "4" }; private BindingSource _source = new BindingSource(); private void Form1_Load(object sender, EventArgs e) { this._source.DataSource = this._data; this.listBox1.DataSource = this._source; this._source.ListChanged += new ListChangedEventHandler(_source_ListChanged); } private void _source_ListChanged(object sender, System.ComponentModel.ListChangedEventArgs e) { switch (e.ListChangedType) { case ListChangedType.Reset: break; case ListChangedType.ItemAdded: break; case ListChangedType.ItemDeleted: break; case ListChangedType.ItemMoved: break; case ListChangedType.ItemChanged: break; case ListChangedType.PropertyDescriptorAdded: break; case ListChangedType.PropertyDescriptorDeleted: break; case ListChangedType.PropertyDescriptorChanged: break; default: throw new ArgumentOutOfRangeException(); } }
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已编辑 余小章MVP 2012年6月19日 6:12
- 已标记为答案 Bob ShenModerator 2012年6月26日 9:16