正文

C#中动态创建控件及事件处理程序  2006-05-25 14:46:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/Csharpsky/14844.html

分享到:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace Miner
{
/// <summary>
/// Summary des cription for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
 private System.Windows.Forms.Panel panel1;
 /// <summary>
 /// Required designer variable.
 /// </summary>
 ///

 private Button[] n =new Button[100];

热门推荐故事

  • C#的控制台输入和输出
  • .Net正式版中的一些Bug及其解决方案
  • .NET体系中的源程序安全问题(2)
  • C#方法中的ref和out
  • c#学习笔记(1)
  • 浅析J2EE与.NET平台优劣
  • 用.net静态变量取代Application,速度更快
  • .NET对Sun ONE: Web服务开发平台对比测试 (2)
  • 微软.Net卧虎藏龙
  • .net中即时消息发送的实现……
  •  private int[] kn=new int[100];

     private System.ComponentModel.Container components = null;

     public Form1()
     {
      //
      // Required for Windows Form Designer support
      //
      InitializeComponent();

      //
      // TODO: Add any constructor code after InitializeComponent call
      //
     }

     /// <summary>
     /// Clean up any resources being used.
     /// </summary>
     protected override void Dispose( bool disposing )
     {
      if( disposing )
      {
      if (components != null)
      {
       components.Dispose();
      }
      }
      base.Dispose( disposing );
     }

     #region Windows Form Designer generated code
     /// <summary>
     /// Required method for Designer support - do not modify
     /// the contents of this method with the code editor.
     /// </summary>
     private void InitializeComponent()
     {
      this.panel1 = new System.Windows.Forms.Panel();
      this.SuspendLayout();
      //
      // panel1
      //
      this.panel1.Location = new System.Drawing.Point(8, 8);
      this.panel1.Name = "panel1";
      this.panel1.Size = new System.Drawing.Size(400, 400);
      this.panel1.TabIndex = 0;
      //
      // Form1
      //
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
      this.BackColor = System.Drawing.Color.White;
      this.ClientSize = new System.Drawing.Size(416, 413);
      this.Controls.AddRange(new System.Windows.Forms.Control[] {
              this.panel1});
      this.Name = "Form1";
      this.Text = "Form1";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);

     }
     #endregion

     /// <summary>
     /// The main entry point for the application.
     /// </summary>
     [STAThread]
     static void Main()
     {
      Application.Run(new Form1());
     }

     private void Form1_Load(object sender, System.EventArgs e)
     {
      int a=0;
      int x=0,y=0;
      for (a=0;a<=99;a++)
      {
      n[a] = new Button();
      n[a].BackColor =Color.White;
      n[a].FlatStyle = FlatStyle.Flat;
      n[a].Width = panel1.Width / 10;
      n[a].Left = x * n[a].Width;
      n[a].Height = panel1.Height / 10;
      n[a].Top = y * n[a].Height;
      n[a].Name = "b" + a;
      panel1.Controls.Add(n[a]);
      panel1.Controls[a].MouseDown  += new MouseEventHandler(this.ButtonArray_OnClick);
      
      


      x += 1;
      if (x == 10)
      {
       x = 0;
       y += 1;
      }
      }

     

     }

     private void ButtonArray_OnClick(object sender, MouseEventArgs e)
     {
      MouseEventArgs arg=(MouseEventArgs)e;
          Button b1=(Button)sender;
      if (arg.Button==MouseButtons.Right )
      b1.BackColor=Color.White ;
      else
      {
      //b1.BackColor =Color.White ;
      b1.Image=Image.FromFile("f:my documentsmy pictureselements egular_smile.gif");
      }
     
     }
    }
    }

    阅读(2474) | 评论(0)


    版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

    评论

    暂无评论
    您需要登录后才能评论,请 登录 或者 注册