实验五:C控件应用_第1页
实验五:C控件应用_第2页
实验五:C控件应用_第3页
实验五:C控件应用_第4页
实验五:C控件应用_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、武夷学院实验报告课程名称:_.NET程序设计_ 项目名称:_ C#控件应用 姓名:_陈东煌_专业:_计算机科学与技术_ 班级:_1_学号:_35_同组成员_一、 实验预习部分 注:1、实验预习部分包括实验环境准备和实验所需知识点准备。2、若是单人单组实验,同组成员填无。:1. 软硬件环境:PC一台、Windows系统、Visual Studiao 20102. 实验准备内容:(1) Windows窗体应用程序 Windows窗体应用程序是运行在用户计算机本地的基于Windows的应用程序,提供丰富的用户界面以实现用户交互,并可以访问操作系统服务和用户计算环境提供的资源,从而实现各种复杂功能的应

2、用程序。由于Windows窗体应用程序涉及复杂的用户界面和事件处理过程,故一般通过集成开发环境Visual Studio开发和调试Windows窗体应用程序。(2) 创建Windows窗体应用程序一般步骤1、创建Windows窗体项目2、创建用户界面3、添加程序代码4、运行和测试程序5、保存项目(3) 常用Windows窗体控件 Label控件、LinkLabel控件、TextBox控件、RichTextBox控件、MaskedTextBox控件、Button控件;RadioButton控件、CheckBox控件、GroupBox控件;ComboBox控件、ListBox控件、CheckedL

3、istBox控件;PictureBox控件、ImageList控件;Timer控件(4) 通用对话框OpenFileDialogSaveFileDialogColorDialogFontDialogPageSetupDialogPrintDialogPrintPreviewDialogFolderBrowserDialog(5) 菜单和工具栏(1) 主菜单:提供窗体的菜单系统。MenuStrip控件用于实现主菜单(2) 上下文菜单:通过鼠标右击某对象而弹出的菜单。ContextMenuStrip控件用于实现上下文菜单。(3)工具栏:通过单击工具栏上的图标,可以执行相关的操作。ToolStrip

4、控件用于实现工具栏。(6) 多重窗体1、添加新窗体2、调用其他窗体:要调用其他窗体,可以再相应的按钮/菜单命令的事件处理程序中,通过下列类似代码,创建(实例化)并显示一个窗体。例如,要显示Form2,可以使用下列代码: Form2 f2 =new Form2();           /创建Form2的实例 f2.ShowDialog();           

5、60;   /显示Form2的实例。也可以使用f2.Show()调用 3、多重窗体应用(7) 多文档界面(1)创建MDI父窗体(2)创建MDI子窗体在MDI父窗体的主菜单或工具栏命令事件处理程序中,可以使用代码创建并显示MDI子窗体的实例:Form2 FormChild=new Form2();FormChild.MdiParent=this;FormChild.show();        (3)处理MDI子窗体二、 实验过程记录 注:实验过程记录要包含实验目的、实验原理、实验步骤,页码不够可

6、自行添加。:实验指导书:实验15-1实验15615-1:(1) 创建Windows窗体应用程序(2)窗体设计,如下:(3)生成并处理CtoF_Click事件和FtoC_Click事件的代码如下: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.Form

7、s;namespace sy15_1 public partial class Form1 : Form public Form1() InitializeComponent(); private void CtoF_Click(object sender, EventArgs e) double c = 9 * double.Parse(textBoxC.Text) / 5 + 32; textBoxF.Text = c.ToString(); private void FtoC_Click(object sender, EventArgs e) double f = 5 * (double

8、.Parse(textBoxF.Text) - 32) / 9; textBoxC.Text = f.ToString(); (4)运行并测试应用程序 15-2:(1) 创建Windows应用程序(2) 窗体设计,如下:(3) 创建处理控件事件代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.T

9、asks;using System.Windows.Forms;namespace sy12_2 public partial class Form1 : Form public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) Message.Text = stuName.Text + ",您好,欢迎进入C#世界!n" Message.Text += "您的密码是:" + Password.Text; if(radioButton1

10、.Checked) Message .Text+="n您的性别是:"+radioButton1.Text ; else if (radioButton2.Checked) Message.Text +="n您的性别是:"+radioButton2.Text ; if (School.SelectedIndex > -1) Message.Text += "n您的学校是:" + School.SelectedItem.ToString(); else Message.Text += "n您没有选择学校" if

11、(Major.SelectedIndex > -1) Message.Text += "n您的专业是:" + Major.SelectedItem.ToString(); else Message.Text += "n您没有选择专业" Message.Text += "n您的爱好是" if (checkBox1.Checked) Message.Text += checkBox1.Text + "" if (checkBox2.Checked) Message.Text += checkBox2.Text +

12、 "" if (checkBox3.Checked) Message.Text += checkBox3.Text + "" if (checkBox4.Checked) Message.Text += checkBox4.Text + "" if (!checkBox1.Checked) & (!checkBox2.Checked) & (!checkBox3.Checked) & (!checkBox4.Checked) Message.Text += "您居然没有兴趣爱好!" (4)

13、运行并测试应用程序15-3:(1) 创建Windows应用程序(2) 窗体设计,如下:(3) 创建处理控件事件 生成并处理,代码如下: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 sy15_3 public part

14、ial class Form1 : Form public Form1() InitializeComponent(); private void openFileDialog1_FileOk(object sender, CancelEventArgs e) private void Open_Click(object sender, EventArgs e) OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "C:C#LABimages" o

15、penFileDialog1.Filter = "Image Files(*.BMP;*.JPG;*.GIF|*.BMP;*.JPG;*.GIF" openFileDialog1.RestoreDirectory = true; if (openFileDialog1 .ShowDialog()=DialogResult .OK) pictureBox1.Load(openFileDialog1.FileName); private void Stretch_Click(object sender, EventArgs e) pictureBox1.SizeMode = P

16、ictureBoxSizeMode.StretchImage; private void Center_Click(object sender, EventArgs e) pictureBox1.SizeMode = PictureBoxSizeMode.CenterImage; private void Zoom_Click(object sender, EventArgs e) pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; private void AutoSize_Click(object sender, EventArgs e) pic

17、tureBox1.SizeMode = PictureBoxSizeMode.AutoSize; (4) 运行并测试应用程序拉伸后:居中后:缩放后:自动大小: 15-4: (1)创建Windows应用程序 (2)窗体设计,如下: (3)创建处理控件事件的方法 代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Thr

18、eading.Tasks;using System.Windows.Forms;namespace sy15_4public partial class Form1:Form private long ticks; public Form1() InitializeComponent(); private void button1_Click(object sender, EventArgs e) if(button1 .Text ="开始") button1.Text = "暂停" timer1.Start(); else button1.Text =

19、 "开始" timer1.Stop(); private void button2_Click(object sender, EventArgs e) button1.Text = "开始" timer1.Stop(); label1.Text = "00:00:00:00" private void Form1_Load(object sender, EventArgs e) ticks =0; private void timer1_Tick(object sender, EventArgs e) ticks+; long hou

20、rs = ticks / 360000; long minutes = (ticks - hours * 360000) / 6000; long seconds = (ticks - hours * 360000 - minutes * 6000) / 100; long ms = ticks - hours * 360000 - minutes * 6000 - seconds * 100; label1.Text = string.Format("0:00:1:00:2:00:3:00", hours, minutes, seconds, ms); (4)运行并测试应

21、用程序 点击开始按钮,然后点击暂停按钮: 点击停止按钮,计时器清零: 15-5: (1)创建Windows应用程序 (2)窗体设计,如下: (3)程序代码: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 sy15_5

22、public partial class Form1 : Form public Form1() InitializeComponent(); private void buttonOpen_Click(object sender, EventArgs e) OpenFileDialog openFileDialog1 = new OpenFileDialog(); openFileDialog1.InitialDirectory = "c" openFileDialog1.Filter = "rft files(*.rtf)|*.rtf" openFi

23、leDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if(openFileDialog1 .ShowDialog()=DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName); private void buttonSave_Click(object sender, EventArgs e) SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.Init

24、ialDirectory = "c" saveFileDialog1.Filter = "rft files(*.rtf)|*.rtf" saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog()=DialogResult .OK) richTextBox1.SaveFile(saveFileDialog1.FileName); private void buttonFont_Click(object

25、sender, EventArgs e) FontDialog fontDialog1 = new FontDialog(); fontDialog1.ShowColor = true; fontDialog1.Font = richTextBox1.SelectionFont; fontDialog1.Color = richTextBox1.SelectionColor; if(fontDialog1.ShowDialog()!=DialogResult.Cancel) richTextBox1.SelectionFont = fontDialog1.Font; richTextBox1.

26、SelectionColor = fontDialog1.Color; private void buttonExit_Click(object sender, EventArgs e) this.Close(); (4)运行并测试应用程序 打开电脑文件夹中的*.rtf文件, 点击“字体”按钮,经修改字体后如下: 再点击保存文件到指定文件夹中。 15-6: (1)创建Windows应用程序 (2)窗体设计,如下: (3)创建处理控件事件的方法 代码:using System;using System.Collections.Generic;using System.ComponentModel

27、;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace sy15_6 public partial class Form1 : Form public Form1() InitializeComponent(); private void richTextBox1_TextChanged(object sender, EventArgs e) private void

28、编辑ToolStripMenuItem_Click(object sender, EventArgs e) private void 新建NToolStripMenuItem_Click(object sender, EventArgs e) richTextBox1.Clear(); this.Text = "新建文档" private void 打开OToolStripMenuItem_Click(object sender, EventArgs e) OpenFileDialog openFileDialog1 = new OpenFileDialog(); open

29、FileDialog1.InitialDirectory = "c" openFileDialog1.Filter = "rft files(*.rtf)|*.rtf" openFileDialog1.FilterIndex = 2; openFileDialog1.RestoreDirectory = true; if (openFileDialog1.ShowDialog() = DialogResult.OK) richTextBox1.LoadFile(openFileDialog1.FileName); private void 保存STool

30、StripMenuItem_Click(object sender, EventArgs e) SaveFileDialog saveFileDialog1 = new SaveFileDialog(); saveFileDialog1.InitialDirectory = "c" saveFileDialog1.Filter = "rft files(*.rtf)|*.rtf" saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; if (saveFileDialog1.ShowDialog()=DialogResult .OK) richTextBox1.SaveFile(saveFileDialog1.FileName); private void 退出XToolStripMenuItem_Click(object sender, EventArgs e) this.Close(); private void 剪切ToolStripMenuItem_Click(object sender, EventArgs e) richTextBox1.Cut(); private

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论