C# 基本控件使用练习
自己设计并编写一个 Windows 应用程序,要求用到 TextBox、GroupBox、RadioButton、CheckBox、ComboBox、ListBox 控件。
代码如下:
页面1:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //页面1 namespace Testing3_2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string name = textName.Text; string sex; if (radioMan.Checked == true) { sex = radioMan.Text; } else { sex = radioWoman.Text; } string age = textAge.Text; string strclass = comboBox1.Text; string strhobby=""; if (checkBox1.Checked == true) { strhobby = strhobby + checkBox1.Text+" "; } if (checkBox2.Checked == true) { strhobby = strhobby+checkBox2.Text + " "; } if (checkBox3.Checked == true) { strhobby = strhobby + checkBox3.Text + " "; } if (checkBox4.Checked == true) { strhobby = strhobby + checkBox4.Text + " "; } string text = listBox2.Text; Form2 frm = new Form2();//实例化form2 Form2.frms.setLabel(name,sex,age, strclass, strhobby, text); this.Hide();//隐藏现在这个窗口 frm.Show();//打开新窗体 } private void groupBox1_Enter(object sender, EventArgs e) { } private void buttonin_Click(object sender, EventArgs e) { int i; for (i = 0; i < listBox1.SelectedItems.Count; i++) { string str1 = listBox1.SelectedItems[i] + ""; if (!(listBox2.Items.Contains(str1))) { listBox2.Items.Add(str1); } listBox1.Items.Remove(str1); } } private void buttonout_Click(object sender, EventArgs e) { int i; for (i = 0; i < listBox2.SelectedItems.Count; i++) { string str1 = listBox2.SelectedItems[i] + ""; if (!(listBox1.Items.Contains(str1))) { listBox1.Items.Add(str1); } listBox2.Items.Remove(str1); } } private void textAge_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } } } }
页面2:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Testing3_2 { public partial class Form2 : Form { public static Form2 frms = null; public Form2() { InitializeComponent(); frms = this; } public void setLabel(string name,string sex,string age,string strclass,string strhobby,string text) { label1.Text = name; label2.Text = sex; label3.Text = age; label4.Text = strclass; label6.Text = strhobby; label5.Text = text; } private void button1_Click(object sender, EventArgs e) { //this.Close(); System.Environment.Exit(0); } } }