實驗課存檔
實驗目的:
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:有任何問題,歡迎留言。