c#练习代码集锦

上传人:沈*** 文档编号:107814790 上传时间:2022-06-15 格式:DOC 页数:78 大小:618.50KB
返回 下载 相关 举报
c#练习代码集锦_第1页
第1页 / 共78页
c#练习代码集锦_第2页
第2页 / 共78页
c#练习代码集锦_第3页
第3页 / 共78页
亲,该文档总共78页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
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 (radioButton1.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 + 你的爱好是: + 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 (canAdd)/没有相同项时,执行下面的操作 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, 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 = false) 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 转换成角度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)comboBox1.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; hScrollBar1.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_CheckedChanged(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 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 = 纯文本(*.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.PlainText); 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 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.SaveFile(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 = DateTime.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 sender, 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 sender, EventArgs e) richTextBox1.SelectionFont =new Font(richTextBox1.Font.FontFamily,richTextBox1.SelectionFont.Size,FontStyle.Bold); private void toolStripComboBox1_TextChanged(object sender, EventArgs e) if( richTextBox1.SelectionFont.Size != float.Parse(toolStripComboBox1.Text) if (richTextBox1.SelectionFont.FontFamily != null) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, float.Parse(toolStripComboBox1.Text),richTextBox1.SelectionFont.Style); else richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, float.Parse(toolStripComboBox1.Text),richTextBox1.SelectionFont.Style); private void contextMenuStrip1_Opening(object sender, CancelEventArgs e) private void 左对齐ToolStripMenuItem_Click(object sender, EventArgs e) richTextBox1.SelectionAlignment = HorizontalAlignment.Left; private void 居中ToolStripMenuItem_Click(object sender, EventArgs e) richTextBox1.SelectionAlignment = HorizontalAlignment.Center; private void 右对齐ToolStripMenuItem_Click(object sender, EventArgs e) richTextBox1.SelectionAlignment = HorizontalAlignment.Right; private void toolStripButton3_Click_1(object sender, EventArgs e)/加粗 if (richTextBox1.SelectionFont.FontFamily != null) if (richTextBox1.SelectionFont.Bold=true) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style&( FontStyle.Bold ); else richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Bold | richTextBox1.SelectionFont.Style); else if (richTextBox1.SelectionFont.Bold = true) richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style & (FontStyle.Bold); else richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Bold | richTextBox1.SelectionFont.Style); private void toolStripButton4_Click(object sender, EventArgs e)/斜体 if (richTextBox1.SelectionFont.FontFamily != null) if (richTextBox1.SelectionFont.Italic=true) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style&( FontStyle.Italic ); else richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Italic | richTextBox1.SelectionFont.Style); / richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Italic | richTextBox1.SelectionFont.Style); else if (richTextBox1.SelectionFont.Italic = true) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style & (FontStyle.Italic); else richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Italic | richTextBox1.SelectionFont.Style); / richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Italic | richTextBox1.SelectionFont.Style); private void toolStripButton5_Click(object sender, EventArgs e)/下划线 if (richTextBox1.SelectionFont.FontFamily != null) if (richTextBox1.SelectionFont.Underline=true) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style&( FontStyle.Underline); else richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Underline | richTextBox1.SelectionFont.Style); /richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Underline | richTextBox1.SelectionFont.Style); else if (richTextBox1.SelectionFont.Underline = true) richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, richTextBox1.SelectionFont.Style & (FontStyle.Underline); else richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Underline | richTextBox1.SelectionFont.Style); /richTextBox1.SelectionFont = new Font(richTextBox1.Font.FontFamily, richTextBox1.SelectionFont.Size, FontStyle.Underline | richTextBox1.SelectionFont.Style); 6.Timer、g.DrawEllipse() int r = 20; int increase = 10; int tick = 0; int x, y; private void pictureBox1_MouseClick(object sender, MouseEventArgs e) Graphics g = pictureBox1.CreateGraphics(); g.Clear(pictureBox1.BackColor); tick = 0; x = e.X; y = e.Y; timer1.Enabled = true; private void drawCircle(int r) Graphics g = pictureBox1.CreateGraphics(); / g.Clear(pictureBox1.BackColor); Pen myPen = new Pen(Color.Blue); g.DrawEllipse(myPen, x - r, y - r, 2*r,2*r);/画出半径为r的圆 private void timer1_Tick(object sender, EventArgs e) int R=r + increase * tick; if (R x | R y | R pictureBox1.Width - x | R pictureBox1.Height - y) timer1.Enabled = false; else drawCircle(R); tick+; 7、绘制闭合曲线Point p1, p2,startP; Pen myPen = new Pen(Color.Red); private void Form1_MouseDown(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left)/左键按下,记录开始点 p1.X = e.X; p1.Y = e.Y; startP.X= e.X; startP.Y = e.Y; private void Form1_MouseMove(object sender, MouseEventArgs e) if (e.Button = MouseButtons.Left)/左键按下,开始画线 p2.X = e.X; p2.Y = e.Y; Graphics g = CreateGraphics(); g.DrawLine(myPen, p1, p2); p1.X = p2.X; p1.Y = p2.Y; private void Form1_MouseUp(object sender, MouseEventArgs e) /闭合线 Graphics g = CreateGraphics(); g.DrawLine(myPen, startP, p2); 8.读写点private void button1_Click(object sender, EventArgs e)/读写点 Graphics g = pictureBox1.CreateGraphics(); g.Clear(pictureBox1.BackColor); Bitmap bmp = new Bitmap(茶1.jpg); int pw = bmp.Width; int ph = bmp.Height; Bitmap bmp2 = new Bitmap(pw, ph); int w = pictureBox1.Width; int h = pictureBox1.Height; Pen myPen = new Pen(Color.Red); ; for (int i = 0; i w / 2; i+) for (int j = 0; j ph; j+) Color cl = bmp.GetPixel(w / 2 + i, j); bmp2.SetPixel(w / 2 + i, j, cl); Color c2 = bmp.GetPixel(w / 2 - i, j); bmp2.SetPixel(w / 2 - i, j, c2); g.DrawImage(bmp2, 0, 0); private void button2_Click(object sender, EventArgs e)/drawImage Graphics g = pictureBox1.CreateGraphics(); Bitmap bmp = null; bmp=new Bitmap(茶1.jpg); int w=pictureBox1.Width; int h=pictureBox1.Height; g.Clear(pictureBox1.BackColor); for (int y = 0; y = pictureBox1.Width/2; y+) Rectangle dest = new Rectangle(w/ 2 - y, 0, y*2 , h); Rectangle src = new Rectangle(0, 0, bmp.Width, bmp.Height); g.DrawImage(bmp, dest, src, GraphicsUnit.Pixel); -
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!