第四版 c#程序设计教程(课后习题答案代码)

private void menu1_Click(object sender, EventArgs e) { listView1.View = View.LargeIcon; }

private void menu2_Click(object sender, EventArgs e) { listView1.View = View.SmallIcon; }

private void menu3_Click(object sender, EventArgs e) { listView1.View = View.List; }

private void menu4_Click(object sender, EventArgs e) { listView1.View = View.Tile ; }}

课本254页3

public partial class Form1 : Form { public Form1()

{ InitializeComponent();}

private void Form1_Paint(object sender, PaintEventArgs e) { Graphics gobj=this.CreateGraphics(); Pen bluePen = new Pen(Color.Blue, 5); Pen redPen = new Pen(Color.Red, 5);

Rectangle myRectangle = new Rectangle(50, 50, 100, 80); gobj.DrawRectangle(bluePen, 30, 20, 100, 80); gobj.DrawRectangle(redPen, myRectangle);

} } 课本254页4

public partial class Form2 : Form { public Form2()

{InitializeComponent();}

private void Form2_Paint(object sender, PaintEventArgs e) { Graphics gobj = this.CreateGraphics(); double PI = 3.14159;

gobj.FillPie(Brushes.Blue, 30, 30, 130, 80, 0, 300); gobj.FillPie(Brushes.Red, 30, 30, 130, 80, 0, -60); }

private void Form2_Load(object sender, EventArgs e) {} }

课本254页5

public partial class Form3 : Form { public Form3()

{ InitializeComponent(); }

private void Form3_Paint(object sender, PaintEventArgs e) {Graphics gobj = this.CreateGraphics();

HatchBrush myBrush = new HatchBrush(HatchStyle.BackwardDiagonal,Color.Blue,

Color.Green);

Pen myPen = new Pen(Color.Red, 8);

gobj.DrawEllipse(myPen, 20, 20, 150, 100); gobj.FillEllipse(myBrush, 20, 20, 150, 100); }

课本254页6

public partial class Form4 : Form { public Form4()

{ InitializeComponent(); }

private void Form4_Paint(object sender, PaintEventArgs e) { int i;

StringFormat strFormat; Font strFont; SolidBrush strBrush;

Graphics gobj = this.CreateGraphics(); for (i=10;i<=24;i+=6)

{ strFormat = new StringFormat();

strFont = new System.Drawing.Font(\隶书\, i); strBrush = new SolidBrush(Color.Red);

gobj.DrawString(\中华人民共和国\, strFont, strBrush, 2 * i,3 * i, strFormat); }}}

课本254上机实验10

public partial class Form5 : Form { public Form5()

{ InitializeComponent();}

private void button1_Click(object sender, EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Blue); Graphics gobj = this.CreateGraphics(); gobj.DrawLine(myPen, 30, 30, 120, 120); } private void button2_Click(object sender, EventArgs e) { Pen myPen = new Pen(System.Drawing.Color.Blue); Graphics gobj = this.CreateGraphics();

gobj.DrawEllipse(myPen, new Rectangle(30, 30, 100, 100)); }

private void button3_Click(object sender, EventArgs e) { Graphics gobj = this.CreateGraphics(); string drawString = \使用DrawString方法\; Font myFont = new Font(\黑体\, 16);

Brush b = new SolidBrush(System.Drawing.Color.Blue); float x = 30.0F; float y = 30.0F;

StringFormat myFormat = new StringFormat();

gobj.DrawString(drawString, myFont, b, x, y, myFormat);

} } 课本274页3

public partial class Form1 : Form { public Form1()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { StreamWriter f;

string path = \; f = new StreamWriter(path); f.WriteLine(textBox1.Text); f.Close();

} } 课本274页4

public partial class Form2 : Form { public Form2()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {string path = \; StreamReader f;

f = new StreamReader(path); textBox1.Text = f.ReadToEnd();

f.Close(); } 课本274页5

public partial class Form3 : Form { public Form3()

{ InitializeComponent();

} private void button1_Click(object sender, EventArgs e) { string path = \; FileStream f = File.OpenWrite(path); BinaryWriter sb = new BinaryWriter(f); int i;

string [] name = new string[3]{\王华\, \孙强\, \成功\}; float [] score = new float[3]{78.5f, 88.6f, 98.5f}; for (i = 0; i <= 2; i++) { sb.Write(name[i]); sb.Write(score[i]); } sb.Close(); f.Close();

} } 课本274页6

public partial class Form4 : Form { public Form4()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { string path = \;

if (!File.Exists(path)) //不存在该文件时 textBox1.Text = \指定的文件不存在\; else

{ FileStream f = File.OpenRead(path); BinaryReader sb = new BinaryReader(f); string mystr = \; string name = \; float score = 0; int i; float s = 0;

for (i = 0; i <= 2; i++) { name = sb.ReadString(); score = sb.ReadSingle(); s = s + score;

mystr = mystr + string.Format(\, name, score); }

textBox1.Text = mystr;

textBox2.Text = string.Format(\,s / 3); sb.Close(); f.Close(); } } }

课本274页上机实验11

public partial class Form5 : Form

{ string path = @\; public Form5()

{InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {if (!File.Exists(path)) //不存在该文件,则创建 { StreamWriter sw = File.CreateText(path); sw.Close(); }

if (textBox1.Text != \)

{ StreamWriter sb = new StreamWriter(path,true,Encoding.Default); sb.WriteLine(textBox1.Text); sb.WriteLine(textBox2.Text); sb.WriteLine(textBox3.Text); sb.WriteLine(textBox4.Text);

sb.WriteLine(textBox5.Text); sb.Close(); } }

private void button2_Click(object sender, EventArgs e) { int i;

string mystr = \学号\\t姓名\\t性别\\t年龄\\t分数\\r\\n\; FileStream fs = File.OpenRead(path);

StreamReader sr = new StreamReader(fs, Encoding.Default); fs.Seek(0, SeekOrigin.Begin); while (sr.Peek() > -1) { for (i = 0; i < 5; i++)

mystr = mystr + sr.ReadLine() + \; mystr = mystr + \; //\\r\\n表示回车换行} sr.Close(); fs.Close();

textBox6.Text = mystr;

}} 课本287页2

public partial class Form1 : Form

{ string[] name = new string[3]{\王华\, \陈兵\, \李强\}; float[] score = new float[3] { 98.5f, 67.4f, 88.6f }; public Form1()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { int i;

i = int.Parse(textBox1.Text); try

{ textBox2.Text = name[i];

textBox3.Text = score[i].ToString(); }

catch(Exception ex) { textBox2.Text = \; textBox3.Text = \;

MessageBox.Show(\输入的序号不正确\);

} } } 课本287页3

public partial class Form2 : Form { public class Myclass { int no; string name;

public void input(int no1, string name1) { no = no1; name = name1; } public string getstud()

//将文件流指针定位在开始位置

{ return string.Format(\, no, name); } } public Form2()

{ InitializeComponent();}

private void button1_Click(object sender, EventArgs e) { Myclass[] st = new Myclass[3]; int i;

string mystr=\; try

{ //for (i = 0; i < 3; i++) 加上本for循环就不会出错 // st[i] = new Myclass(); st[0].input(1, \王华\); st[1].input(3, \曾丽\); st[2].input(2, \陈山\); for (i = 0; i < 3; i++)

mystr = mystr + st[i].getstud() + \; textBox1.Text = mystr; }

catch (Exception ex)

{ string str = ex.Message + \ + ex.Source + \ + ex.HelpLink ; MessageBox.Show(str);

} } } 课本288上机实验12

public partial class Form3 : Form { public Form3()

{ InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) { string path = textBox1.Text; try

{ string[] dirs = Directory.GetDirectories(path); label2.Text = \子文件夹个数为:\ + dirs.Length; }

catch(Exception ex)

{ label2.Text = \不存在指定的文件夹!\; } }

private void Form3_Load(object sender, EventArgs e)

{ label2.Text = \;}}

第十三章课后题和本章例题相似,代码太长这里不写了。

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4