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

📄 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_4
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ListBox lst;
		private System.Windows.Forms.Label lblInfo1;
		private System.Windows.Forms.Button button1;
        private Label lblInfo2;
        private Button button2;
		/// <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.lst = new System.Windows.Forms.ListBox();
            this.lblInfo1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.lblInfo2 = new System.Windows.Forms.Label();
            this.button2 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // lst
            // 
            this.lst.ItemHeight = 15;
            this.lst.Location = new System.Drawing.Point(8, 16);
            this.lst.Name = "lst";
            this.lst.Size = new System.Drawing.Size(120, 199);
            this.lst.TabIndex = 0;
            // 
            // lblInfo1
            // 
            this.lblInfo1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblInfo1.Location = new System.Drawing.Point(152, 24);
            this.lblInfo1.Name = "lblInfo1";
            this.lblInfo1.Size = new System.Drawing.Size(204, 40);
            this.lblInfo1.TabIndex = 1;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(212, 78);
            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);
            // 
            // lblInfo2
            // 
            this.lblInfo2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            this.lblInfo2.Location = new System.Drawing.Point(152, 124);
            this.lblInfo2.Name = "lblInfo2";
            this.lblInfo2.Size = new System.Drawing.Size(204, 44);
            this.lblInfo2.TabIndex = 3;
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(212, 182);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(75, 32);
            this.button2.TabIndex = 4;
            this.button2.Text = "求素数";
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(8, 18);
            this.ClientSize = new System.Drawing.Size(381, 243);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.lblInfo2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.lblInfo1);
            this.Controls.Add(this.lst);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);

		}
		#endregion

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

		private void Form1_Load(object sender, System.EventArgs e)
		{
			Random rn=new Random( );    // 声明一个随机数对象rn
			//int [ ]intA=new int[50];
			//listBox1.Items=new ArrayList(50);
			for(int i=0;i<50;i++)
			{
				//intA[i]=;
				lst.Items.Add(rn.Next(1,1001));   // 将一个1到1000的随机数添加到列表中
			}
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			long sum=0;
			for(int i=0;i<lst.Items.Count;i++)
				sum+=int.Parse(lst.Items[i].ToString( ));
			lblInfo1.Text="列表中所有数值的和为:"+sum;
		}

        private void button2_Click(object sender, EventArgs e)
        {
            //int sum = lst.Items.Count;
            for (int i = 0; i < lst.Items.Count; i++)
            {
                //lst.SelectedIndex = i;
                //MessageBox.Show("lstCount:" + lst.Items.Count);
                int k = 0;
                int a = int.Parse(lst.Items[i].ToString());// (lst.SelectedItem.ToString());
                for (int j = 2; j < a; j++)
                {
                    //MessageBox.Show("a="+a+"j="+j);
                    if (a % j == 0)
                    { k = 1; break; }
                }
                if (k == 1)
                {
                    lst.Items.RemoveAt(i);
                    //lst.SelectedIndex = i;
                    i--;
                }
            }
            lblInfo2.Text = "共有" + lst.Items.Count + "素数";// +sum;
        }
    }
}

⌨️ 快捷键说明

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