public int GetRequiredWidth(Graphics graphics) { int width = 0; for (int i = 0; i < columns.Count; i++) { if (columns[i].AutoSize) { int minimumWidth = DynamicListColumn.DefaultWidth; foreach (DynamicListRow row in Rows) { DynamicListItem item = row[i]; item.MeasureMinimumWidth(graphics, ref minimumWidth); } width += minimumWidth; } else { width += columns[i].Width; } width += 1; } return(width); }
protected override void OnPaint(PaintEventArgs e) { //Debug.WriteLine("OnPaint"); Graphics g = e.Graphics; allowedControls.Clear(); int columnIndex = -1; foreach (DynamicListColumn col in Columns) { columnIndex += 1; if (!col.AutoSize) { continue; } int minimumWidth = DynamicListColumn.DefaultWidth; foreach (DynamicListRow row in Rows) { DynamicListItem item = row[columnIndex]; item.MeasureMinimumWidth(e.Graphics, ref minimumWidth); } col.MinimumWidth = minimumWidth; } int controlIndex = 0; int xPos; int yPos = -scrollOffset; Size clientSize = ClientSize; foreach (DynamicListRow row in Rows) { if (yPos + row.Height > 0 && yPos < clientSize.Height) { xPos = 0; for (columnIndex = 0; columnIndex < columns.Count; columnIndex++) { DynamicListColumn col = columns[columnIndex]; Rectangle rect = new Rectangle(xPos, yPos, col.Width, row.Height); if (columnIndex == columns.Count - 1) { rect.Width = clientSize.Width - 1 - rect.Left; } DynamicListItem item = row[columnIndex]; Control ctl = item.Control; if (ctl != null) { allowedControls.Add(ctl); if (rect != ctl.Bounds) { ctl.Bounds = rect; } if (!this.Controls.Contains(ctl)) { this.Controls.Add(ctl); this.Controls.SetChildIndex(ctl, controlIndex); } controlIndex += 1; } else { item.PaintTo(e.Graphics, rect, this, col, item == itemAtMousePosition); } xPos += col.Width + 1; } } yPos += row.Height + lineMarginY; } xPos = 0; Form containerForm = FindForm(); bool isFocused; if (containerForm is IActivatable) { isFocused = (containerForm as IActivatable).IsActivated; } else { isFocused = this.Focused; } for (columnIndex = 0; columnIndex < columns.Count - 1; columnIndex++) { DynamicListColumn col = columns[columnIndex]; xPos += col.Width + 1; Color separatorColor; if (isFocused) { separatorColor = col.ColumnSeperatorColor; if (separatorColor.IsEmpty) { separatorColor = col.ColumnSeperatorColorInactive; } } else { separatorColor = col.ColumnSeperatorColorInactive; if (separatorColor.IsEmpty) { separatorColor = col.ColumnSeperatorColor; } } if (separatorColor.IsEmpty) { separatorColor = BackColor; } using (Pen separatorPen = new Pen(separatorColor)) { e.Graphics.DrawLine(separatorPen, xPos - 1, 1, xPos - 1, Math.Min(clientSize.Height, yPos) - 2); } } removedControls.Clear(); foreach (Control ctl in Controls) { if (!allowedControls.Contains(ctl)) { removedControls.Add(ctl); } } foreach (Control ctl in removedControls) { Debug.WriteLine("Removing control"); Controls.Remove(ctl); Debug.WriteLine("Control removed"); } allowedControls.Clear(); removedControls.Clear(); base.OnPaint(e); }