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

内容发布更新时间 : 2024/12/22 22:56:08星期一 下面是文章的全部内容请认真阅读。

private void rbutton1_CheckedChanged(object sender, EventArgs e) { Font f = new Font(\宋体\,textBox1.Font.Size,textBox1.Font.Style); textBox1.Font = f; }

private void rbutton2_CheckedChanged(object sender, EventArgs e)

{ Font f = new Font(\楷体_GB2312\, textBox1.Font.Size, textBox1.Font.Style); textBox1.Font = f;

//textBox1.Font.Name = \楷体_GB2312\ }

private void rbutton3_CheckedChanged(object sender, EventArgs e) { Font f = new Font(textBox1.Font.Name, 10, textBox1.Font.Style); textBox1.Font = f; }

private void rbutton4_CheckedChanged(object sender, EventArgs e) { Font f = new Font(textBox1.Font.Name, 16, textBox1.Font.Style); textBox1.Font = f; }

private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked)

{ Font f = new Font(textBox1.Font.Name, textBox1.Font.Size, FontStyle.Bold); textBox1.Font = f; } else

{ Font f = new Font(textBox1.Font.Name, textBox1.Font.Size, FontStyle.Regular); textBox1.Font = f;

} } } 课本200页 5

public partial class Form3 : Form { int a=0, b=0; public Form3()

{InitializeComponent(); }

private void Form3_Load(object sender, EventArgs e) { listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox1.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\);

listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); listBox2.Items.Add(\); }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { a = int.Parse(listBox1.Text); }

private void listBox2_SelectedIndexChanged(object sender, EventArgs e) { b = int.Parse(listBox2.Text);

listBox3.Items.Add(string.Format(\×{1}={2}\,a,b,a*b)); }

private void listBox3_SelectedIndexChanged(object sender, EventArgs e) { if (listBox3.SelectedIndex>=0)

listBox3.Items.RemoveAt(listBox3.SelectedIndex); }

} 课本200页6

public partial class Form4 : Form { public Form4()

{ InitializeComponent(); }

private void Form4_Load(object sender, EventArgs e) { comboBox1.Items.Add(\北京\); comboBox1.Items.Add(\上海\); comboBox1.Items.Add(\天津\); comboBox1.Items.Add(\广州\); comboBox1.Items.Add(\武汉\); comboBox1.Items.Add(\沈阳\); comboBox1.Items.Add(\合肥\); comboBox1.Items.Add(\长沙\); comboBox1.Items.Add(\重庆\); }

private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) { //MessageBox.Show(e.KeyChar.ToString()); if (e.KeyChar == 13) //按Enter键后判断 { if (comboBox1.Items.Contains(comboBox1.Text)) label2.Text = \你的输入已在组合框!\; else

{ comboBox1.Items.Add(comboBox1.Text);

label2.Text = \你的输入新项已添加到组合框中!\; } }}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

{ MessageBox.Show(string.Format(\你选择了'{0}'项\,comboBox1.Text)); }} 课本200页上机实验8

public partial class Form5 : Form { struct StudType { public int no; public string name; public string sex; public string sclass; public DateTime rq; }

StudType[] stud = new StudType[10]; //定义一个结构类型的数组 public Form5()

{ InitializeComponent();}

private void Form5_Load(object sender, EventArgs e) { //给定义的数组赋初值

stud[0].no = 1; stud[0].name = \王华\; stud[0].sex = \男\; stud[0].rq = new DateTime(1980,2,10); stud[0].sclass = \; stud[1].no = 2; stud[1].name = \李强\; stud[1].sex = \男\; stud[1].rq = new DateTime(1981,10,4); stud[1].sclass = \; stud[2].no = 3; stud[2].name = \张丽\; stud[2].sex = \女\; stud[2].rq = new DateTime(1980,3,2); stud[2].sclass = \; stud[3].no = 4; stud[3].name = \汪洋\; stud[3].sex = \男\; stud[3].rq = new DateTime(1980,1,5); stud[3].sclass = \; stud[4].no = 5; stud[4].name = \江华\; stud[4].sex = \男\; stud[4].rq = new DateTime(1980,3,10); stud[4].sclass = \; stud[5].no = 6; stud[5].name = \李英\; stud[5].sex = \女\; stud[5].rq = new DateTime(1980,6,2); stud[5].sclass = \; stud[6].no = 7; stud[6].name = \胡军\; stud[6].sex = \男\; stud[6].rq = new DateTime(1981,10,9); stud[6].sclass = \; stud[7].no = 8; stud[7].name = \刘驰\; stud[7].sex = \女\; stud[7].rq = new DateTime(1982,5,2); stud[7].sclass = \; stud[8].no = 9; stud[8].name = \宋仁\; stud[8].sex = \男\; stud[8].rq = new DateTime(1980,8,3); stud[8].sclass = \; stud[9].no = 10; stud[9].name = \许兵\; stud[9].sex = \男\; stud[9].rq = new DateTime(1980,11,8); stud[9].sclass = \; //将学号都添加到组合框中 for(int i = 0;i<10;i++)

comboBox1.Items.Add(stud[i].no); } private void button1_Click(object sender, EventArgs e) { if (comboBox1.Text != \)

{ int i = 0; //查找指定学号的学生记录

while (Convert.ToInt32(comboBox1.Text) != stud[i].no) i = i + 1; if (i >= 10)

MessageBox.Show(\没有该学号的记录\);

else

{ textBox1.Text = stud[i].no.ToString(); //显示找到的学生记录 textBox2.Text = stud[i].name; textBox3.Text = stud[i].sex; textBox4.Text = stud[i].sclass;

textBox5.Text = stud[i].rq.ToString();}

}}} 课本236页2

public partial class Form1 : Form {public Form1()

{InitializeComponent();}

private void menu11_Click(object sender, EventArgs e)

{ Font f = new Font(\宋体\,label1.Font.Size,label1.Font.Style); label1.Font = f; }

private void menu12_Click(object sender, EventArgs e)

{ Font f = new Font(\仿宋_GB2312\, label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu13_Click(object sender, EventArgs e)

{ Font f = new Font(\黑体\, label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu14_Click(object sender, EventArgs e)

{ Font f = new Font(\幼圆\, label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu15_Click(object sender, EventArgs e)

{ Font f = new Font(\楷体_GB2312\, label1.Font.Size, label1.Font.Style); label1.Font = f; }

private void menu21_Click(object sender, EventArgs e)

{ Font f = new Font(label1.Font.Name, 28, label1.Font.Style); label1.Font = f; }

private void menu22_Click(object sender, EventArgs e)

{Font f = new Font(label1.Font.Name, 20, label1.Font.Style); label1.Font = f; }

private void menu23_Click(object sender, EventArgs e)

{ Font f = new Font(label1.Font.Name, 16, label1.Font.Style); label1.Font = f; }

private void menu24_Click(object sender, EventArgs e)

{ Font f = new Font(label1.Font.Name, 12, label1.Font.Style); label1.Font = f; }

private void menu25_Click(object sender, EventArgs e)

{Font f = new Font(label1.Font.Name, 8, label1.Font.Style); label1.Font = f; }

}

课本237页上机实验9

public partial class Form2 : Form

{ public Form2()

{InitializeComponent();}

private void Form2_Load(object sender, EventArgs e) { treeView1.Indent = 20;

treeView1.Nodes.Add(\哺乳动物\); treeView1.Nodes[0].Nodes.Add(\豹子\); treeView1.Nodes[0].Nodes.Add(\老虎\); treeView1.Nodes[0].Nodes.Add(\北极熊\); treeView1.Nodes[0].Nodes.Add(\狼\); treeView1.Nodes[0].Nodes.Add(\大象\); treeView1.Nodes[0].Nodes.Add(\犀牛\); treeView1.Nodes.Add(\鱼类\);

treeView1.Nodes[1].Nodes.Add(\鲨鱼\); treeView1.Nodes[1].Nodes.Add(\热带鱼\); treeView1.Nodes[1].Nodes.Add(\金鱼\); treeView1.Nodes.Add(\鸟类\);

treeView1.Nodes[2].Nodes.Add(\天鹅\); treeView1.Nodes[2].Nodes.Add(\猫头鹰\); treeView1.Nodes[2].Nodes.Add(\翠鸟\); }

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {string s;

s = e.Node.Text; listView1.Items.Clear();

listView1.LargeImageList = imageList1; listView1.SmallImageList = imageList2; switch(s)

{ case \哺乳动物\:

listView1.Items.Add(\豹子\, 0); listView1.Items.Add(\老虎\, 0); listView1.Items.Add(\北极熊\, 0); listView1.Items.Add(\狼\, 0); listView1.Items.Add(\大象\, 0); listView1.Items.Add(\犀牛\, 0); break; case \鱼类\:

listView1.Items.Add(\鲨鱼\, 0); listView1.Items.Add(\热带鱼\, 0); listView1.Items.Add(\金鱼\, 0); break; case \鸟类\:

listView1.Items.Add(\天鹅\, 0); listView1.Items.Add(\猫头鹰\, 0); listView1.Items.Add(\翠鸟\, 0); break; }}

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