//显示除法运算符
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) {
}
} }