实验课存档
实验目的:
1.练习Winform程序的创建过程
2.练习listBox控件的使用方法
3.应用事件机制动态设置控件属性
实验内容:
1.在VS中新建项目chapter1;
2.添加窗体,调整窗体属性并布局。其中Botton1、Botton2、Botton3和Botton4控件分别命名为btn1、btn2、btn3和btn4;
3.将listBox1与listBox2的SelectionMode属性改为MultiExtended;
4.为btn1、btn2、btn3和btn4的Click事件编写代码。
代码:
private void btn1_Click(object sender, EventArgs e)
{
foreach (object x in this.listBox1.Items)
this.listBox2.Items.Add(x);
this.listBox1.Items.Clear();
}
private void btn2_Click(object sender, EventArgs e)
{
if (this.listBox1.SelectedItems.Count > 0)
{
string check1 = this.listBox1.SelectedItem.ToString();
if (!this.listBox2.Items.Contains(check1))
{
//添加到listbox2中
this.listBox2.Items.Add(check1);
//移除listbox1中
this.listBox1.Items.Remove(check1);
}
else
{
MessageBox.Show("已转移!");
}
}
else
{
MessageBox.Show("未选中!");
}
}
private void btn3_Click(object sender, EventArgs e)
{
if (this.listBox2.SelectedItems.Count > 0)
{
string check1 = this.listBox2.SelectedItem.ToString();
//判断是否添加到listbox1
if (!this.listBox1.Items.Contains(check1))
{
//添加到listbox1中
this.listBox1.Items.Add(check1);
//移除listbox1中
this.listBox2.Items.Remove(check1);
}
else
{
MessageBox.Show("已转移!");
}
}
else
{
MessageBox.Show("未选中!");
}
}
private void btn4_Click(object sender, EventArgs e)
{
foreach (object x in this.listBox2.Items)
this.listBox1.Items.Add(x);
this.listBox2.Items.Clear();
}
运行结果:
ps:有任何问题,欢迎留言。