internal MsgBox_CheckBoxes(string message, string caption, List <string> DataSource, string buttonText, CBDisplayType type = CBDisplayType.GRID) { InitializeComponent(); currentType = type; label1.Text = message; if (currentType == CBDisplayType.GRID) { for (int i = 0; i < DataSource.Count; i++) { CheckBox checkBox = new CheckBox(); checkBox.Name = "CheckBox_" + i; checkBox.Text = DataSource[i]; checkBoxes.Add(checkBox); } flowLayoutPanel1.Controls.AddRange(checkBoxes.ToArray()); this.Size = new Size(this.Size.Width, 150 + 35 * (int)Math.Ceiling((double)(checkBoxes.Count / 3))); } else { clb = new CheckedListBox(); CheckedListBox.ObjectCollection oc = clb.Items; for (int i = 0; i < DataSource.Count; i++) { oc.Add(DataSource[i]); } clb.Dock = DockStyle.Fill; clb.CheckOnClick = true; tableLayoutPanel1.Controls.RemoveByKey("flowLayoutPanel1"); tableLayoutPanel1.Controls.Add(clb, 1, 1); this.Size = new Size(this.Size.Width, 100 + clb.Size.Height); } button1.Text = buttonText; }
/// <summary> /// Opens a Message Box form with multiple checkboxes (length of the data source). Returns a list of strings representing the text beside each checked check box. /// </summary> /// <param name="message">The message displayed directly above the list of checkboxes.</param> /// <param name="DataSource">The data source representing the list of strings to display checkboxes for.</param> /// <param name="type">The way to display the checkboxes in the window.</param> /// <param name="caption">The title of the Message Box form.</param> /// <param name="buttonText">The text displayed over the return button.</param> /// <returns></returns> public static List <string> _Checkboxes(string message, List <string> DataSource, CBDisplayType type = CBDisplayType.GRID, string caption = "Message Box", string buttonText = "OK") { MsgBox_CheckBoxes cb = new MsgBox_CheckBoxes(message, caption, DataSource, buttonText, type); DialogResult dr = cb.ShowDialog(); if (dr == DialogResult.OK) { return(cb.returnList); } return(null); }