Example #1
0
        public static CheckedListBox ShowCheckBoxes(string[] options, string caption, int w, int h, int wc = 110, int hc = 300)
        {
            Form prompt = new Form()
            {
                AutoSize        = true,
                Width           = w,
                Height          = h,
                FormBorderStyle = FormBorderStyle.FixedDialog,
                Text            = caption,
                StartPosition   = FormStartPosition.CenterParent,
                ControlBox      = false,
                AutoScaleMode   = AutoScaleMode.Font,
                AutoSizeMode    = AutoSizeMode.GrowOnly
            };
            FlowLayoutPanel pnl = new FlowLayoutPanel
            {
                Dock          = DockStyle.Fill,
                Visible       = true,
                Name          = caption,
                TabIndex      = 0,
                AutoSize      = true,
                AutoSizeMode  = AutoSizeMode.GrowOnly,
                FlowDirection = FlowDirection.TopDown
            };
            CheckedListBox cb = new CheckedListBox();

            cb.ItemCheck   += Cb_ItemCheck;
            cb.CheckOnClick = true;
            cb.Items.AddRange(options);
            cb.IntegralHeight = true;
            cb.SetItemCheckState(0, CheckState.Checked);
            for (int i = 1; i < cb.Items.Count; i++)
            {
                cb.SetItemChecked(i, false);
                cb.GetItemRectangle(i); //This is required for GetPreferredSize to function.  Very confusing.
            }


            Button toggleAllSelection = new Button()
            {
                Anchor = AnchorStyles.Right, Dock = DockStyle.Top, Text = "Select/Deselect All", Left = 350, Width = 100, Top = 70, AutoSize = true
            };

            toggleAllSelection.Click += (sender, e) =>
            {
                bool isChecked = false;
                for (int i = 1; i < cb.Items.Count && isChecked == false; i++)
                {
                    isChecked = cb.GetItemChecked(i);
                }

                for (int i = 1; i < cb.Items.Count; i++)
                {
                    cb.SetItemChecked(i, !isChecked);
                }
            };
            pnl.Controls.Add(toggleAllSelection);

            cb.ClientSize = cb.GetPreferredSize(new System.Drawing.Size());
            pnl.Controls.Add(cb);

            Button confirmation = new Button()
            {
                Anchor = AnchorStyles.Right, Dock = DockStyle.Bottom, Text = "OK", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK, AutoSize = true
            };

            confirmation.Click += (sender, e) => { prompt.Close(); };

            pnl.Controls.Add(confirmation);

            prompt.AcceptButton = confirmation;
            prompt.Controls.Add(pnl);
            prompt.ShowDialog();

            return(cb);
        }