Csharp/CSharp Tutorial/GUI Windows Forms/CheckedListBox — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 12:15, 26 мая 2010
Содержание
CheckedListBox: checked and selected item count
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class CheckedListBoxes : Form
{
CheckedListBox clb;
Button btnToggle;
Button btnClear;
String str;
public CheckedListBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
clb = new CheckedListBox();
clb.Parent = this;
clb.Location = new Point(10,10);
clb.Size = new Size(ClientSize.Width - 20, Height - 240);
clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
clb.BorderStyle = BorderStyle.Fixed3D;
clb.MultiColumn = true;
clb.ScrollAlwaysVisible = true;
clb.ThreeDCheckBoxes = true;
clb.CheckOnClick = true;
clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);
// Toggle Indeterminate Button
btnToggle = new Button();
btnToggle.Parent = this;
btnToggle.Text = "Toggle Indeterminate";
btnToggle.Size = new Size((int)(Font.Height * .75) * btnToggle.Text.Length, Font.Height + 10);
btnToggle.Location = new Point( clb.Left, clb.Bottom + 10);
btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnToggle.Click += new System.EventHandler(btnToggle_Click);
// Clear Button
btnClear = new Button();
btnClear.Parent = this;
btnClear.Text = "Clear All";
btnClear.Size = new Size((int)(Font.Height * .75) * btnClear.Text.Length, Font.Height + 10);
btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnClear.Click += new System.EventHandler(btnClear_Click);
clb.Items.Add("12345");
clb.Items.Add("67890");
clb.Items.Add("7890");
clb.Items.Add("890");
}
static void Main()
{
Application.Run(new CheckedListBoxes());
}
private void btnToggle_Click(object sender, EventArgs e)
{
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
if (clb.GetItemCheckState(i) == CheckState.Checked)
{
clb.SetItemCheckState(i, CheckState.Indeterminate);
}
else if (clb.GetItemCheckState(i) == CheckState.Indeterminate)
{
clb.SetItemCheckState(i, CheckState.Checked);
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
clb.ClearSelected();
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
clb.SetItemChecked(i, false);
}
}
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
Console.WriteLine("Current Index:" + e.Index.ToString() );
Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
Console.WriteLine("New Value:" + e.NewValue.ToString() );
}
private void clb_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
}
private void this_Load(object sender, EventArgs e)
{
clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
}
}
CheckedListBox: get selection state
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class CheckedListBoxes : Form
{
CheckedListBox clb;
Button btnToggle;
Button btnClear;
String str;
public CheckedListBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
clb = new CheckedListBox();
clb.Parent = this;
clb.Location = new Point(10,10);
clb.Size = new Size(ClientSize.Width - 20, Height - 240);
clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
clb.BorderStyle = BorderStyle.Fixed3D;
clb.MultiColumn = true;
clb.ScrollAlwaysVisible = true;
clb.ThreeDCheckBoxes = true;
clb.CheckOnClick = true;
clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);
// Toggle Indeterminate Button
btnToggle = new Button();
btnToggle.Parent = this;
btnToggle.Text = "Toggle Indeterminate";
btnToggle.Size = new Size((int)(Font.Height * .75) * btnToggle.Text.Length, Font.Height + 10);
btnToggle.Location = new Point( clb.Left, clb.Bottom + 10);
btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnToggle.Click += new System.EventHandler(btnToggle_Click);
// Clear Button
btnClear = new Button();
btnClear.Parent = this;
btnClear.Text = "Clear All";
btnClear.Size = new Size((int)(Font.Height * .75) * btnClear.Text.Length, Font.Height + 10);
btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnClear.Click += new System.EventHandler(btnClear_Click);
clb.Items.Add("12345");
clb.Items.Add("67890");
clb.Items.Add("7890");
clb.Items.Add("890");
}
static void Main()
{
Application.Run(new CheckedListBoxes());
}
private void btnToggle_Click(object sender, EventArgs e)
{
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
if (clb.GetItemCheckState(i) == CheckState.Checked)
{
clb.SetItemCheckState(i, CheckState.Indeterminate);
}
else if (clb.GetItemCheckState(i) == CheckState.Indeterminate)
{
clb.SetItemCheckState(i, CheckState.Checked);
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
clb.ClearSelected();
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
clb.SetItemChecked(i, false);
}
}
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
Console.WriteLine("Current Index:" + e.Index.ToString() );
Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
Console.WriteLine("New Value:" + e.NewValue.ToString() );
}
private void clb_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
}
private void this_Load(object sender, EventArgs e)
{
clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
}
}
CheckedListBox: selected change event
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class CheckedListBoxes : Form
{
CheckedListBox clb;
Button btnToggle;
Button btnClear;
String str;
public CheckedListBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
clb = new CheckedListBox();
clb.Parent = this;
clb.Location = new Point(10,10);
clb.Size = new Size(ClientSize.Width - 20, Height - 240);
clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
clb.BorderStyle = BorderStyle.Fixed3D;
clb.MultiColumn = true;
clb.ScrollAlwaysVisible = true;
clb.ThreeDCheckBoxes = true;
clb.CheckOnClick = true;
clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);
// Toggle Indeterminate Button
btnToggle = new Button();
btnToggle.Parent = this;
btnToggle.Text = "Toggle Indeterminate";
btnToggle.Size = new Size((int)(Font.Height * .75) * btnToggle.Text.Length, Font.Height + 10);
btnToggle.Location = new Point( clb.Left, clb.Bottom + 10);
btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnToggle.Click += new System.EventHandler(btnToggle_Click);
// Clear Button
btnClear = new Button();
btnClear.Parent = this;
btnClear.Text = "Clear All";
btnClear.Size = new Size((int)(Font.Height * .75) * btnClear.Text.Length, Font.Height + 10);
btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnClear.Click += new System.EventHandler(btnClear_Click);
clb.Items.Add("12345");
clb.Items.Add("67890");
clb.Items.Add("7890");
clb.Items.Add("890");
}
static void Main()
{
Application.Run(new CheckedListBoxes());
}
private void btnToggle_Click(object sender, EventArgs e)
{
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
if (clb.GetItemCheckState(i) == CheckState.Checked)
{
clb.SetItemCheckState(i, CheckState.Indeterminate);
}
else if (clb.GetItemCheckState(i) == CheckState.Indeterminate)
{
clb.SetItemCheckState(i, CheckState.Checked);
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
clb.ClearSelected();
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
clb.SetItemChecked(i, false);
}
}
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
Console.WriteLine("Current Index:" + e.Index.ToString() );
Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
Console.WriteLine("New Value:" + e.NewValue.ToString() );
}
private void clb_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
}
private void this_Load(object sender, EventArgs e)
{
clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
}
}
CheckedListBox: set item selected
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
public class CheckedListBoxes : Form
{
CheckedListBox clb;
Button btnToggle;
Button btnClear;
String str;
public CheckedListBoxes()
{
Size = new Size(300,400);
this.Load += new EventHandler(this_Load);
clb = new CheckedListBox();
clb.Parent = this;
clb.Location = new Point(10,10);
clb.Size = new Size(ClientSize.Width - 20, Height - 240);
clb.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom;
clb.BorderStyle = BorderStyle.Fixed3D;
clb.MultiColumn = true;
clb.ScrollAlwaysVisible = true;
clb.ThreeDCheckBoxes = true;
clb.CheckOnClick = true;
clb.ItemCheck += new ItemCheckEventHandler(clb_ItemCheck);
// Toggle Indeterminate Button
btnToggle = new Button();
btnToggle.Parent = this;
btnToggle.Text = "Toggle Indeterminate";
btnToggle.Size = new Size((int)(Font.Height * .75) * btnToggle.Text.Length, Font.Height + 10);
btnToggle.Location = new Point( clb.Left, clb.Bottom + 10);
btnToggle.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnToggle.Click += new System.EventHandler(btnToggle_Click);
// Clear Button
btnClear = new Button();
btnClear.Parent = this;
btnClear.Text = "Clear All";
btnClear.Size = new Size((int)(Font.Height * .75) * btnClear.Text.Length, Font.Height + 10);
btnClear.Location = new Point(btnToggle.Left, btnToggle.Bottom + 10);
btnClear.Anchor = AnchorStyles.Left | AnchorStyles.Bottom;
btnClear.Click += new System.EventHandler(btnClear_Click);
clb.Items.Add("12345");
clb.Items.Add("67890");
clb.Items.Add("7890");
clb.Items.Add("890");
}
static void Main()
{
Application.Run(new CheckedListBoxes());
}
private void btnToggle_Click(object sender, EventArgs e)
{
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
if (clb.GetItemCheckState(i) == CheckState.Checked)
{
clb.SetItemCheckState(i, CheckState.Indeterminate);
}
else if (clb.GetItemCheckState(i) == CheckState.Indeterminate)
{
clb.SetItemCheckState(i, CheckState.Checked);
}
}
}
private void btnClear_Click(object sender, EventArgs e)
{
clb.ClearSelected();
for (int i = 0; i <= (clb.Items.Count - 1); i++)
{
clb.SetItemChecked(i, false);
}
}
private void clb_ItemCheck(object sender, ItemCheckEventArgs e)
{
Console.WriteLine("Current Item:" + clb.GetItemText(clb.Items[e.Index]));
Console.WriteLine("Current Index:" + e.Index.ToString() );
Console.WriteLine("Current Value:" + e.CurrentValue.ToString() );
Console.WriteLine("New Value:" + e.NewValue.ToString() );
}
private void clb_SelectedIndexChanged(object sender, EventArgs e)
{
Console.WriteLine("Selected Items:" + clb.SelectedItems.Count.ToString() );
Console.WriteLine("Checked Items:" + clb.CheckedItems.Count.ToString() );
}
private void this_Load(object sender, EventArgs e)
{
clb.SelectedIndexChanged += new EventHandler(clb_SelectedIndexChanged);
}
}
Copy selected CheckedItems to another CheckedListBox
using System;
using System.Collections.Generic;
using System.ruponentModel;
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());
}
}