/// <summary> /// Remove a control from the ListView /// </summary> /// <param name="c">Control to be removed</param> public void RemoveEmbeddedControl(Control c) { if (c == null) { throw new ArgumentNullException(); } for (int i = 0; i < _embeddedControls.Count; i++) { EmbeddedControl ec = (EmbeddedControl)_embeddedControls[i]; if (ec.Control == c) { c.Click -= new EventHandler(_embeddedControl_Click); this.Controls.Remove(c); _embeddedControls.RemoveAt(i); return; } } throw new Exception("Control not found!"); }
/// <summary> /// Add a control to the ListView /// </summary> /// <param name="c">Control to be added</param> /// <param name="col">Index of column</param> /// <param name="row">Index of row</param> /// <param name="dock">Location and resize behavior of embedded control</param> public void AddEmbeddedControl(Control c, int col, int row, DockStyle dock) { if (c == null) throw new ArgumentNullException(); if (col >= Columns.Count || row >= Items.Count) throw new ArgumentOutOfRangeException(); EmbeddedControl ec = new EmbeddedControl(); ec.Control = c; ec.Column = col; ec.Row = row; ec.Dock = dock; ec.Item = Items[row]; _embeddedControls.Add(ec); // Add a Click event handler to select the ListView row when an embedded control is clicked c.Click += _embeddedControl_Click; Controls.Add(c); }
/// <summary>Adds a control to a given sub-item.</summary> /// <param name="control">The control to add.</param> /// <param name="item">The item parent of the sub-item we want the control be added.</param> /// <param name="index">The index of the sub-item inside the item.</param> public void AddControlToSubItem(Control control, ListViewItem item, int index) { var ec = new EmbeddedControl() { Control = control, Item = item, SubItemIndex = index }; var subItem = ec.SubItem; if (subItem != null) { // It should follow the subItem size, and not be docked to // the listview itself. But the subItem is not a control! // TODO: use the specified dock & anchor properties do determine // the way the control should be layed out into the sub item. if (control.Dock == DockStyle.Fill) control.Dock = DockStyle.None; base.Controls.Add(control); //subItem.Control = control; embeddedControls.Add(ec); control.Click += new EventHandler(OnEmbeddedControlClick); } else throw new ArgumentException(string.Format("No ListViewExControlSubitem at index {0}", index), "index"); }