C#中动态创建控件及事件处理程序

  1    
  2   using System;
  3   using System.Drawing;
  4   using System.Collections;
  5   using System.ComponentModel;
  6   using System.Windows.Forms;
  7   using System.Data;
  8   
  9   namespace Miner
 10   {
 11     /// <summary>
 12       /// Summary des cription for Form1.
 13       /// </summary>
 14       public class Form1 : System.Windows.Forms.Form
 15       {
 16         private System.Windows.Forms.Panel panel1;
 17         /// <summary>
 18         /// Required designer variable.
 19         /// </summary>
 20   
 21         private Button[] n =new Button[100];
 22         private int[] kn=new int[100];
 23   
 24         //private System.ComponentModel.Container components = null;
 25         private System.ComponentModel.Container components = null;
 26   
 27         public Form1()
 28         {
 29           //
 30           // Required for Windows Form Designer support
 31           //
 32           InitializeComponent();
 33   
 34           //
 35           // TODO: Add any constructor code after InitializeComponent call
 36           //
 37         }
 38   
 39         /// <summary>
 40         /// Clean up any resources being used.
 41         /// </summary>
 42         protected override void Dispose( bool disposing )
 43         {
 44           if( disposing )
 45           {
 46             if (components != null)
 47             {
 48               components.Dispose();
 49             }
 50         }
 51           base.Dispose( disposing );
 52       }
 53   
 54       #region Windows Form Designer generated code
 55       /// <summary>
 56       /// Required method for Designer support - do not modify
 57       /// the contents of this method with the code editor.
 58       /// </summary>
 59       private void InitializeComponent()
 60       {
 61         this.panel1 = new System.Windows.Forms.Panel();
 62         this.SuspendLayout();
 63         //
 64         // panel1
 65         //
 66         this.panel1.Location = new System.Drawing.Point(8, 8);
 67         this.panel1.Name = "panel1";
 68         this.panel1.Size = new System.Drawing.Size(400, 400);
 69         this.panel1.TabIndex = 0;
 70         //
 71         // Form1
 72         //
 73         this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
 74         this.BackColor = System.Drawing.Color.White;
 75         this.ClientSize = new System.Drawing.Size(416, 413);
 76         this.Controls.AddRange(new System.Windows.Forms.Control[] {
 77         this.panel1});
 78         this.Name = "Form1";
 79         this.Text = "Form1";
 80         this.Load += new System.EventHandler(this.Form1_Load);
 81         this.ResumeLayout(false);  
 82       }
 83       #endregion
 84   
 85       /// <summary>
 86       /// The main entry point for the application.
 87       /// </summary>
 88       [STAThread]
 89       static void Main()
 90       {
 91         Application.Run(new Form1());
 92       }
 93   
 94       private void Form1_Load(object sender, System.EventArgs e)
 95       {
 96         int a=0;
 97         int x=0,y=0;
 98         for (a=0;a<=99;a++)
 99         {
100           n[a] = new Button();
101           n[a].BackColor =Color.White;
102           n[a].FlatStyle = FlatStyle.Flat;
103           n[a].Width = panel1.Width / 10;
104           n[a].Left = x * n[a].Width;
105           n[a].Height = panel1.Height / 10;
106           n[a].Top = y * n[a].Height;
107           n[a].Name = "b" + a;
108           panel1.Controls.Add(n[a]);
109           panel1.Controls[a].MouseDown += new MouseEventHandler(this.ButtonArray_OnClick);
110           
111           x += 1;
112           if (x == 10)
113           {
114            x = 0;
115            y += 1;
116           }
117         }  
118       }
119   
120       private void ButtonArry_Onclick(object sender, MouseEventArgs e)
121         {
122             MouseEventArgs arg = (MouseEventArgs)e;
123             Button b1 = (Button)sender;
124             string bname = (Convert.ToInt32(b1.Name.Substring(1)) + 1).ToString();
125             string zerolen = "000";
126             zerolen = zerolen.Substring(bname.Length - 1);
127             if (arg.Button == MouseButtons.Right)
128             {
129                 //b1.BackColor = Color.White;
130                 //b1.BackColor = Color.Black;
131                 if (b1.Image != null)
132                 {
133                     b1.Image = null;
134                 }
135             }
136             else
137             {
138                 b1.Image = Image.FromFile(@"F:\PNG-" + zerolen + bname + ".png");
139             }
140         }
141     }
142   }

 

posted @ 2013-09-04 15:35  Cornelius  阅读(358)  评论(0)    收藏  举报