.net写的程序,遇到点问题
A页面中相关代码
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.chId = new System.Windows.Forms.ColumnHeader();
this.chType = new System.Windows.Forms.ColumnHeader();
this.chEquName = new System.Windows.Forms.ColumnHeader();
this.chEquKMLabel = new System.Windows.Forms.ColumnHeader();
this.chDetail = new System.Windows.Forms.ColumnHeader();
this.chTime = new System.Windows.Forms.ColumnHeader();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.tsmClearAll = new System.Windows.Forms.ToolStripMenuItem();
//this.tsmClear = new System.Windows.Forms.ToolStripMenuItem();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// chId
//
this.chId.Text = "";
this.chId.Width = 20;
//
// chType
//
this.chType.Text = "类型";
this.chType.Width = 40;
//
// chEquName
//
this.chEquName.Text = "设备名称";
this.chEquName.Width = 120;
//
// chEquKMLabel
//
this.chEquKMLabel.Text = "公里标";
this.chEquKMLabel.Width = 120;
//
// chDetail
//
this.chDetail.Text = "详细内容";
//
// chTime
//
this.chTime.Text = "时间";
this.chTime.Width = 120;
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {this.tsmClearAll});
// this.tsmClearAll});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(149, 26);
this.contextMenuStrip1.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.ContextMenuStrip_ItemClicked);
//
// tsmClearAll
//
this.tsmClearAll.Image = global::StationMachinApp.Properties.Resources.DeleteHS;
this.tsmClearAll.Name = "tsmClearAll";
this.tsmClearAll.Size = new System.Drawing.Size(148, 22);
this.tsmClearAll.Text = "清除数据";
//
// SubListView
//
this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.chId,
this.chType,
this.chEquName,
this.chEquKMLabel,
this.chTime,
this.chDetail});
this.ContextMenuStrip = this.contextMenuStrip1;
this.Dock = System.Windows.Forms.DockStyle.Fill;
this.FullRowSelect = true;
this.GridLines = true;
this.View = System.Windows.Forms.View.Details;
this.SizeChanged += new System.EventHandler(this.SubListView_SizeChanged);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ColumnHeader chId;
private System.Windows.Forms.ColumnHeader chType;
private System.Windows.Forms.ColumnHeader chEquName;
private System.Windows.Forms.ColumnHeader chEquKMLabel;
private System.Windows.Forms.ColumnHeader chDetail;
private System.Windows.Forms.ColumnHeader chTime;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem tsmClearAll;
B页面中的清除方法
private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Name.Equals("tsmClearAll"))
{
this.Items.Clear();
}
}
这样的结果是选中的是选中当前行,点击“清除数据”时,是将整个集合清空了,而非是将当前选中的项清理掉,请大神们帮忙看看,怎样改才能清除当前选中的项。
在for循环中操作list 并移除list中的项 如果for循环正序 当删除的时候 list的坐标会产生变化 会导致越界的问题
我们可以倒叙删除
for (int i = count; i<=0; i--)
{
list.delete(i);
}
this.Items.Clear();这行代码就是清空整个集合
//要想清除当前项,可以试一下这样做
private void ContextMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Name.Equals("tsmClearAll"))
{
var currentItem= sender as 类型;
//想法找到currentItem在集合中的索引,没有的话,currentItem有很多属性吧,在属性里设置一个唯一的标识
//从集合中根据标识删除var currentItem
}
}
最后一行代码更改 this.Items.Remove(this.selecteditem)