实验六 windows编程

更新时间:2024-04-19 07:43:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

贺州学院

C#程序设计与开发实战实验报告

班级: 实验名称: 一、实验目的

1.掌握窗体的常用属性和方法的使用。

2. 掌握文本操作类控件中的标签控件和文本控件的使用。

3.掌握选择操作类控件中的复选框、单选框、列表框、组合框的使用。 二、实验内容

1. 试编写Windows应用程序,完成下列要求:

(1)Form1(登陆窗口)和Form2窗体设计界面如下:注意Form1窗口的外形设置。

14软件2 姓名: 学号 完成时间 2016/5/31 实验六 Windows编程

(2)应用程序从Form1启动,输入用户名和密码,要求:密码框以字符“#”代替用户输入显示;

(3)当用户单击Form1中的“登陆”按钮时,弹出Form2窗体,并将用户输入的用户名和密码传递到Form2的只读textBox中显示;

(4)当用户单击Form2中的“返回”按钮时,关闭Form2窗体,并将Form1窗体中的两个textBox清空;

(5)当单击Form1的取消时,结束整个程序的运行。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test1 {

public partial class Form1 : Form

{

public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//登录按钮

private void button1_Click(object sender, EventArgs e) {

if (textBox1.Text == \ {

MessageBox.Show(\请输入用户名!\提示信息\

return; }

if (textBox2.Text == \ {

MessageBox.Show(\请输入密码!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

return; }

Form2 f2 = new Form2(); //定义from2窗口的对象 f2.user = textBox1.Text; //记住当前用户名 f2.pass = textBox2.Text; //记住当前密码 f2.Parent = this;

this.Hide(); //隐藏父窗口

f2.Show(); //显示子窗口 }

//取消按钮

private void button2_Click(object sender, EventArgs e) {

if (DialogResult.Yes == MessageBox.Show(\是否退出?\\提示信息\MessageBoxButtons.YesNo))

{ this.Close(); } }

//窗口活动时执行的代码

private void Form1_Activated(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox1.Focus();

}

private void Form1_Load(object sender, EventArgs e) {

} } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test1 {

public partial class Form2 : Form {

public Form1 Parent; //父窗口 public String user, pass; //用户名、密码 public Form2() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//返回按钮

private void button1_Click(object sender, EventArgs e) {

if (DialogResult.Yes == MessageBox.Show(\是否返回?\\提示信息\MessageBoxButtons.YesNo))

{

this.Parent.Show(); //显示父窗口 this.Close(); //关闭子窗口 } }

//加载窗口

private void Form2_Load(object sender, EventArgs e) {

textBox1.Text = user; //显示用户名 textBox2.Text = pass; //显示密码

}

//关闭窗口

private void Form2_FormClosed(object sender, FormClosedEventArgs e) {

this.Parent.Show(); //显示父窗口 } } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms; namespace Windows1 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

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

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

private void button1_Click(object sender, EventArgs e) {

Form2 frm2 = new Form2(); //实例化Form2 frm2.user = textBox1.Text; frm2.password = textBox2.Text; frm2.myparent = this; this.Hide();

if (textBox1.Text == \ frm2.Show(); //调用Show方法显示Form2窗体 else {

MessageBox.Show(\密码或者用户名不正确,请重新输入\ textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); } }

private void Form1_Load(object sender, EventArgs e) {

textBox2.MaxLength = 6;

textBox2.PasswordChar = '#'; }

private void button2_Click(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); }

private void Form1_Activated(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ } } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms; namespace Windows1 {

public partial class Form2 : Form {

public string user, password; public Form myparent; public Form2() {

InitializeComponent(); }

private void Form2_Load(object sender, EventArgs e)

{

textBox1.Text = user; textBox2.Text = password; }

private void button1_Click(object sender, EventArgs e) {

this .Close(); this .myparent.Show(); }

private void Form2_FormClosed(object sender, FormClosedEventArgs e) {

this.myparent.Show(); } } }

2. 试编写Windows应用程序,完成下列要求:

(1)Form2(登陆窗口)和Form1(我的应用程序)窗体设计界面如下。注意Form2窗口的外形设置,另外主方法中的Application.Run(new Form1());不能修改。

(2)运行应用程序时弹出登录窗口,输入用户名和密码,要求:密码框以字符“*”代替用户输入显示(注意登录窗体要求显示在屏幕中间);

(3)当用户单击Form2中的“登陆”按钮时,如果用户名和密码都正确,就关闭登录窗口并弹出Form1窗体,并将用户输入的用户名和密码传递到Form1的richTextBox1分行显示;如果密码不正确,则弹出消息框显示错误信息,并提示重新输入;

(4)当用户单击“取消”按钮时,结束整个程序的运行; (5)当用户关闭Form1时,结束整个程序的运行。 using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms; namespace Test2 {

public partial class Form2 : Form {

public Form2() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//登录按钮

private void button1_Click(object sender, EventArgs e) {

if (textBox1.Text == \ {

MessageBox.Show(\请输入用户名!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

return; }

if (textBox2.Text == \ { MessageBox.Show(\请输入密码!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

return; }

//判断用户名和密码是否正确

if (textBox1.Text == \ {

Form1.user = textBox1.Text; Form1.pass = textBox2.Text;

this.Close(); //关闭登录窗口 } else {

MessageBox.Show(\用户名或密码输入不正确,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

textBox1.Text = \ textBox2.Text = \ textBox1.Focus(); }

}

private void button2_Click(object sender, EventArgs e) {

if (DialogResult.Yes == MessageBox.Show(\是否退出?\\提示信息\MessageBoxButtons.YesNo))

{

Application.Exit(); //退出整个应用程序

} }

private void Form2_Load(object sender, EventArgs e) {

} } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test2 {

public partial class Form1 : Form {

public static string user, pass; //显示用户名、密码 public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//加载窗口

private void Form1_Load(object sender, EventArgs e) {

richTextBox1.AppendText(Form1.user+\ //显示用户名和密码

} } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;

using System.Text;

using System.Threading.Tasks; using System.Windows.Forms; namespace Windows2 {

public partial class Form2 : Form {

public Form2() {

InitializeComponent(); }

private void button1_Click(object sender, EventArgs e) {

Form1 frm1 = new Form1(); //实例化Form1 Form2 frm2 = new Form2(); //实例化Form2 frm1.test1 = textBox1.Text; frm1.test2 = textBox2.Text; frm1.myparent = this; this.Hide();

if (textBox1.Text == \this.Close(); else {

MessageBox.Show(\密码或者用户名不正确,请重新输入\ textBox1.Text = \ textBox2.Text = \

frm2.Show(); //调用Show方法显示Form2窗体 textBox1.Focus(); this.Close(); } }

private void Form2_Load(object sender, EventArgs e) {

textBox1.MaxLength = 6;

textBox2.PasswordChar = '*'; }

private void button2_Click(object sender, EventArgs e) { this.Close();

}

private void Form2_FormClosed(object sender, FormClosedEventArgs e) { this.Close();

} } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms; namespace Windows2 {

public partial class Form1 : Form {

public string test1,test2; public Form myparent; public Form1() {

InitializeComponent(); }

private void Form1_FormClosed(object sender, FormClosedEventArgs e) {

//this.Close();

//this.myparent.Show(); }

private void Form1_Load(object sender, EventArgs e) {

richTextBox1.Text = test1; richTextBox1.Text = test2; } } }

3. 试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下:

(2)单击“确定”按钮,可将textBox中的文字添加到label2中; (3)label2中的文字可字体大小可切换三次(小、中、大),初始状态下字体为“小”,则“缩小字体”按钮不可用,每次单击“增大字体”按钮,可使增大字体一号,当字体增大为“大”时,“增大字体”按钮不可用,每次单击“缩小字体”按钮,可使字体缩小一号;

(4)“变换字体颜色”按钮,可使label2中的字体颜色随机改变。 using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms; namespace Test3 {

public partial class Form1 : Form {

public int Add = 0; //增大字体标志:0代表小,1代表中,2代表大 public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//确定按钮

private void button1_Click(object sender, EventArgs e) {

if (textBox1.Text == \ {

MessageBox.Show(\请输入文字!\提示信息\MessageBoxIcon.Warning);

return; }

if (Add == 0) {

button3.Enabled = false; //设置缩小字体不可用 }

richTextBox1.Text = textBox1.Text; //显示文字 textBox1.Text = \清空文字

textBox1.Focus(); //定位输入焦点 }

//增大字体按钮

private void button2_Click(object sender, EventArgs e) {

richTextBox1.SelectAll(); //全选文字

//richTextBox1.Select(0, 0); //取消全选文字 if (richTextBox1.Text == \ {

MessageBox.Show(\当前文本框无文字\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

return; }

richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size + 1, richTextBox1.SelectionFont.Style);

Add++; if (Add !=0) {

button3.Enabled = true; //设置缩小字体可用 }

if (Add == 2) {

button2.Enabled = false; //设置增大字体不可用 } }

//缩小字体按钮

private void button3_Click(object sender, EventArgs e) {

richTextBox1.SelectAll(); //全选文字 richTextBox1.SelectionFont = Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size richTextBox1.SelectionFont.Style);

Add--;

if (Add == 0) {

button3.Enabled = false; //设置缩小字体不可用 }

if (Add < 2) {

button2.Enabled = true; //设置增大字体可用 } }

//变换字体颜色按钮

private void button4_Click(object sender, EventArgs e) {

richTextBox1.SelectAll(); //全选文字 //richTextBox1.SelectionColor = Color.Red;

//设置字体颜色:GetRandomColor()方法是为了获取随机颜色 richTextBox1.SelectionColor = GetRandomColor();

new -1,

}

//加载窗口

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

//获取随机颜色

public Color GetRandomColor() {

Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // C#的随机数

System.Threading.Thread.Sleep(RandomNum_First.Next(50));

Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks); // 为了在白色背景上显示,尽量生成深色 int int_Red = RandomNum_First.Next(256);

int int_Green = RandomNum_Sencond.Next(256);

int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green; int_Blue = (int_Blue > 255) ? 255 : int_Blue;

return Color.FromArgb(int_Red, int_Green, int_Blue); } } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms;

namespace WindowsFormsApplication3 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

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

private void button1_Click(object sender, EventArgs e) {

textBox2.Text = textBox1.Text; }

public inti = 1;

private void button2_Click(object sender, EventArgs e) {

if (i< 3) {

textBox2.Font = new Font(\宋体\i++;

} }

private void button3_Click(object sender, EventArgs e) {

if (i> 1) {

textBox2.Font = new Font(\宋体\i--;

} }

private void button4_Click(object sender, EventArgs e) {

textBox2.ForeColor = Color.Red; } } }

4.试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下:

(2)窗体左侧为一个靠左停靠的panel,其中包含一个label控件; (3)初试状态时,“水平移动”选中,当用户单击“开始移动”按钮时,label在panel中水平从左向右移动,单击“暂停移动”按钮时,label停在原位置不动;

(4)在label移动过程中,若用户切换移动方式,则弹出对话框,提示先暂停移动;在label暂停移动时,用户切换移动方式,label在原位置以新的移动方式进行移动。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data;

using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test4 {

public partial class Form1 : Form {

int y; //标志位 public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 y = 1;

InitializeComponent(); }

//开始移动按钮

private void button1_Click(object sender, EventArgs e) {

timer1.Enabled = true; //设置定时器可用

timer1.Interval = 200; //设置定时器间隔为500毫秒

}

//暂停移动按钮

private void button2_Click(object sender, EventArgs e) {

timer1.Enabled = false; //设置定时为不可用 }

//加载窗口

private void Form1_Load(object sender, EventArgs e) {

radioButton1.Enabled = true; }

//定时器

private void timer1_Tick(object sender, EventArgs e) {

//水平移动

if (radioButton1.Checked) {

//注意:laber1.Right属性不可改变,只能读。所以改变laber1的位置只能用top和left属性

label1.Left = label1.Left + 10*y ; if (label1.Right>panel1.Size.Width) {

y = -1; }

if (label1.Left < 5) {

y = 1; } }

//垂直移动

if (radioButton2.Checked) {

label1.Top = label1.Top + 10 * y;

if (label1.Top > panel1.Size.Height-20) {

y = -1; }

if (label1.Bottom < 20) {

y = 1; } } }

//鼠标单击\水平移动\控件时发生

private void radioButton1_MouseClick(object sender, MouseEventArgs e) {

if (timer1.Enabled == true) {

timer1.Enabled = false;

MessageBox.Show(\请先暂停移动,再选择移动方式!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

radioButton2.Checked = true; }

timer1.Enabled = true; }

//鼠标单击\垂直移动\控件时发生

private void radioButton2_MouseClick(object sender, MouseEventArgs e) {

if (timer1.Enabled == true) {

timer1.Enabled = false;

MessageBox.Show(\请先暂停移动,再选择移动方式!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

radioButton1.Checked = true; }

timer1.Enabled = true;

} } }

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Threading.Tasks; using System.Windows.Forms;

namespace WindowsFormsApplication3 {

public partial class Form1 : Form {

public int k = 1; public Form1() {

InitializeComponent(); } public inti = 1;

private void button2_Click(object sender, EventArgs e) {

timer1.Enabled = false; timer2.Enabled = false; }

private void button1_Click_1(object sender, EventArgs e) {

if (k == 1) {

timer1.Enabled = true; } else

timer2.Enabled = true; }

private void radioButton1_CheckedChanged_1(object sender, EventArgs e) {

k = 1;

timer2.Enabled = false; timer1.Enabled = true; }

private void radioButton2_CheckedChanged(object sender, EventArgs e) {

k = 2;

timer2.Enabled = true; timer1.Enabled = false; }

private void timer1_Tick(object sender, EventArgs e) {

if (i == 0) {

if (label1.Left > 2)

label1.Left = label1.Left - 2; else i = 1;

} else {

if (label1.Left < panel1.Width - label1.Width) label1.Left = label1.Left + 2; else i = 0;

} }

private void timer2_Tick(object sender, EventArgs e) {

if (i == 0) {

if (label1.Top > 2)

label1.Top = label1.Top - 2; else i = 1;

} else {

if (label1.Top < panel1.Height - label1.Height) label1.Top = label1.Top + 2; else i = 0;

} } } }

5. 试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下:

(2)运算类型的下列列表中包括:加法、减法、乘法、除法、取模共5种操作; 当用户未选定运算类型时,下面的控件均不可见;当用户选定一种运算类型后,下面的控件可用,其中加号“+”应自动更改为相应的运算符;

(3)当用户在前两个文本框中输入时,最后得到结果的文本框自动显示运算结果,注意该文本框是只读的,用户不能更改其值;

(4)使用过程中,用户修改运算类型时,三个文本框的内容自动清空;

(5)注意第一个和第二个文本框如果输入的不是数字时,要有异常处理检查,并自动清空里面的内容然后继续输入。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms; namespace Test5 {

public partial class Form1 : Form {

double a, b, c; public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//控件改变索引值

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \

panel1.Visible = true; //使计算控件可见 int m = comboBox1.SelectedIndex; //获取当前选择的索引 switch (m) {

//加法 case 0:

label2.Text = \显示加法运算符 break; case 1:

label2.Text = \显示减法运算符 break; case 2:

label2.Text = \显示乘法运算符 break; case 3:

label2.Text = \显示除法运算符 break; case 4:

label2.Text = \显示取模运算符 break; } }

//计算方法

public void compute() {

int m = comboBox1.SelectedIndex; //获取当前选择的索引 switch (m) {

//加法 case 0:

c = a + b;

textBox3.Text = c.ToString(); //显示运算结果 break; //减法 case 1:

c = a - b;

textBox3.Text = c.ToString(); //显示运算结果 break; //乘法 case 2:

c = a * b;

textBox3.Text = c.ToString(); //显示运算结果 break; //除法 case 3:

c = a / b;

textBox3.Text = c.ToString(); //显示运算结果 break;

//取模 case 4:

c = a % b;

textBox3.Text = c.ToString(); //显示运算结果 break; } }

//输入第一个数字

private void textBox1_TextChanged(object sender, EventArgs e) {

if (textBox1.Text != \ {

if (double.TryParse(textBox1.Text, out a) == false) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \MessageBox.Show(\输入的不是数字,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

textBox1.Focus(); return; } }

if (textBox2.Text != \ {

compute(); //调用计算方法 } }

//输入第二个数字

private void textBox2_TextChanged(object sender, EventArgs e) {

if (textBox2.Text != \ {

if (double.TryParse(textBox2.Text, out b) == false) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \MessageBox.Show(\输入的不是数字,请重新输入!\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);

textBox2.Focus(); }

else if (textBox1.Text != \ {

int m = comboBox1.SelectedIndex; //获取当前选择的索引 //判断除法时除数是否为0 if (m == 3) {

if (b == 0) {

MessageBox.Show(\除数不能为0,请重新输入!\提示信息\MessageBoxIcon.Warning);

textBox2.Text = \ textBox2.Focus(); return; } }

//判断取模时模数是否为0 if (m == 4) {

if (b == 0) {

MessageBox.Show(\模数不能为0,请重新输入!\提示信息\MessageBoxIcon.Warning);

textBox2.Text = \ textBox2.Focus(); return; } }

compute(); //调用计算方法 } } }

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

6. 试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下:

(2)运算类型的下列列表中包括:加法、减法、乘法、除法、取模共5种操作;初始状态下,选择“加法”运算,当用户更改运算类型时,下面式子中的加号“+”应自动更改为相应的运算符;

(3)当用户在前两个文本框中输入时,最后得到结果的文本框始终是空白状态,注意该文本框是只读的,用户不能更改其值;只有当用户单击确定按钮时,结果文本框中才会显示正确的计算结果;

(4)使用过程中,用户修改运算类型时,三个文本框的内容自动清空;

(5)注意第一个和第二个文本框如果输入的不是数字时,要有异常处理检查,并自动清空里面的内容然后继续输入。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

namespace Test6 {

public partial class Form1 : Form {

double a, b, c; public Form1() {

InitializeComponent(); }

//输入第一个数字

private void textBox1_TextChanged(object sender, EventArgs e) {

if (textBox1.Text != \ {

if (double.TryParse(textBox1.Text, out a) == false) {

textBox1.Text = \

MessageBox.Show(\输入的不是数字,请重新输入!\提示信息\

textBox1.Focus(); return; } } }

//输入第二个数字

private void textBox2_TextChanged(object sender, EventArgs e)

{

if (textBox2.Text != \ {

if (double.TryParse(textBox2.Text, out b) == false) {

textBox2.Text = \

MessageBox.Show(\输入的不是数字,请重新输入!\提示信息\

textBox2.Focus(); return; }

if (radioButton4.Checked) {

if (b == 0) {

MessageBox.Show(\除数不能为0,请重新输入!\\提示信息\

textBox2.Text = \ textBox2.Focus(); return; } }

if (radioButton5.Checked) {

if (b == 0) {

MessageBox.Show(\模数不能为0,请重新输入!\\提示信息\

textBox2.Text = \ textBox2.Focus(); return; } } } }

//确定按钮

private void button1_Click(object sender, EventArgs e) {

if (radioButton1.Checked) {

c = a + b;

textBox3.Text = c.ToString(); }

else if (radioButton2.Checked)

{

c = a - b;

textBox3.Text = c.ToString(); }

else if (radioButton3.Checked) {

c = a * b;

textBox3.Text = c.ToString(); }

else if (radioButton4.Checked) {

c = a / b;

textBox3.Text = c.ToString(); }

else if (radioButton5.Checked) {

c = a % b;

textBox3.Text = c.ToString(); } }

//显示加法运算符

private void radioButton1_MouseClick(object sender, MouseEventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示减法运算符

private void radioButton2_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示乘法运算符

private void radioButton3_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示除法运算符

private void radioButton4_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

//显示取模运算符

private void radioButton5_CheckedChanged(object sender, EventArgs e) {

textBox1.Text = \ textBox2.Text = \ textBox3.Text = \ label3.Text = \ }

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

7.试编写Windows应用程序,完成下列要求:

(1)Form1窗体设计界面如下,该程序功能:数组生成处理器。

(2)程序运行时,窗体中仅“生成数组”按钮可用,用户单击该按钮后,随机生成10个20以内的整数并显示;同时,“排序”按钮可用;

(3)用户单击“排序”按钮后,将上面生成的数组按从小到大的顺序排列显示;同时,下面的“插入”相关控件可用;

(4)用户在文本框中输入一个20以内的整数,单击“插入”按钮,即可将该数插入到数组的合适位置并显示。

using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;

using System.Windows.Forms;

//添加集合命名空间 using System.Collections;

namespace Test7 {

public partial class Form1 : Form {

ArrayList list = new ArrayList(); //定义一个数组列表 public Form1() {

this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }

//生成数组按钮

private void button1_Click(object sender, EventArgs e) {

Random r = new Random(); //定义一个随机数生成类的对象

label2.Text = \list.Clear();

for (inti = 0; i< 10; i++) {

int m = Math.Abs(r.Next(20)); //生成一个20以内的随机数 list.Add(m); //添加y元素到集合中

label2.Text += m.ToString() + \ //显示集合元素 }

button2.Enabled = true; //使排序按钮可用 }

//排序按钮

private void button2_Click(object sender, EventArgs e) { list.Sort();

label4.Text = \

//用foreach遍历集合 foreach (inti in list) {

label4.Text += i.ToString() + \ //显示集合元素 }

button3.Enabled = true; //使插入按钮可用 }

//插入按钮

private void button3_Click(object sender, EventArgs e) {

if (textBox1.Text == \

{

MessageBox.Show(\未输入插入的数!\\错误信息\MessageBoxButtons.OK, MessageBoxIcon.Error);

return; } int m;

if (int.TryParse(textBox1.Text, out m) == false) { MessageBox.Show(\输入的不是数字,请重新输入!\错误信息\

textBox1.Text = \ textBox1.Focus(); return; }

list.Add(m); //将元素插入集合 list.Sort(); //重新排序 label7.Text = \ //用foreach遍历集合 foreach (inti in list) {

label7.Text += i.ToString() + \ //显示集合元素 } }

private void Form1_Load(object sender, EventArgs e) {

}

} }

本文来源:https://www.bwwdw.com/article/3tfp.html

Top