📄 sample16.cs
字号:
namespace apiBook
{
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO ;
public class Form1 : System.Windows.Forms.Form
{
private CheckedListBox checkedListBox1;
private TextBox textBox1;
private Button button1;
private Button button2;
private ListBox listBox1;
private Button button3;
private System.ComponentModel.Container components;
public Form1()
{
InitializeComponent();
string[] animalType = {"Dog", "Cat","Tiger","Monkey"};
checkedListBox1.Items.AddRange(animalType);
checkedListBox1.CheckOnClick = true;
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
this.textBox1.Location = new Point(144, 16);
this.textBox1.Size = new System.Drawing.Size(88, 22);
this.textBox1.TabIndex = 1;
this.textBox1.TextChanged+=new EventHandler(this.textBox1_TextChanged);
this.checkedListBox1.Location = new System.Drawing.Point(16, 16);
this.checkedListBox1.Size = new System.Drawing.Size(100, 160);
this.checkedListBox1.TabIndex = 0;
this.checkedListBox1.ItemCheck+=new ItemCheckEventHandler(this.checkedListBox1_ItemCheck);
this.listBox1.Location=new System.Drawing.Point(255, 16);
this.listBox1.Size = new System.Drawing.Size(100, 160);
this.listBox1.TabIndex = 1;
this.button1.Enabled = false;
this.button1.Location =
new System.Drawing.Point(144, 40);
this.button1.Size = new System.Drawing.Size(88, 22);
this.button1.TabIndex = 2;
this.button1.Text = "添加动物种类";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button2.Enabled = false;
this.button2.Location =
new System.Drawing.Point(144, 70);
this.button2.Size = new System.Drawing.Size(88, 22);
this.button2.TabIndex = 2;
this.button2.Text = "排序";
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button3.Enabled = false;
this.button3.Location = new System.Drawing.Point(144, 100);
this.button3.Size = new System.Drawing.Size(88, 22);
this.button3.TabIndex = 2;
this.button3.Text = "保存该次序";
this.button3.Click += new System.EventHandler(this.button3_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(400, 150);
this.Controls.AddRange(new Control[] {this.listBox1, this.button3, this.button2,this.button1, this.textBox1, this.checkedListBox1});
this.Text = "动物名称排列次序:";
}
[STAThread]
public static void Main(string[] args)
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
if(textBox1.Text != "")
{
if(checkedListBox1.CheckedItems.Contains(textBox1.Text)== false)
checkedListBox1.Items.Add(textBox1.Text,CheckState.Checked);
textBox1.Text = "";
}
}
private void textBox1_TextChanged(object sender,EventArgs e)
{
if (textBox1.Text == "")
{
button1.Enabled = false;
}
else
{
button1.Enabled = true;
}
}
private void button2_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
button3.Enabled=false;
for (int i=0; i< checkedListBox1.CheckedItems.Count;i++)
{
listBox1.Items.Add(checkedListBox1.CheckedItems[i]);
}
if (listBox1.Items.Count>0)
button3.Enabled=true;
}
private void checkedListBox1_ItemCheck (object sender,ItemCheckEventArgs e)
{
if(e.NewValue==CheckState.Unchecked)
{
if(checkedListBox1.CheckedItems.Count==1)
{
button2.Enabled = false;
}
}
else
{
button2.Enabled = true;
}
}
private void button3_Click(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
IEnumerator myEnumerator;
myEnumerator =
checkedListBox1.CheckedIndices.GetEnumerator();
int y;
while (myEnumerator.MoveNext() != false)
{
y =(int) myEnumerator.Current;
checkedListBox1.SetItemChecked(y, false);
}
button3.Enabled = false ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -