⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 Visual C#2005程序设计教程
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WinAppArray9_3
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		int [ ,]intA={{32,61,28},{39,58,23},{29,10,69}};
		private System.Windows.Forms.Label lblOut;
		private System.Windows.Forms.Label lblAverage;
		private System.Windows.Forms.Button button1;
        private ListBox lstOut;
        private GroupBox groupBox1;
        private RadioButton radShowToLst;
        private RadioButton radSowToLbl;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
            this.lblOut = new System.Windows.Forms.Label();
            this.lblAverage = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.lstOut = new System.Windows.Forms.ListBox();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.radShowToLst = new System.Windows.Forms.RadioButton();
            this.radSowToLbl = new System.Windows.Forms.RadioButton();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();
            // 
            // lblOut
            // 
            this.lblOut.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblOut.Location = new System.Drawing.Point(16, 7);
            this.lblOut.Name = "lblOut";
            this.lblOut.Size = new System.Drawing.Size(175, 125);
            this.lblOut.TabIndex = 0;
            // 
            // lblAverage
            // 
            this.lblAverage.Location = new System.Drawing.Point(224, 16);
            this.lblAverage.Name = "lblAverage";
            this.lblAverage.Size = new System.Drawing.Size(102, 64);
            this.lblAverage.TabIndex = 1;
            this.lblAverage.Text = "矩阵元素的平均值为:";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(227, 88);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 32);
            this.button1.TabIndex = 2;
            this.button1.Text = "平均值";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // lstOut
            // 
            this.lstOut.ColumnWidth = 70;
            this.lstOut.FormattingEnabled = true;
            this.lstOut.ItemHeight = 15;
            this.lstOut.Location = new System.Drawing.Point(23, 26);
            this.lstOut.MultiColumn = true;
            this.lstOut.Name = "lstOut";
            this.lstOut.Size = new System.Drawing.Size(153, 94);
            this.lstOut.TabIndex = 3;
            this.lstOut.Visible = false;
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.radShowToLst);
            this.groupBox1.Controls.Add(this.radSowToLbl);
            this.groupBox1.Location = new System.Drawing.Point(16, 135);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(172, 51);
            this.groupBox1.TabIndex = 4;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "矩阵显示方式";
            // 
            // radShowToLst
            // 
            this.radShowToLst.AutoSize = true;
            this.radShowToLst.Location = new System.Drawing.Point(94, 24);
            this.radShowToLst.Name = "radShowToLst";
            this.radShowToLst.Size = new System.Drawing.Size(66, 19);
            this.radShowToLst.TabIndex = 7;
            this.radShowToLst.Text = "方式2";
            this.radShowToLst.UseVisualStyleBackColor = true;
            this.radShowToLst.CheckedChanged += new System.EventHandler(this.radShowToLst_CheckedChanged);
            // 
            // radSowToLbl
            // 
            this.radSowToLbl.AutoSize = true;
            this.radSowToLbl.Checked = true;
            this.radSowToLbl.Location = new System.Drawing.Point(13, 24);
            this.radSowToLbl.Name = "radSowToLbl";
            this.radSowToLbl.Size = new System.Drawing.Size(66, 19);
            this.radSowToLbl.TabIndex = 6;
            this.radSowToLbl.TabStop = true;
            this.radSowToLbl.Text = "方式1";
            this.radSowToLbl.UseVisualStyleBackColor = true;
            this.radSowToLbl.CheckedChanged += new System.EventHandler(this.radSowToLbl_CheckedChanged);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
            this.ClientSize = new System.Drawing.Size(358, 196);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.lstOut);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.lblAverage);
            this.Controls.Add(this.lblOut);
            this.Name = "Form1";
            this.Text = "求矩阵平均值";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{   // 设置标签框与列表框重合
            lstOut.Top = lblOut.Top;
            lstOut.Left = lblOut.Left;
            lstOut.Size = lblOut.Size; //lstOut.Height += 10;
            // 将数组元素值以矩阵形式输出到标签框中
            for (int i = 0; i < 3; i++)   // 外循环控制行数
                for (int j = 0; j < 3; j++)   // 内循环控制列数
                    if ((j + 1) % 3 == 0)   // 输出3个元素后换行
						lblOut.Text+=intA[i,j]+"\n\n";
					else
						lblOut.Text+=intA[i,j]+"      ";
            // 将数组元素值以矩阵形式输出到列表框中
            for (int i = 0; i < 3; i++)   // 外循环控制列数
                for (int j = 0; j < 3; j++)   // 内循环控制行数
                {
                    lstOut.Items.Add(intA[j, i]);
                    lstOut.Items.Add("");
                    //lstOut.Items.Add("");

                }
        }

		private void button1_Click(object sender, System.EventArgs e)
		{
            int sum = intA[0, 0];
			for(int i = 0;i < 3;i++)
                for(int j = 0;j < 3;j++)
                    sum += intA[i,j];
			lblAverage.Text = "矩阵元素的平均值为:" + sum/intA.Length;
            MessageBox.Show("" + intA.Length);
		}

        private void radSowToLbl_CheckedChanged(object sender, EventArgs e)
        {
            if (radSowToLbl.Checked)
            {
                lstOut.Visible = false;
                lblOut.Visible = true;
            }
        }

        private void radShowToLst_CheckedChanged(object sender, EventArgs e)
        {
            if (radShowToLst.Checked)
            {
                lstOut.Visible = true;
                lblOut.Visible = false;
            }
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -