C#相关程序设计.docx

上传人:b****0 文档编号:18125438 上传时间:2023-08-13 格式:DOCX 页数:72 大小:29.21KB
下载 相关 举报
C#相关程序设计.docx_第1页
第1页 / 共72页
C#相关程序设计.docx_第2页
第2页 / 共72页
C#相关程序设计.docx_第3页
第3页 / 共72页
C#相关程序设计.docx_第4页
第4页 / 共72页
C#相关程序设计.docx_第5页
第5页 / 共72页
C#相关程序设计.docx_第6页
第6页 / 共72页
C#相关程序设计.docx_第7页
第7页 / 共72页
C#相关程序设计.docx_第8页
第8页 / 共72页
C#相关程序设计.docx_第9页
第9页 / 共72页
C#相关程序设计.docx_第10页
第10页 / 共72页
C#相关程序设计.docx_第11页
第11页 / 共72页
C#相关程序设计.docx_第12页
第12页 / 共72页
C#相关程序设计.docx_第13页
第13页 / 共72页
C#相关程序设计.docx_第14页
第14页 / 共72页
C#相关程序设计.docx_第15页
第15页 / 共72页
C#相关程序设计.docx_第16页
第16页 / 共72页
C#相关程序设计.docx_第17页
第17页 / 共72页
C#相关程序设计.docx_第18页
第18页 / 共72页
C#相关程序设计.docx_第19页
第19页 / 共72页
C#相关程序设计.docx_第20页
第20页 / 共72页
亲,该文档总共72页,到这儿已超出免费预览范围,如果喜欢就下载吧!
下载资源
资源描述

C#相关程序设计.docx

《C#相关程序设计.docx》由会员分享,可在线阅读,更多相关《C#相关程序设计.docx(72页珍藏版)》请在冰点文库上搜索。

C#相关程序设计.docx

C#相关程序设计

相关程序设计

一、实现一个Window窗体应用程序,可以实现画圆、计算、画五环与填充颜色的功能

代码部分

创建一个新类,名叫DrawPic.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

namespaceCase01

{

classDrawPic

{

#region属性

//字段坐标值

privateintx;

publicintX

{

get{returnx;}

set{

if(value<=0||value>=300)

x=150;

else

x=value;

}

}

privateinty;

publicintY

{

get{returny;}

set

{

if(value<100||value>130)

y=100;

else

y=value;

}

}

//字段宽高

privateintp_width;

publicintP_width

{

get{returnp_width;}

set

{

if(value<=10||value>=380)

p_width=200;

else

p_width=value;

}

}

privateintp_height;

publicintP_height

{

get{returnp_height;}

set

{

if(value<=10||value>=160)

p_height=200;

else

p_height=value;

}

}

//结构颜色

Colorc_color;

constdoublepi=3.14F;

#endregion

#region构造函数

publicDrawPic()

{

}

///

///指定坐标和宽高

///

///x坐标值

///y坐标值

///宽度

///高度

publicDrawPic(stringX,stringY,stringP_width,stringP_height)

{

this.X=int.Parse(X);

this.Y=int.Parse(Y);

this.P_width=int.Parse(P_width);

this.P_height=int.Parse(P_height);

}

///

///指定坐标和宽高构造函数重载

///

///x坐标值

///y坐标值

///宽度

///高度

///颜色

publicDrawPic(stringX,stringY,stringP_width,stringP_height,Colorcolors)

{

this.X=int.Parse(X);

this.Y=int.Parse(Y);

this.P_width=int.Parse(P_width);

this.P_height=int.Parse(P_height);

this.c_color=colors;

}

#endregion

///

///实现画圆

///

///所需画圆的窗体

publicvoidDrawCircle(System.Windows.Forms.Formf)

{

//在窗体上绘制一个画图图面(画布)

Graphicsg=f.CreateGraphics();

//创造一个画笔指定画笔颜色及画笔宽度

Penpen=newPen(Color.Black,3);

//指定画出的图形质量使用消除锯齿

g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

//使用画笔画圆

g.DrawEllipse(pen,newRectangle(x,y,p_width,p_height));

//释放画布

g.Dispose();

//释放画笔

pen.Dispose();

}

publicvoidFillColor(System.Windows.Forms.Formf)

{

Graphicsg=f.CreateGraphics();

Penpen=newPen(Color.Black,3);

g.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

g.InterpolationMode=System.Drawing.Drawing2D.InterpolationMode.High;

//定义一个使用颜色的填充对象

Brushb=newSolidBrush(c_color);

//填充一个形状在固定的坐标上

g.FillEllipse(b,x+1.5F,y+1.5F,p_height-3,p_width-3);

g.Dispose();

pen.Dispose();

}

publicstringgetResult(intd)

{

doublec=pi*d;

doubles=pi*((d/2)*(d/2));

returnstring.Format("当前圆的周长是{0:

F2},面积是{1:

F2}!

",c,s);

}

}

}

二、关于属性的使用

创建一个Window窗体应用程序

例子:

学生年龄输入合法性的判断(成功实现)

1、关于Form1.cs查看代码可得:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCase02

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidbtnMsg_Click(objectsender,EventArgse)

{

studentmyStudent=newstudent();

myStudent.Age=int.Parse(txtAge.Text.Trim());

MessageBox.Show(string.Format("年龄是:

{0}",myStudent.Age.ToString()),"显示年龄",MessageBoxButtons.OK,MessageBoxIcon.Information);

//myStudent.Name="zane";

//stringgrade=myStudent.Grade;

}

privatevoidbtnNo_Click(objectsender,EventArgse)

{

MessageBox.Show("该用户的今年\t"+txtAge.Text+"\T岁","显示年龄",MessageBoxButtons.OK,MessageBoxIcon.Information);

}

}

}

2、添加类student.cs

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Linq;

usingSystem.Text;

namespaceCase02

{

classstudent

{

privateintage;

publicintAge

{

get{returnage;}

set

{

if(value>0&&value<110)

{

age=value;

}

else

{

age=18;

}

}

}

privatestringname;

///

///只读

///

publicstringName

{

get{returnname;}

}

privatestringgrade;

///

///只写

///

publicstringGrade

{

set{grade=value;}

}

}

}

三、参数的值传递(使用ref与out进行)

自定义计算税后工资

事例:

工资计税的方法为:

低于等于3500不计税,超出3500的部分按10%缴税

查看form.cs的代码加以编写

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCase03

{

publicpartialclassForm1:

Form

{

publicForm1()

{

InitializeComponent();

}

privatevoidbtnAccount_Click(objectsender,EventArgse)

{

intpay=int.Parse(txtPay.Text.Trim());

doubleresult=0.00F;

getResult(refpay,outresult);

MessageBox.Show(string.Format("您的工资总额是:

{0}\n超出起征点{1:

F2}元\n应缴纳{2:

F2}元个人所得税",txtPay.Text.Trim(),pay,result),"税率计算",MessageBoxButtons.OK,MessageBoxIcon.Information);

}

privatevoidgetResult(refintpay,outdoubleresult)

{

pay-=3500;

if(pay<1500)

{

result=0.00F;

}

elseif(pay>=1500&&pay<4500)

{

result=pay*0.1F;

}

elseif(pay>=4500&&pay<9000)

{

result=pay*0.2F;

}

elseif(pay>=9000&&pay<35000)

{

result=pay*0.25F;

}

elseif(pay>=35000&&pay<55000)

{

result=pay*0.3F;

}

elseif(pay>=55000&&pay<80000)

{

result=pay*0.35F;

}

else

{

result=pay*0.45F;

}

}

}

}

四、构造函数的使用

应先添加Window窗体,代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCase04

{

publicpartialclassMainForm:

Form

{

publicMainForm()

{

InitializeComponent();

}

privatevoidbtnExit_Click(objectsender,EventArgse)

{

this.Close();

}

privatevoidbtnTestStudentClass_Click(objectsender,EventArgse)

{

//Studentzhang=newStudent();

//zhang.Name="张靓靓";

//zhang.Age=20;

//zhang.Hobby="唱歌歌";

Studentscofield=newStudent("Scofield",28,"越狱狱");

Studentzhang=newStudent("张靓靓",20,"唱歌歌");

Studentjay=newStudent("周杰杰",21,"耍双节棍棍");

//Studentscofield=newStudent("Scofield");

scofield.SayHi();

zhang.SayHi();

jay.SayHi();

}

}

}

事例:

学生的自我介绍,代码如下:

Student.cs代码

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCase04

{

classStudent

{

publicStudent(){}

publicStudent(stringname)

{

this.Name=name;

}

publicStudent(stringname,intage,stringhobby)

{

this.Name=name;

this.Age=age;

this.Hobby=hobby;

}

///

///姓名

///

privatestringname;

publicstringName

{

get{returnname;}

set{name=value;}

}

///

///年龄

///

privateintage;

publicintAge

{

get{returnage;}

set

{

//属性是聪明的字段

if(value>0&&value<100)

{

age=value;

}

else

{

age=18;

}

}

}

///

///爱好

///

privatestringhobby;

publicstringHobby

{

get{returnhobby;}

set{hobby=value;}

}

publicvoidSayHi()

{

stringmessage;

message=string.Format(

"大家好,我是{0}同学,今年{1}岁了,我喜欢{2}。

",

name,this.age,this.hobby

);

MessageBox.Show(message);

}

}

}

五、应先添加Windows窗体(MainForm.cs查看代码并进行编辑)

使用构造函数实例化

1、StructStudent.cs的代码如下

usingSystem;

usingSystem.Collections.Generic;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCaseStruct

{

publicenumGenders

{

Male,Female

}

//结构版Student

structStructStudent

{

publicstringName;

publicGendersGender;

publicintAge;

publicstringHobby;

publicintPopularity;

publicStructStudent(stringname,Gendersgender,intage,stringhobby):

this(name,gender,age,hobby,100){}

publicStructStudent(stringname,Gendersgender,intage,stringhobby,intpopularity)

{

this.Name=name;

this.Gender=gender;

this.Age=age;

this.Hobby=hobby;

this.Popularity=popularity;

}

publicvoidSayHi()

{

stringmessage;

message=string.Format("大家好,我是{0}同学,今年{1}岁了,我喜欢{2}。

我的人气值高达{3}!

",this.Name,this.Age,this.Hobby,this.Popularity);

MessageBox.Show(message);

}

}

}

2、MainForm.cs代码如下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceCaseStruct

{

publicpartialclassMainForm:

Form

{

publicMainForm()

{

InitializeComponent();

}

privatevoidbtnExit_Click(objectsender,EventArgse)

{

this.Close();

}

privatevoidbtnTest_Click(objectsender,EventArgse)

{

//不使用new

//TestStruct();

//使用new

TestStructNew();

}

privatevoidTestStruct()

{

StructStudentmyStu;

myStu.Age=20;

myStu.Gender=Genders.Female;

myStu.Hobby="唱歌歌";

myStu.Name="张靓靓";

myStu.Popularity=100;

myStu.SayHi();

}

privatevoidTestStructNew()

{

StructStudentmyStu=newStructStudent("张靓靓",Genders.Female,20,"唱歌歌",100);

myStu.SayHi();

}

}

}

六、mySchool类

使用索引器知识点

1、MainForm.cs的代码入下:

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Text;

usingSystem.Windows.Forms;

namespaceMySchool

{

publicpartialclassMainForm:

Form

{

publicMainForm()

{

InitializeComponent();

}

privatevoidbtnExit_Click(objectsender,EventArgse)

{

this.Close();

}

//测试入口

privatevoidbtnTestStudentClass_Click(objectsender,EventArgse)

{

//TestValueReference();

TestArrayParameter();

//TestIndexer();

}

//值类型和引用类型参数演示

privatevoid

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

当前位置:首页 > 表格模板 > 书信模板

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2