
东北大学秦皇岛分校可视化程序设计实验报告.pdf
24页可视化程序设计 实验报告 班 级: 学 号: 姓 名: 东北大学秦皇岛分校计算机与通信工程学院 一、实验步骤: 实验一 控件和窗体 1. 点击起始页创建项目或者菜单栏文件—>新建项目,在左边选择 visual C#,在右边选择 windows 窗体应用程序 2. 点击菜单 视图,打开“工具箱”,“属性”和“解决方案管理器”三个窗口 3. 在解决方案资源管理器中的解决方案名上点击右键,选择添加—>windows 窗体, 取名 Myform 4. 添加后会在资源管理器重出现 myform.cs,可以双击它打开设计页面 5. 在工具箱中拖拽一个 button 到设计页面中的myform 窗体上 6. 在属性窗口中修改Text 属性为“打开 form1 ” 7. 双击这个 button,在函数button1_Click 中输入如下语句 private void button1_Click(object sender, EventArgs e) { new Form1().Show(this); } 8. 双击资源管理器中的 programe.cs,修改main 函数如下所示static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault( false); Application.Run(new Myform()); } 9. 点击菜单栏上的按钮运行程序 二、实验任务: 做一个简单的小计算器,实现整数的加减法,如下图所示三、实验代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication3 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } float temp1 = -1;//记录第一个数字 int pos = 0; //存储计算方式 public void addNum(int num) { textBox1.Text = textBox1.Text + num.ToString(); } //7 private void button1_Click(object sender, EventArgs e) { addNum(7);//在显示屏中添加数字 } //8 private void button2_Click(object sender, EventArgs e) { addNum(8);//在显示屏中添加数字 } //9 private void button3_Click(object sender, EventArgs e) { addNum(9);//在显示屏中添加数字 } //4 private void button4_Click(object sender, EventArgs e) { addNum(4);//在显示屏中添加数字 } //5 private void button5_Click(object sender, EventArgs e) { addNum(5);//在显示屏中添加数字 } //6 private void button6_Click(object sender, EventArgs e) { addNum(6);//在显示屏中添加数字 } //1 private void button7_Click(object sender, EventArgs e) { addNum(1);//在显示屏中添加数字 } //2 private void button8_Click(object sender, EventArgs e) { addNum(2);//在显示屏中添加数字 } //3 private void button9_Click(object sender, EventArgs e) { addNum(3);//在显示屏中添加数字 } //0 private void button10_Click(object sender, EventArgs e) { addNum(0);//在显示屏中添加数字 } //除法 private void button12_Click(object sender, EventArgs e) { pos = 4;//修改计算方式标志位 temp1 = Convert.ToInt64(textBox1.Text);// 获取前一个数值 textBox1.Text = ""; } //乘法 private void button13_Click(object sender, EventArgs e) { pos = 3;//修改计算方式标志位 temp1 = Convert.ToInt64(textBox1.Text);// 获取前一个数值 textBox1.Text = ""; } //减法 private void button14_Click(object sender, EventArgs e) { pos = 2;//修改计算方式标志位 temp1 = Convert.ToInt64(textBox1.Text);// 获取前一个数值 textBox1.Text = ""; } //加法 private void button15_Click(object sender, EventArgs e) { pos = 1;//修改计算方式标志位 temp1 = Convert.ToInt64(textBox1.Text);// 获取前一个数值 textBox1.Text = ""; } //等于 private void button17_Click(object sender, EventArgs e) { float temp2 = Convert.ToInt64(textBox1.Text);// 记录第二个数字 switch (pos)//根据计算方法进行计算,显示计算结果 { case 1: textBox1.Text = (temp1 + temp2).ToString(); break; case 2: textBox1.Text = (temp1 - temp2).ToString(); break; case 3: textBox1.Text = (temp1 * temp2).ToString(); break; case 4: textBox1.Text = (temp1 / temp2).ToString(); break; } } //归零 private void button16_Click(object sender, EventArgs e) { textBox1.Text = "0";// 显示屏清空 temp1 = 0;//临时计算结果清零 pos = 0;//计算方式归零 } } } 四、实验截图: 实验任务: 实验二 目录与文件 做一个简单的记事本,有打开和保存功能 可以打开一个txt 文档,显示在文本编辑框中 可以将文本编辑框中的文字保存为一个文件 将使用的控件 RichTextBox MenuStrip 使用文件操作的相关类和方法: OpenFileDialog , SaveFileDialog StreamWriter , StreamReader , ReadAllText AppendAllText ReadAllLines WriteAllLines 二、实验代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace 可视化 02 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 保存 ToolStripMenuItem_Click(object sender, EventArgs e) { SaveFileDialog save = new SaveFileDialog(); save.Filter = " 文 本 文 件 (*.txt)|*.txt"; save.AddExtension = true; if (save.ShowDialog() == DialogResult.OK) { FileMode.Create); FileStream fileStream = new FileStream(save.FileName, StreamWriter streamWriter = new StreamWriter(fileStream); streamWriter.Write(this.richTextBox1.Text); streamWriter.Flush(); streamWriter.Close(); fileStream.Close(); } } private void 打开 ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog open = new OpenFileDialog(); open.InitialDirectory = "C:\\Documents and Settings\\Administrator\\My Documents"; open.Filter = "文本文件(*.txt)|*.txt"; open.RestoreDirectory = true; open.FilterIndex = 1; if (open.ShowDialog() == DialogResult.OK) { StreamReader read = new StreamReader(open.FileName, System.Text.Encoding.UTF8); richTextBox1.Text = read.ReadToEnd(); read.Close(); } } private void 文件 ToolStripMenuItem_Click(object sender, EventArgs e) { } private void richTextBox1_TextChanged(object sender, EventArgs e) { } private void Form1_Load(object sender, EventArgs e) { } } } 三、实验截图: 实验任务: 实验三 图形图像处理 做一个程序,可以读取一个位图并显示,通过点击上下左右按钮,可以调整图片的位置,通过点击放大缩小,可以缩放图片。
运行效果如shiyan3.exe 本实验用到了bitmap 类和 Graphics 类的 DrawImage 函数二、实验代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace shiyan4 { public partial class Form2 : Form { public Form2() { InitializeComponent(); pictureBox1.Height = 100; pictureBox1.Width = 150; } private void 获取 ToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "图片文件(*.jpg)|*.jpg"; if (ofd.ShowDialog() == DialogResult.OK) { string fname = ofd.FileName; Bitmap bitmap = new Bitmap(fname); pictureBox1.Image = bitmap; } } private void pictureBox1_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { pictureBox1.Top -= 60; } private void button2_Click(object sender, EventArgs e) { pictureBox1.Left -= 30; } private void button3_Click(object sender, EventArgs e) { pictureBox1.Height += 60; pictureBox1.Width += 60; } private void button4_Click(object sender, EventArgs e) { pictureBox1.Height -= 60; pictureBox1.Width -= 60; } private void button5_Click(object sender, EventArgs e) { pictureBox1.Left += 60; } private void button6_Click(object sender, EventArgs e) { pictureBox1.Top += 30; } private void openFileDialog1_FileOk(object sender, CancelEventArgs e) { } private void Form2_Load(object sender, EventArgs e) { } } } 三、实验截图: 实验四 数据操作 一、 实验内容: 1 . 建立项目 shiyan4 2 . 在解决方案资源管理器中,shiyan4 上点右键,选择添加—>新建项 3 . 在弹出的窗口中选择数据基于服务的数据库,点击添加,然后点击完成 4 . 在菜单栏视图中打开服务器资源管理器,在“表”上点击右键,选择添加新表 5 . 按下图新建一个表并在行前点击右键来设置主键,点击保存 并输入一个表名 6 . 在刚才新建的表上点击右键,选择显示表数据 7.在表中输入如下的数据并保存 8. 点击菜单数据显示数据源,在 database1dataset 上点击右键选择使用向导配 置数据集,选中表,点击完成 9. 点击菜单生成—>生成解决方案,会在工具箱中生成新的控件,在解决方案资源管理器中生成链接字符串 10. 在界面上添加一个 datagridview 控件和一个 button 11. 添加命名空间引用 using System.Data.SqlClient; 12. 给 form1 类添加两个变量 SqlDataAdapter adapter; DataTable table; 13. 给 Form1 添加一个 Form1_Load 事件,事件中添加代码如下 string connStr = Properties.Settings.Default.Database1ConnectionString;//连接字符串 SqlConnection conn = new SqlConnection(connStr);//建立到数据库的连接 adapter = new SqlDataAdapter("select * from Table1", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); adapter.InsertCommand = builder.GetInsertCommand(); adapter.DeleteCommand = builder.GetDeleteCommand(); adapter.UpdateCommand = builder.GetUpdateCommand(); table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table; 14. 给按钮 save 添加一个 click 事件响应函数,添加如下代码 adapter.Update(table); 二、实验任务: 做一个如 shiyan4_2.exe 的小程序,输入一个姓名,点击查询 在修改后可以保存 三、实验代码: sing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; namespace shiyan4 { public partial class Form1 : Form { SqlDataAdapter adapter; DataTable table; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { textBox1.Text = ""; dataGridView1.EndEdit(); try { adapter.Update(table); MessageBox.Show("保存成功!"); } catch (Exception ee) { MessageBox.Show(ee.Message, "保存失败!"); } } private void label1_Click(object sender, EventArgs e) { } private void textBox1_TextChanged(object sender, EventArgs e) { } private void button2_Click(object sender, EventArgs e) { string varNumber = textBox1.Text.Trim(); if (varNumber == "") { MessageBox.Show(" 请输入你要查询的姓名", " 提示信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); } try { string connStr = Properties.Settings.Default.Database1ConnectionString; SqlConnection conn = new SqlConnection(connStr); adapter = new SqlDataAdapter("select * from student where 姓名='" + varNumber + "'", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); adapter.InsertCommand = builder.GetInsertCommand(); table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table; //conn.Close(); } catch (Exception ee) { MessageBox.Show(ee.Message, " 提 示 信 息 ",MessageBoxButtons.OK,MessageBoxIcon.Warning); } } private void toolTip1_Popup(object sender, PopupEventArgs e) { } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void Form1_Load(object sender, EventArgs e) { 连接字符串连接 } string connStr = Properties.Settings.Default.Database1ConnectionString;// SqlConnection conn = new SqlConnection(connStr);//建立到数据库的 adapter = new SqlDataAdapter("select * from student", conn); SqlCommandBuilder builder = new SqlCommandBuilder(adapter); adapter.InsertCommand = builder.GetInsertCommand(); adapter.DeleteCommand = builder.GetDeleteCommand(); adapter.UpdateCommand = builder.GetUpdateCommand(); table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table; } } 四、实验截图: .Ț Database1. stude Ț ee0ll 。





![河南新冠肺炎文件-豫建科[2020]63号+豫建科〔2019〕282号](http://img.jinchutou.com/static_www/Images/s.gif)






