Csharp/C Sharp/GUI Windows Form/CheckBox List — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
м (1 версия)
 
(нет различий)

Текущая версия на 11:33, 26 мая 2010

CheckedListBox Item Check event

using System;
using System.Collections.Generic;
using System.ruponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
public class Form1 : Form
{
      private System.Windows.Forms.CheckedListBox inputCheckedListBox;
      private System.Windows.Forms.ListBox displayListBox;
    
    public Form1() {
         InitializeComponent();
    }
      private void inputCheckedListBox_ItemCheck(object sender, ItemCheckEventArgs e )
      {
         string item = inputCheckedListBox.SelectedItem.ToString();
         if ( e.NewValue == CheckState.Checked )
            displayListBox.Items.Add( item );
         else
            displayListBox.Items.Remove( item );
      }
      private void InitializeComponent()
      {
         this.inputCheckedListBox = new System.Windows.Forms.CheckedListBox();
         this.displayListBox = new System.Windows.Forms.ListBox();
         this.SuspendLayout();
         // 
         // inputCheckedListBox
         // 
         this.inputCheckedListBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.inputCheckedListBox.FormattingEnabled = true;
         this.inputCheckedListBox.Items.AddRange(new object[] {
            "A",
            "B",
            "C",
            "D",
            "E",
            "F",
            "G",
            "H"});
         this.inputCheckedListBox.Location = new System.Drawing.Point(17, 12);
         this.inputCheckedListBox.Name = "inputCheckedListBox";
         this.inputCheckedListBox.Size = new System.Drawing.Size(202, 188);
         this.inputCheckedListBox.TabIndex = 0;
         this.inputCheckedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.inputCheckedListBox_ItemCheck);
         // 
         // displayListBox
         // 
         this.displayListBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
         this.displayListBox.FormattingEnabled = true;
         this.displayListBox.ItemHeight = 20;
         this.displayListBox.Location = new System.Drawing.Point(236, 12);
         this.displayListBox.Name = "displayListBox";
         this.displayListBox.Size = new System.Drawing.Size(190, 184);
         this.displayListBox.TabIndex = 1;
         // 
         // CheckedListBoxTestForm
         // 
         this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
         this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
         this.ClientSize = new System.Drawing.Size(438, 211);
         this.Controls.Add(this.displayListBox);
         this.Controls.Add(this.inputCheckedListBox);
         this.Name = "CheckedListBoxTestForm";
         this.Text = "CheckedListBoxTest";
         this.ResumeLayout(false);
      }
  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }
}


Get selected checkbox list items

using System;
using System.Drawing;
using System.Collections;
using System.ruponentModel;
using System.Windows.Forms;
using System.Data;
public class Form1 : System.Windows.Forms.Form {
   private System.Windows.Forms.CheckedListBox chkListPossibleValues;
   private System.Windows.Forms.ListBox lstSelected;
   private System.Windows.Forms.Button btnMove;
   private System.ruponentModel.Container components=null;
   public Form1() {
      InitializeComponent();
      this.chkListPossibleValues.Items.Add("Ten");
   }
   private void InitializeComponent() {
      this.lstSelected = new System.Windows.Forms.ListBox();
      this.btnMove = new System.Windows.Forms.Button();
      this.chkListPossibleValues = new System.Windows.Forms.CheckedListBox();
      this.SuspendLayout();
  
      this.lstSelected.Location = new System.Drawing.Point(232, 8);
      this.lstSelected.Name = "lstSelected";
      this.lstSelected.Size = new System.Drawing.Size(136, 186);
      this.lstSelected.TabIndex = 1;
      this.btnMove.Location = new System.Drawing.Point(152, 80);
      this.btnMove.Name = "btnMove";
      this.btnMove.TabIndex = 3;
      this.btnMove.Text = "Move";
      this.btnMove.Click += new System.EventHandler(this.btnMove_Click);
      this.chkListPossibleValues.CheckOnClick = true;
      this.chkListPossibleValues.Items.AddRange(new object[] {"One", "Two", "Three",
                  "Four", "Five","Six","Seven", "Eight", "Nine"});
      this.chkListPossibleValues.Location = new System.Drawing.Point(8, 8);
      this.chkListPossibleValues.Name = "chkListPossibleValues";
      this.chkListPossibleValues.Size = new System.Drawing.Size(136, 184);
      this.chkListPossibleValues.TabIndex = 0;
      this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
      this.ClientSize = new System.Drawing.Size(376, 205);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {this.btnMove,
                                  this.lstSelected,  this.chkListPossibleValues});
      this.Name = "Form1";
      this.Text = "List Boxes";
      this.ResumeLayout(false);
      }
      static void Main() {
         Application.Run(new Form1());
      }
      private void btnMove_Click(object sender, System.EventArgs e) {
         if (this.chkListPossibleValues.CheckedItems.Count > 0) {
            this.lstSelected.Items.Clear();
            foreach (string item in this.chkListPossibleValues.CheckedItems) {
                this.lstSelected.Items.Add(item.ToString());
            }
            for (int i = 0; i < this.chkListPossibleValues.Items.Count; i++){
                 this.chkListPossibleValues.SetItemChecked(i, false);
            }     
         }
      }
}