动态生成复选框
CheckBox[] cb=new CheckBox[n*n];
添加事件
cb[i].Click+=new System.EventHandler(this.MyClick);
在事件处理函数中
x=i/n;y=i%n;
cb[(x-1)*3+y].Clicked取反
cb[(x+1}*3+y].Clicked取反
cb[x*3+y-1].Clicked取反
cb[x*3+y+1].Clicked取反
(要自己判断是否越界)

解决方案 »

  1.   

    using System;
    using System.Drawing;
    using System.Collections;
    0Ausing System.ComponentModel;0D
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication11
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.CheckBox checkBoxSeed;
    CheckBox[,] arrCB;
    /// <summary>
    /// Required designer variable.
    /// </summary>
    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 );
    0A}#region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    0A/// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.checkBoxSeed = new System.Windows.Forms.CheckBox();
    this.button1 = new System.Windows.Forms.Button();
    this.SuspendLayout();
    // 
    // checkBoxSeed
    // 
    this.checkBoxSeed.Location = new System.Drawing.Point(80, 96);
    this.checkBoxSeed.Name = "checkBoxSeed";
    this.checkBoxSeed.Size = new System.Drawing.Size(16, 16);
    this.checkBoxSeed.TabIndex = 0;
    this.checkBoxSeed.Text = "checkBoxSeed";
    this.checkBoxSeed.Click += new System.EventHandler(this.checkBoxSeed_CheckedChanged);0D
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(16, 8);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(184, 40);
    this.button1.TabIndex = 1;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Controls.AddRange(new System.Windows.Forms.Control[] {0D
      this.button1,
      this.checkBoxSeed});
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.ResumeLayout(false);
    0A
    }
    #endregion/// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());0D
    }private void button1_Click(object sender, System.EventArgs e)
    {
    AddCheckBox(10,10);
    }
    private struct ArrLocation
    0A{
    public ArrLocation(int ii,int ij)
    {
    i=ii;
    j=ij;
    }
    public int i;
    public int j;
    }
    public void AddCheckBox(int row,int colomn){
    arrCB=new CheckBox[row,colomn];
    0APoint po=checkBoxSeed.Location;
    Point p=po;for(int i=0;i<10;i++)
    {
                    p.Y=po.Y;
    p.Offset(20,0);
    for(int j=0;j<10;j++)
    {
    arrCB[i,j]=new CheckBox();
    // 
    // checkBoxSeed
    // 
    p.Offset(0,20);
    arrCB[i,j].Location=p;
    arrCB[i,j].Name = "checkBox" + i.ToString() + j.ToString();
    arrCB[i,j].Size = checkBoxSeed.Size;
    arrCB[i,j].TabIndex = 0;
    arrCB[i,j].Tag=new ArrLocation(i,j);
    arrCB[i,j].Text = "";
    arrCB[i,j].Click += new System.EventHandler(this.checkBoxSeed_CheckedChanged);0D
    this.Controls.Add(arrCB[i,j]);}
    }
    this.ResumeLayout();}private void checkBoxSeed_CheckedChanged(object sender, System.EventArgs e)
    {
    CheckBox cb=(CheckBox)sender;
    ArrLocation al=(ArrLocation)cb.Tag;try
    {
    arrCB[al.i,al.j+1].Checked=!arrCB[al.i,al.j+1].Checked;arrCB[al.i,al.j-1].Checked=!arrCB[al.i,al.j-1].Checked;0DarrCB[al.i+1,al.j].Checked=!arrCB[al.i+1,al.j].Checked;arrCB[al.i-1,al.j].Checked=!arrCB[al.i-1,al.j].Checked;0D
    }
    catch(Exception ex)
    {
    Console.Write(ex);
    }}private void Form1_Load(object sender, System.EventArgs e)0D
    {}
    }
    }
    // 新建一winform apllication,把这个整个拷贝进去。run一下就行了。 
     
    ------------------------------
    我是一只小小鸟
    欢迎交流!
    MSN&