c#实验报告.docx

上传人:b****5 文档编号:12148274 上传时间:2023-04-17 格式:DOCX 页数:24 大小:868.15KB
下载 相关 举报
c#实验报告.docx_第1页
第1页 / 共24页
c#实验报告.docx_第2页
第2页 / 共24页
c#实验报告.docx_第3页
第3页 / 共24页
c#实验报告.docx_第4页
第4页 / 共24页
c#实验报告.docx_第5页
第5页 / 共24页
点击查看更多>>
下载资源
资源描述

c#实验报告.docx

《c#实验报告.docx》由会员分享,可在线阅读,更多相关《c#实验报告.docx(24页珍藏版)》请在冰豆网上搜索。

c#实验报告.docx

c#实验报告

可视化程序设计实验报告

院系

计算机与通信工程学院

专业名称

计算机科学与技术

班级学号

2123212

学生姓名

聂杰

提交日期

2015.5.4

成绩

 

 

东北大学秦皇岛分校

 

实验一控件和窗体

一、实验步骤:

1.点击起始页创建项目或者菜单栏文件—>新建项目,在左边选择visualC#,在右边选择windows窗体应用程序

2.点击菜单视图,打开“工具箱”,“属性”和“解决方案管理器”三个窗口。

3.在解决方案资源管理器中的解决方案名上点击右键,选择添加—>windows窗体,取名Myform

4.添加后会在资源管理器重出现myform.cs,可以双击它打开设计页面

5.在工具箱中拖拽一个button到设计页面中的myform窗体上

6.在属性窗口中修改Text属性为“打开form1”

7.双击这个button,在函数button1_Click中输入如下语句

privatevoidbutton1_Click(objectsender,EventArgse)

{

newForm1().Show(this);

}

8.双击资源管理器中的programe.cs,修改main函数如下所示

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newMyform());

}

9.点击菜单栏上的按钮运行程序

二、实验内容

做一个简单的小计算器,实现整数的加减法,如下图所示

相关代码:

namespaceCounter

{

staticclassProgram

{

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

namespaceCounter

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

floattemp1=-1;

intpos=0;

publicvoidaddnum(intnum)

{

textBox1.Text=textBox1.Text+num.ToString();

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

addnum

(1);

}

privatevoidbutton4_Click(objectsender,EventArgse)

{

addnum(4);

}

privatevoidbutton10_Click(objectsender,EventArgse)

{

pos=1;

temp1=Convert.ToInt64(textBox1.Text);

textBox1.Text="";

}

privatevoidbutton8_Click(objectsender,EventArgse)

{

addnum(8);

}

privatevoidbutton9_Click(objectsender,EventArgse)

{

addnum(9);

}

privatevoidbutton2_Click(objectsender,EventArgse)

{

addnum

(2);

}

privatevoidbutton3_Click(objectsender,EventArgse)

{

addnum(3);

}

privatevoidbutton5_Click(objectsender,EventArgse)

{

addnum(5);

}

privatevoidbutton6_Click(objectsender,EventArgse)

{

addnum(6);

}

privatevoidbutton7_Click(objectsender,EventArgse)

{

addnum(7);

}

privatevoidbutton11_Click(objectsender,EventArgse)

{

pos=2;

temp1=Convert.ToInt64(textBox1.Text);

textBox1.Text="";

}

privatevoidbutton12_Click(objectsender,EventArgse)

{

floattemp2=Convert.ToInt64(textBox1.Text);

switch(pos)

{

case1:

textBox1.Text=(temp1+temp2).ToString();

break;

case2:

textBox1.Text=(temp1-temp2).ToString();

break;

}

}

}

}

三、实验截图:

实验二目录与文件

一、实验内容

作业:

做一个简单的记事本,有打开和保存功能

可以打开一个txt文档,显示在文本编辑框中

可以将文本编辑框中的文字保存为一个文件

将使用的控件:

menuStrip、richTextBox

使用文件操作的相关类:

OpenFileDialog,SaveFileDialog,StreamWriter

二、相关代码

namespaceshiyan2

{

staticclassProgram

{

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

namespaceshiyan2

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse)

{

OpenFileDialogfbd=newOpenFileDialog();

fbd.ShowDialog();

Stringss=fbd.FileName;

FileStreamfs=newFileStream(ss,FileMode.Open);

StreamReadersr=newStreamReader(fs,Encoding.Default);

richTextBox1.Text=sr.ReadToEnd();

sr.Close();

fs.Close();

}

privatevoid保存ToolStripMenuItem_Click(objectsender,EventArgse)

{

SaveFileDialogsfd=newSaveFileDialog();

sfd.Title="保存文件";

sfd.Filter="文本文件(*.txt)|*.txt";

sfd.ShowDialog();

Stringss=sfd.FileName;

if(File.Exists(ss))

{

FileStreamfd=newFileStream(ss,FileMode.Create);

StreamWritersw=newStreamWriter(fd,Encoding.Default);

sw.WriteLine(richTextBox1.Text);

sw.Close();

fd.Close();

}

else

{

FileStreamfd=newFileStream(ss,FileMode.Create);

StreamWritersw=newStreamWriter(fd,Encoding.Default);

sw.WriteLine(richTextBox1.Text);

sw.Close();

fd.Close();

}

}

privatevoidForm1_Load(objectsender,EventArgse)

{

}

}

}

三、实验截图:

实验三图形图像处理

一,实验内容

作业:

做一个程序,可以读取一个位图并显示,通过点击上下左右按钮,可以调整图片的位置,通过点击放大缩小,可以缩放图片。

程序如shiyan3.exe

本实验用到了bitmap类和Graphics类的DrawImage函数。

二,相关代码

namespaceGraphicExamaple

{

staticclassProgram

{

///

///应用程序的主入口点。

///

[STAThread]

staticvoidMain()

{

Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newForm1());

}

}

}

namespaceGraphicExamaple

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

pictureBox1.Height=380;

pictureBox1.Width=380;

}

privatevoid打开ToolStripMenuItem_Click(objectsender,EventArgse)

{

OpenFileDialogofd=newOpenFileDialog();

ofd.InitialDirectory=@"c:

";

ofd.Filter="*.jpg|*.jpg";

if(ofd.ShowDialog()==DialogResult.OK)

{

stringfname=ofd.FileName;

Bitmapbitmap=newBitmap(fname);

pictureBox1.Image=bitmap;

}

}

privatevoidbuttonup_Click(objectsender,EventArgse)

{

pictureBox1.Top-=10;

}

privatevoidbuttonLeft_Click(objectsender,EventArgse)

{

pictureBox1.Left-=10;

}

privatevoidbuttonmax_Click(objectsender,EventArgse)

{

pictureBox1.Height+=10;

pictureBox1.Width+=10;

}

privatevoidbuttonmin_Click(objectsender,EventArgse)

{

pictureBox1.Height-=10;

pictureBox1.Width-=10;

}

privatevoidbuttonRight_Click(objectsender,EventArgse)

{

pictureBox1.Left+=10;

}

privatevoidbuttondown_Click(objectsender,EventArgse)

{

pictureBox1.Top+=10;

}

}

}

三、实验截图

实验四、数据库

1、实验目的

做一个如shiyan4_2.exe的小程序,输入一个姓名,点击查询:

在修改后可以保存:

二、实验内容

1、点击起始页创建项目或者菜单栏文件—>新建项目,在左边选择visualC#,在右边选择windows窗体应用程序,并为项目起名为shiyan4,如下图所示:

2、在解决方案资源管理器中,shiyan4上点右键,选择添加—>新建项。

在弹出的窗口中选择数据基于服务的数据库,点击添加,然后点击完成,如下图所示:

3、在菜单栏视图中打开服务器资源管理器,在“表”上点击右键,选择添加新表。

按下图新建一个表并在行前点击右键来设置主键,点击保存并输入一个表名:

4、在新建的表上点击右键,选择显示表数据,如下图所示:

5、在表中输入如下的数据并保存,如下图所示:

6、点击菜单数据显示数据源,在database1dataset上点击右键选择使用向导配置数据集,选中表,点击完成,如下图所示:

7、点击菜单生成—>生成解决方案,会在工具箱中生成新的控件,在解决方案资源管理器中生成链接字符串。

8、按要求生成用户界面:

9、添加命名空间引用

usingSystem.Data.SqlClient;

10、给form1类添加两个变量

SqlDataAdapteradapter;

DataTabletable;

11、给Form1添加一个Form1_Load事件,事件中添加代码如下:

StringconnStr=Properties.Settings.Default.MyDatabaseConnectionString;

SqlConnectionconn=newSqlConnection(connStr);

adapter=newSqlDataAdapter("select*fromMyTable",conn);

SqlCommandBuilderbuilder=newSqlCommandBuilder(adapter);

adapter.InsertCommand=builder.GetInsertCommand();

adapter.DeleteCommand=builder.GetDeleteCommand();

adapter.UpdateCommand=builder.GetUpdateCommand();

table=newDataTable();

adapter.Fill(table);

dataGridView1.DataSource=table;

12、给按钮查询添加一个click事件响应函数,添加如下代码:

stringvarNumber=textBoxNumber.Text.Trim();

if(varNumber=="")

{

MessageBox.Show("请输入你要查询的学号","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}

try

{

stringconnStr=Properties.Settings.Default.MyDatabaseConnectionString;SqlConnectionconn=newSqlConnection(connStr);

adapter=newSqlDataAdapter("select*fromMyTablewhere学号='"+varNumber+"'",conn);

qlCommandBuilderbuilder=newSqlCommandBuilder(adapter);

adapter.InsertCommand=builder.GetInsertCommand();

adapter.DeleteCommand=builder.GetDeleteCommand();

adapter.UpdateCommand=builder.GetUpdateCommand();

table=newDataTable();

adapter.Fill(table);

dataGridView1.DataSource=table;

//conn.Close();

}

catch(Exceptionee)

{

MessageBox.Show(ee.Message,"提示信息",MessageBoxButtons.OK,MessageBoxIcon.Warning);

}

13、给保存按钮添加一个click事件响应函数,添加如下代码:

textBoxNumber.Text="";

dataGridView1.EndEdit();

try

{

adapter.Update(table);

MessageBox.Show("保存成功");

}

catch(Exceptionee)

{

MessageBox.Show(ee.Message,"保存失败");

}

MyLoad();

三、实验结果

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 考试认证 > 司法考试

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1