第五章 函数
description
Transcript of 第五章 函数
![Page 1: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/1.jpg)
C# 程序设计教程 本书目录 1
第五章 用户界面设计
本章目录
![Page 2: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/2.jpg)
C# 程序设计教程 本书目录
5.1 用户界面概述
2
例5.1 记事本程序 演示
主菜单
菜单项下拉式菜单MenuStrip
![Page 3: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/3.jpg)
C# 程序设计教程 本书目录 3
弹出式菜单ContextMenuStrip
![Page 4: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/4.jpg)
C# 程序设计教程 本书目录 4
通用对话框
![Page 5: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/5.jpg)
C# 程序设计教程 本书目录 5
自定义对话框
![Page 6: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/6.jpg)
C# 程序设计教程 本书目录 6
工具栏
![Page 7: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/7.jpg)
C# 程序设计教程 本书目录
界面设计预定义类
7
本章目录
System.Windows.Forms 命名空间界面元素 控件 说明
菜单 MenuStripContextMenuStrip
下拉式菜单弹出式菜单
通用对话框OpenFileDialogSaveFileDialogFontDialogColorDialog
打开文件对话框保存文件对话框字体对话框颜色对话框
工具栏 ToolStrip 工具栏
![Page 8: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/8.jpg)
C# 程序设计教程 本书目录
5.2 菜单设计
8
MenuStrip
下拉式菜单
本章目录
ContextMenuStrip
弹出式菜单
5.2.1 菜单设计概述
![Page 9: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/9.jpg)
C# 程序设计教程 本书目录
菜单项的设置
9
热键 & 热键字符 输入:新建 (&N)
显示:新建 (N) N 为热键
本章目录
重要属性(1)Text
设置菜单项文本
(2) ShortcutKeys
设置快捷键
重要事件
事件 Click 为菜单项编写程序
仅在当前控件中起作用 ( 如Alt+F 打开”文件”菜单后,直接按 N 键”新建” )
在当前活动的程序中起作用;特殊:系统级快捷键, Ctrl+C ;Alt+Tab;Wins+U
![Page 10: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/10.jpg)
C# 程序设计教程 本书目录 10
例5.2 设计例 5.1 的菜单项
本章目录
5.2.2 创建下拉式菜单
菜单显示 快捷键 菜单显示 快捷键 菜单显示
菜单显示
文件 (F) 编辑 (E) 格式 (O) 帮助 (H)
新建 (N) Ctrl+N 剪切 (T) Ctrl+X 字体 (F) 关于 (A)…
打开 (O)… Ctrl+O 复制 (C) Ctrl+C 颜色 (C) 更多帮助另存为 (A)… Ctrl+S 粘贴 (P) Ctrl+V
—— 分隔线
退出 (X)
在标题栏输入 - (减号);或在菜单项上右击鼠标,”插入 -Separator”
下拉式菜单的建立过程:1) 在窗体上放置一个 MenuStrip 控件2) 设计菜单 Text3) 设置菜单项的属性 ShortcutKeys4) 编写菜单项的事件过程 Click
private void
FileNew_Click(…)
{//“ 新建”菜单项的事件过程 textBox1.Text = "";
textBox1.Focus();
}
private void FileExit_Click(…)
{//“ 退出”菜单项的事件过程 Application.Exit();
}
![Page 11: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/11.jpg)
C# 程序设计教程 本书目录 11
string s;
private void EditCut_Click(…) // 剪切事件{
s = textBox1.SelectedText ;// 将选中的内容存放到变量 s
中 textBox1. SelectedText = “”;// 将选中的内容清除实现剪切}private void EditCopy_Click(…) // 复制事件{
s = textBox1. SelectedText ;
}
private void EditPaste_Click(…) // 粘贴事件{
textBox1. SelectedText = s;
// 将 s 中的内容赋值给光标所在的 SelectedText ,实现粘贴}
![Page 12: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/12.jpg)
C# 程序设计教程 本书目录 12本章目
录
5.2.3 创建弹出式菜单
private void FileNew_Click(…){// “ 新建 (N)” 菜单项的事件过程
textBox1.Text = "";textBox1.Focus();
}
例5.3 设计例 5.1 的弹出式菜单弹出式菜单的建立过程:1)在窗体上放置一个 ContextMenuStrip 控件2)设置菜单项的属性 Text 、 ShorcutKeys
3)建立弹出式菜单与文本框之间的关联
4)编写 ( 或共享 ) 菜单项的事件过程 Click
选定 textBox1 文本框,将其 ContextMenuStrip
属性设置为contextMenuStrip1 。
![Page 13: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/13.jpg)
C# 程序设计教程 本书目录
5.3 对话框设计
13
本章目录
通用对话框 自定义对话框
![Page 14: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/14.jpg)
C# 程序设计教程 本书目录
通用对话框控件
颜色对话框目录浏览对话框字体对话框打开文件对话框保存文件对话框打印页面设置对话框打印预览对话框
14
![Page 15: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/15.jpg)
C# 程序设计教程 本书目录
5.3.1 通用对话框
15
创建通用对话框 : 将控件拖到窗体上
本章目录
![Page 16: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/16.jpg)
C# 程序设计教程 本书目录
通用对话框的使用
– 例如: openFileDialog1.ShowDialog();
16
DialogResult.OK
DialogResult.Cancel
弹出对话框: ShowDialog 方法
返回对话框的选择结果
功能:信息交互,是输入输出的界面,但实际的功能需要编程实现。
![Page 17: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/17.jpg)
C# 程序设计教程 本书目录
1. 打开文件对话框 OpenFileDialog 控件
重要属性 :
(1) FileName
设置初始打开的文件名,也可返回从对话框当前文件列表中选择的文件名。包含了文件路径。
(2) Filter
确定所显示文件的类型 , 例如:
Doc Files(*.docx)|*.docx
|Text Files(*.txt)|*.txt| 所有文件 |
*.*
显示 word 文档、文本文件和所有文件
(4) InitialDirectory
设置文件对话框的初始目录
(5) Title
获取或设置对话框的标题
(3) FilterIndex
设置或获取文件过滤器索引
openFileDialog1.FilterIndex = 3;openFileDialog1.FileName = “123";
openFileDialog1.Filter = "Doc Files|*.docx|Text Files|*.txt|All Files|*.*";
openFileDialog1.InitialDirectory = @"c:\";
![Page 18: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/18.jpg)
C# 程序设计教程 本书目录
例5.5 为引例中的“打开”菜单项编写事件过程。
18
private void FileOpen_Click(…)
{
openFileDialog1.FileName = “123";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "Doc Files|*.docx|Text Files|*.txt|All Files|*.*";
openFileDialog1.FilterIndex = 3;
openFileDialog1.ShowDialog(); // 显示对话框 if (dr == DialogResult.OK) {
StreamReader sr = new StreamReader(openFileDialog1.FileName);
textBox1.Text = sr.ReadToEnd(); // 读数据到文件结束 sr.Close(); // 关闭文件 }
}本章目录
属性设置,也可在属性窗口中设置
实际文件打开功能的实现编码
![Page 19: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/19.jpg)
C# 程序设计教程 本书目录 19
例5.4 编写一个应用程序,弹出打开文件对话框, 从中选择一个图片,能在图形框中显示该图片
本章目录
可以在属性窗口中设置
openFileDialog1.FileName = "123";
openFileDialog1.InitialDirectory = "D:";
openFileDialog1.Filter = "Pictures|*.bmp;*.jpg|All Files|*.*";
openFileDialog1.FilterIndex = 1;
// 显示“打开文件”对话框,并判断是否选择文件if (openFileDialog1.ShowDialog() == DialogResult.OK)
pictureBox1.Image = Image.FromFile(openFileDialog1.FileName);
else
MessageBox.Show(" 没有选择文件 ");
![Page 20: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/20.jpg)
C# 程序设计教程 本书目录
例5.6 为引例中的“另存为”菜单项编写事件过程。
2. 保存文件对话框
20
本章目录
控件: SaveFileDialog
属性与 OpenFileDialog 控件基本相同,特有属性是 DefaultExt, 用于设置缺省的扩展名private void FileSaveAs_Click(…)
{ saveFileDialog1.FileName = “123";// 设置缺省文件名 saveFileDialog1.DefaultExt = “txt" ; // 设置缺省扩展名 saveFileDialog1.ShowDialog(); // 打开对话框 if (dr == DialogResult.OK) { StreamWriter sw = new StreamWriter(saveFileDialog1.FileName);
sw.Write(textBox1.Text); // 将文本框中的内容写入文件 sw.Close(); // 关闭文件 }
}
属性设置,也可在属性窗口中设置
实际文件保存功能的实现编码
![Page 21: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/21.jpg)
C# 程序设计教程 本书目录 21
3. 颜色对话框 控件: ColorDialog重要属性: Color
用户选定的颜色,属于 Color 结构类型
例5.7 为例 5.1 中的“颜色”菜单项编写事件过程。
private void FormatColor_Click(object sender, EventArgs e)
{
colorDialog1.ShowDialog(); // 打开颜色对话框textBox1.ForeColor = colorDialog1.Color; // 设置文件框前景颜色
}
设置文件框前景颜色本章目录
![Page 22: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/22.jpg)
C# 程序设计教程 本书目录 22
4. 字体对话框 控件: FontDialog
例5.8 为例 5.1 中的“字体”菜单项编写事件过程。 private void FormatFont_Click(object sender, EventArgs e)
{
fontDialog1.ShowColor = true; // 字体对话框上添加“颜色”选项fontDialog1.ShowDialog(); // 打开字体对话框textBox1.Font = fontDialog1.Font; // 设置文本框的字体textBox1.ForeColor = fontDialog1.Color; // 设置文件框字体颜色}
Font 获取选择的字体Color 选定的颜色
ShowColor 控制是否有“颜色”选项
![Page 23: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/23.jpg)
C# 程序设计教程 本书目录
总结
菜单设计– 下拉式菜单 MenuStrip– 弹出式菜单 ContextMenuStrip
通用对话框打开文件对话框 OpenFileDialog保存文件对话框 SaveFileDialog
自定义对话框及对话框间的数据传递?
![Page 24: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/24.jpg)
C# 程序设计教程 本书目录 24
创建自定义对话框 (1) 添加窗体: 项目|添加 Windows 窗体 注意:一个项目中不能有相同名称的窗体
本章目录
5.3.2 自定义对话框 具有特殊属性的窗体添加 Windows 窗体,自定义设置属性使用 .NET 提供的模板,如对话框、“关于”框等
本节使用第一种方法
![Page 25: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/25.jpg)
C# 程序设计教程 本书目录 25
(2) 设置属性
本章目录
![Page 26: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/26.jpg)
C# 程序设计教程 本书目录 26
例 添加窗体 Form2 设置属性编写代码显示窗体:
能否用下列语句显示 Form2 ?
本章目录
(3) 窗体的实例化和显示 只有启动窗体的实例化是自动完成的 ,
而其他窗体都需要通过代码实例化并进行显示
Form2 f2 = new Form2(); // 定义 f2 为类 Form2 的对象变量,并创建一个实例赋予 f2
f2.ShowDialog();// 显示 f2
Form2. ShowDialog();
原因: Form2 是一个类名,不是窗体名称
![Page 27: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/27.jpg)
C# 程序设计教程 本书目录
(1)Show 以非模式方法显示窗体 窗体对象 .Show(); 非模式 程序可继续执行
(2)ShowDialog 以模式方法显示窗体 窗体对象 .ShowDialog();模式 在关闭该窗体后才能对其它窗体进行操作
(3)Hide 将窗体暂时隐藏 ( 还在内存中 ) [ 窗体对象 .]Hide();
(4) Close 关闭窗体,释放内存 [ 窗体对象 .]Close();
27
注意: this 关键字代表当前窗体,如 this.Hide();
本章目录
对话框的重要方法
![Page 28: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/28.jpg)
C# 程序设计教程 本书目录 28
例 5.9 为例 5.1 设计一个如图 1.5.9 所示的对话框, 并且为“帮助|关于”编写事件过程
private void AboutDialog_Click(object sender, EventArgs e)
{
Form2 Dialog = new Form2();
Dialog.ShowDialog();
}
本章目录
![Page 29: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/29.jpg)
C# 程序设计教程 本书目录
窗体间切换
例 实现”更多帮助”菜单项的功能
29
Form1 中:主窗体切换至其它窗体
private void 更多关于ToolStripMenuItem_Click(…)
{
Form2 fr = new Form2();
fr.ShowDialog();
}
项目 | 添加 Windows 窗体
Form2 中:其它窗体切换回主窗体
private void button1_Click(…)
{
this.Hide();
//this.Close();
} 如何进行窗体间数据传递?
![Page 30: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/30.jpg)
C# 程序设计教程 本书目录 30
5.4 工具栏 控件: ToolStrip
ToolStrip 控件是一个容器, 可以放置 ToolStripBotton 、 ToolStripLabel 等对象
本章目录
例 5.10 为例 5.1 配置一个工具栏(1) 建立 ToolStrip 控件(2) 创建图标按钮 选择 Button
(3) 设置属性 Image 属性值是图标按钮中显示的图像 (4) 编写事件过程 一般与菜单项共享事件过程
![Page 31: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/31.jpg)
C# 程序设计教程 本书目录 31本章目录
private void toolStripButton1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
textBox1.Focus();
}
注意:可以在项目编辑器中设置属性 在工具栏的属性窗口选择编辑器 ToolStrip 控件上还可以放置 ToolStripLabel 等对象。 ToolStripButton 还有一个主要属性是 ToolTipText (提示)
![Page 32: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/32.jpg)
C# 程序设计教程 本书目录 32
5.5 综合应用 例 5.11 根据菜单选项,实现产生数据、修改数据以及数据统计功能。
本章目录
![Page 33: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/33.jpg)
C# 程序设计教程 本书目录
分析
① 界面设计
② 设置对象属性
③ 对象事件过程及编程
④ 程序调试和运行程序
33
三个窗体
三个窗体间切换及数据传递
![Page 34: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/34.jpg)
C# 程序设计教程 本书目录
菜单
34
主菜单
菜单项下拉式菜单MenuStrip
![Page 35: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/35.jpg)
C# 程序设计教程 本书目录
菜单设计过程
35
下拉式菜单的建立过程:1)在窗体上放置一个 MenuStrip 控件
2)设计菜单并设置菜单项的属性3)编写菜单项的事件过程
![Page 36: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/36.jpg)
C# 程序设计教程 本书目录 36
private void 随机产生 10 个数据ToolStripMenuItem_Click(…)
{
listBox1. Items.Clear ();
Random rm = new Random();
for (int i = 0; i < 10; i++)
listBox1. Items .Add (rm.Next(0, 101));
}private void 删除最小值 ToolStripMenuItem_Click(…)
{
int min=(int)listBox1 .Items [0], minx = 0;
for (int i = 1; i < listBox1.Items.Count ; i++)
if (min > (int)listBox1.Items[i])
{ min = (int)listBox1.Items[i];
minx = i; }
listBox1. Items.RemoveAt(minx) ;
}
清空列表框内容
逐个产生随机数并加入到列表框
找到列表框中的最小值的索引
移除列表框中的最小值
![Page 37: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/37.jpg)
C# 程序设计教程 本书目录
对话框
37
自定义对话框
如何能在主窗体中访问自
定义对话框中输入的数据?
36
int addi; addi=int.Parse(textBox1.Text);
//Form2.cs 中public int addi;
private void button1_Click(…)
{
addi=int.Parse(textBox1.Text); }
//Form1.cs 中Form2 fm2 = new Form2();
fm2.addi
![Page 38: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/38.jpg)
C# 程序设计教程 本书目录 38
private void 添加数据 ToolStripMenuItem_Click(…)
{
Form2 fm2 = new Form2();
if (fm2.ShowDialog(this) == DialogResult.OK)
listBox1.Items.Add( fm2.addi );
}
public int addi;
private void button1_Click(…)
{
addi=int.Parse(textBox1.Text);
}
Form1.cs 中菜单项代码
Form2.cs 中主要代码
![Page 39: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/39.jpg)
C# 程序设计教程 本书目录
对话框
39
自定义对话框
如何能从主窗体中传递
数据给自定义对话框?
最小值: 5最大值: 93平均值:
39.36//Form3.cs 中public int min, max;
public double ave;//Form1.cs 中Form3 fm3 = new Form3();
fm3.min = mini;
fm3.max = maxi;
fm3.ave = avei;
![Page 40: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/40.jpg)
C# 程序设计教程 本书目录 40
private void 统计 ToolStripMenuItem_Click(…)
{ Form3 fm3 = new Form3();
/* 此处省略求 listBox1 中的最小值 mini ,最大值 maxi, 和值sum*/
fm3.min = mini;
fm3.max = maxi;
fm3.ave = sum / listBox1.Items.Count;
fm3.ShowDialog(this);
}public int min, max;
public double ave;
private void Form3_Load(…)
{ textBox1.Text = min.ToString ();
textBox2.Text =max.ToString ();
textBox3.Text = ave.ToString ("0.00");
}
Form1.cs 中菜单项代码
Form3.cs 中主要代码
![Page 41: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/41.jpg)
C# 程序设计教程 本书目录
对话框间数据传递
41
定义公共数据成员
public 数据类型 变量名
Form1 : Form2 :public int addi;
读 fm2.addi
Form2 fm2 = new Form2();
Form3 :public int min, max;
public double ave;
Form3 fm3 = new Form3();
写 fm3.min 写 fm3.ave
与类成员变量区别?
![Page 42: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/42.jpg)
C# 程序设计教程 本书目录 42
例 5.12 多重窗体应用示例
本章目录
从 Form2读
取成绩数据 输出统计数据到 Form3
//Form1.cs 中public double
math, chemi,
eng, phy,
chin;
写入 读出并计算
![Page 43: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/43.jpg)
C# 程序设计教程 本书目录 43
Form1.cs 中主要代码
public int math, chemi, eng, phy, chin;
private void 输入成绩 _Click(…)
{
Form2 fm2 = new Form2();
fm2.ShowDialog( this );
}
Form1 fm1 = (Form1)(this.Owner) ;
fm1.math = Convert. ToDouble(textBox1.Text);
fm1.chemi = Convert. ToDouble(textBox2.Text);
fm1.eng = Convert. ToDouble(textBox3.Text);
fm1.phy = Convert. ToDouble(textBox4.Text);
fm1.chin = Convert. ToDouble(textBox5.Text);
this.Hide();
Form2.cs 中 button1_Click () 代码
![Page 44: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/44.jpg)
C# 程序设计教程 本书目录 44
Form1.cs 中主要代码
private void 计算成绩 _Click(…)
{
Form3 fm3 = new Form3();
fm3.ShowDialog( this );
}
private void Form3_Load(…)
{
Form1 fm1 = (Form1)this.Owner ;
double total=fm1.math +fm1.chemi +fm1.eng +fm1.phy +fm1.chin;
double ave = total/5;
textBox1.Text = ave.ToString ();
textBox2.Text = total.ToString ();
}
Form3.cs 中 Load() 代码
![Page 45: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/45.jpg)
C# 程序设计教程 本书目录
对话框间数据传递
45
定义公共数据成员
public 数据类型 变量名
Form1 : Form2 :
fm1.mathpublic double math,
chemi, eng, phy, chin; Form3 :fm2.ShowDialog( this );
fm3.ShowDialog( this );
Form1 fm1 = (Form1)
(this.Owner) ;fm1.chemi
Form1 fm1 = (Form1)
(this.Owner) ;
fm1.math fm1.chemi
![Page 46: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/46.jpg)
C# 程序设计教程 本书目录
另外的方法
46
修改控件的 Modifiers属性为Public
Form1 :Form2 :
sMath =
Convert.ToDouble(fm2.text
Box1.Text);
![Page 47: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/47.jpg)
C# 程序设计教程 本书目录 47
修改控件的 Modifiers属性为Public
Form1 :Form3 :
fm3.textBox1.Text =
sum.ToString ("0.00");
![Page 48: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/48.jpg)
C# 程序设计教程 本书目录 48
Form1.cs 中主要代码
double sMath, sEnglish, sChinese, sPhysics, sChemical;private void 输入成绩 _Click(……) { Form2 fm2 = new Form2(); fm2.ShowDialog(this); sMath = Convert.ToDouble( fm2.textBox1.Text ); sEnglish = Convert.ToDouble(fm2.textBox2.Text); sChinese = Convert.ToDouble(fm2.textBox3.Text); sPhysics = Convert.ToDouble(fm2.textBox4.Text); sChemical = Convert.ToDouble(fm2.textBox5.Text); }
private void button1_Click(……){ this.Close(); }
Form2.cs 中 button1_Click () 代码
![Page 49: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/49.jpg)
C# 程序设计教程 本书目录 49
Form1.cs 中主要代码
private void 计算成绩 _Click(object sender, EventArgs e){ double sum; Form3 fm3 = new Form3(); sum=sMath+sEnglish+sChinese+sPhysics+sChemical; fm3.textBox1.Text = sum.ToString ("0.00"); fm3.textBox2.Text = (sum / 5).ToString("0.00"); fm3.ShowDialog(this); }
private void button1_Click(object sender, EventArgs e){ this.Hide();}
Form3.cs 中主要代码
![Page 50: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/50.jpg)
C# 程序设计教程 本书目录
小结
菜单设计自定义窗体设计窗体间的数据交换
声明 Public全局变量设置控件的 Modifier属性为 Public
50
![Page 51: 第五章 函数](https://reader035.fdocument.pub/reader035/viewer/2022062321/56813fe4550346895daacd71/html5/thumbnails/51.jpg)
C# 程序设计教程 本书目录
实验
• 6-2. 设计菜单系统
• 6-6. 整合实验项目–将要整合的窗体在解决方案资源管理器内重命名–在现项目中“添加所有项”,选择要添加的窗体cs文件
–若两者的命名空间不同,需要添加引用
51