c#练习代码集锦.doc
《c#练习代码集锦.doc》由会员分享,可在线阅读,更多相关《c#练习代码集锦.doc(78页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、Four short words sum up what has lifted most successful individuals above the crowd: a little bit more.-author-datec#练习代码集锦c#练习代码集锦天津市大学软件园2011-2012学年C#程序设计复习集锦1RadioButton、 CheckBox关键代码:private void button1_Click(object sender, EventArgs e)string str,name,sex,ah=; name = textBox1.Text; if (radioBut
2、ton1.Checked) sex = radioButton1.Text; else if (radioButton2.Checked) sex = radioButton2.Text; else sex = 未知; if (checkBox1.Checked) ah += checkBox1.Text; if (checkBox2.Checked) ah += + checkBox2.Text; if (checkBox3.Checked) ah += + checkBox3.Text; str = 你的姓名是: + name + rn + 你的性别是: + sex + rn + 你的爱好
3、是: + ah;2ListBox private void button2_Click(object sender, EventArgs e) bool canAdd = true; string minfo=; if (textBox1.Text = ) canAdd = false; minfo = 添加项不能为空!; else int z= listBox1.FindStringExact(textBox1.Text); if (z != -1) canAdd = false; minfo = 列表中已经存在“ + textBox1.Text + ”,无法完成添加操作!; if (can
4、Add)/没有相同项时,执行下面的操作 listBox1.Items.Add(textBox1.Text); else/否则提示信息 DialogResult dlogRs= MessageBox.Show(minfo, 系统提示, MessageBoxButtons.OK, MessageBoxIcon.Information); if (dlogRs = DialogResult.OK) textBox1.Focus(); textBox1.Select(0, textBox1.Text.Length); private void button1_Click(object sender,
5、EventArgs e) if (listBox2.Items.Count -1) listBox2.Items.Clear(); button1.Enabled = false; private void listBox1_DoubleClick(object sender, EventArgs e) if (listBox1.SelectedIndex != -1) listBox2.Items.Add(listBox1.SelectedItem); listBox1.Items.Remove(listBox1.SelectedItem); if (button1.Enabled = fa
6、lse) button1.Enabled = true; 3.Math类private void button1_Click(object sender, EventArgs e)if (comboBox2.SelectedIndex!=-1|comboBox2.Text!=)double ch = double.Parse(comboBox2.Text);double result=0;switch (comboBox1.SelectedIndex)case 0: result = Math.Sin(ch * Math.PI / 180); break;/x* Math.PI / 180 转
7、换成角度case 1: result = Math.Cos(ch * Math.PI / 180); break;case 2: result = Math.Sqrt(ch); break;label3.Text = 结果= + result.ToString(0.00);elsestring minfo = 请选择或输入一个值!;MessageBox.Show(minfo, 系统提示, MessageBoxButtons.OK, MessageBoxIcon.Information);private void Form1_Load(object sender, EventArgs e)com
8、boBox1.SelectedIndex = 0;4.PictureBox、RadioButton、ScrollBar private void Form1_Load(object sender, EventArgs e) radioButton1.Checked=true; pictureBox1.Load(图片1.jpg);/注意:在调试时需要将图片复制到Debug目录下 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Width = 200; hScrollBar1.Minimum = 200; hS
9、crollBar1.Maximum = 300; hScrollBar1.Value = 200; hScrollBar1.SmallChange = 2; hScrollBar1.LargeChange = 5; private void radioButton2_CheckedChanged(object sender, EventArgs e) if (radioButton1.Checked) pictureBox1.Load(图片1.jpg); else pictureBox1.Load(图片2.jpg); private void radioButton1_CheckedChang
10、ed(object sender, EventArgs e) /注意 此处也可radioButton2 共用一段代码,这里进行了改写 if (radioButton2.Checked) pictureBox1.Load(图片2.jpg); else pictureBox1.Load(图片1.jpg); private void hScrollBar1_Scroll(object sender, ScrollEventArgs e) pictureBox1.Width = hScrollBar1.Value; 5.RichTextBox、FontDialog、ColorDialog 、Menu
11、private void 退出UToolStripMenuItem_Click(object sender, EventArgs e) this.Close(); private void 打开ToolStripMenuItem_Click(object sender, EventArgs e) openFileDialog1.InitialDirectory = C:Documents and SettingsAdministratorMy Documents;/设定默认目录 openFileDialog1.FileName = ;/默认文件名 openFileDialog1.Filter
12、= 纯文本(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf;/文件过滤 openFileDialog1.FilterIndex = 2;/设定默认过滤器 if (openFileDialog1.ShowDialog() = DialogResult.OK) toolStripStatusLabel1.Text = openFileDialog1.FileName; if (openFileDialog1.FilterIndex = 1) richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.P
13、lainText); else richTextBox1.LoadFile(openFileDialog1.FileName); private void 保存ToolStripMenuItem_Click(object sender, EventArgs e) string fname = openFileDialog1.FileName.ToString(); saveFileDialog1.AddExtension = true; if (fname != & fname != openFileDialog1) saveFileDialog1.FileName = fname; else
14、 saveFileDialog1.FileName = 新建文件; saveFileDialog1.Filter = 纯文本(*.txt)|*.txt|RTF文件(*.rtf)|*.rtf; if (fname != & fname != openFileDialog1) saveFileDialog1.FilterIndex = openFileDialog1.FilterIndex; if (saveFileDialog1.ShowDialog() = DialogResult.OK) if (saveFileDialog1.FilterIndex = 1) richTextBox1.Sa
15、veFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText); else richTextBox1.SaveFile(saveFileDialog1.FileName); /将是否修改值 重置 richTextBox1.Modified = false; private void Form1_Load(object sender, EventArgs e) toolStripComboBox1.SelectedIndex = 0;/字号列表初始化 选择第一项 toolStripStatusLabel2.Text = Date
16、Time.Now.ToLocalTime().ToString();/2011-11-19 16:08:07 toolStripStatusLabel1.Text =暂未打开任何文件!; private void 字体ToolStripMenuItem_Click(object sender, EventArgs e) FontDialog fdg = new FontDialog(); fdg.ShowDialog(); richTextBox1.SelectionFont = fdg.Font; private void 颜色ToolStripMenuItem_Click(object s
17、ender, EventArgs e) ColorDialog cdg = new ColorDialog(); cdg.ShowDialog(); richTextBox1.SelectionColor = cdg.Color; private void timer1_Tick(object sender, EventArgs e) toolStripStatusLabel2.Text = DateTime.Now.ToLocalTime().ToString();/2011-11-19 16:08:07 private void toolStripButton3_Click(object
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- c# 练习 代码 集锦
限制150内