Copy selected CheckedItems to another CheckedListBox : CheckedListBox « GUI Windows Forms « C# / CSharp Tutorial






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;


class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.checkedListBoxPossibleValue.Items.Add("Ten");
    }

    private void buttonMove_Click(object sender, EventArgs e) {
        if (this.checkedListBoxPossibleValue.CheckedItems.Count > 0) {
            this.listBoxSelected.Items.Clear();
            foreach (string item in this.checkedListBoxPossibleValue.CheckedItems) {
                this.listBoxSelected.Items.Add(item.ToString());
            }
            for (int i = 0; i < this.checkedListBoxPossibleValue.Items.Count; i++)
                this.checkedListBoxPossibleValue.SetItemChecked(i, false);
        }
    }
    private void InitializeComponent() {
        this.checkedListBoxPossibleValue = new System.Windows.Forms.CheckedListBox();
        this.buttonMove = new System.Windows.Forms.Button();
        this.listBoxSelected = new System.Windows.Forms.ListBox();
        this.SuspendLayout();

        this.checkedListBoxPossibleValue.CheckOnClick = true;
        this.checkedListBoxPossibleValue.FormattingEnabled = true;
        this.checkedListBoxPossibleValue.Items.AddRange(new object[] {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine"});
        this.checkedListBoxPossibleValue.Location = new System.Drawing.Point(13, 13);
        this.checkedListBoxPossibleValue.Name = "checkedListBoxPossibleValue";
        this.checkedListBoxPossibleValue.Size = new System.Drawing.Size(187, 242);

        this.buttonMove.Location = new System.Drawing.Point(207, 129);
        this.buttonMove.Text = "Move";
        this.buttonMove.Click += new System.EventHandler(this.buttonMove_Click);

        this.listBoxSelected.FormattingEnabled = true;
        this.listBoxSelected.Location = new System.Drawing.Point(288, 13);
        this.listBoxSelected.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
        this.listBoxSelected.Size = new System.Drawing.Size(187, 238);

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(492, 273);
        this.Controls.Add(this.listBoxSelected);
        this.Controls.Add(this.buttonMove);
        this.Controls.Add(this.checkedListBoxPossibleValue);
        this.Text = "List Boxes";
        this.ResumeLayout(false);

    }
    private System.Windows.Forms.CheckedListBox checkedListBoxPossibleValue;
    private System.Windows.Forms.Button buttonMove;
    private System.Windows.Forms.ListBox listBoxSelected;

    [STAThread]
    static void Main() {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }
}








23.28.CheckedListBox
23.28.1.CheckedListBox: get selection stateCheckedListBox: get selection state
23.28.2.CheckedListBox: set item selectedCheckedListBox: set item selected
23.28.3.CheckedListBox: checked and selected item countCheckedListBox: checked and selected item count
23.28.4.CheckedListBox: selected change eventCheckedListBox: selected change event
23.28.5.Copy selected CheckedItems to another CheckedListBox