同意
用控件数组
这样就可以在button的单击事件中改变控件数组的背景色

解决方案 »

  1.   

    把一百个按钮的单击事件都委托到一个函数中其中参数object就是你所单击的按钮的装箱,拆箱之后就可以了。
      

  2.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WinFormWidth
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {

    private  Button[,] btn =  new Button[3,3];

    /// <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 Form Designer generated code
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 273);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    this.Size= new Size(Screen.PrimaryScreen.Bounds.Width,200);
    this.Location= new Point(0,0);
    for(int i=0;i<3;i++)
    {
    for(int j=0; j<3; j++)
    {
    btn[i,j]=new Button();
    btn[i,j].Parent=this;
    btn[i,j].Text=i.ToString() + "," + j.ToString();
    btn[i,j].Tag=new Point(i,j);
    btn[i,j].Size=new Size(60,20);
    btn[i,j].Location=new Point(60*i,20*j);
    btn[i,j].Enabled =true;
    btn[i,j].Click += new EventHandler(btnsOnClicked);
    }
    }
    } void btnsOnClicked(object o, EventArgs e)
    {
    //this.Text=o.GetType().ToString();
    Button sender=(Button)o;

    //this.Text= sender.Name;
    Point point=(Point)sender.Tag;
    //this.Text=(point.X + point.Y).ToString();
    btn[point.X,point.Y].BackColor = Color.Red ; } }
    }
      

  3.   

    为什么要用控件数组可以继承一个button :让button自己来维护自己的状态
    在Initialize 或者 构造函数中添加维护事件
    button.Click +=  new System.EventHandler(MaintanceFunc) ;
    private void MaintanceFunc(object sender, System.EventArgs e)
    {
       爱干嘛干嘛
    }
    然后不论你是10×10 还是10×100 都无所谓啊 你所要做地 就是把你的控件加上去
      

  4.   

    可以用picturebox,根据坐标来判断是那个区域,在重绘区域的颜色。
      

  5.   

    Public Class Form1    Inherits System.Windows.Forms.Form    'Private Declare Function GetPixel Lib "gdi32" Alias "GetPixel" (ByVal hdc As Integer, ByVal x As Integer, ByVal y As Integer) As Integer
        Private WithEvents pic As pics
    #Region " Windows 窗体设计器生成的代码 "    Public Sub New()
            MyBase.New()        '该调用是 Windows 窗体设计器所必需的。
            InitializeComponent()        '在 InitializeComponent() 调用之后添加任何初始化    End Sub    '窗体重写处置以清理组件列表。
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub    'Windows 窗体设计器所必需的
        Private components As System.ComponentModel.IContainer    '注意:以下过程是 Windows 窗体设计器所必需的
        '可以使用 Windows 窗体设计器修改此过程。
        '不要使用代码编辑器修改它。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            '
            'Form1
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
            Me.ClientSize = New System.Drawing.Size(416, 357)
            Me.Name = "Form1"
            Me.Text = "Form1"    End Sub#End Region    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            pic = New pics()
            pic.BackColor = Color.BurlyWood
            pic.Dock = DockStyle.Fill
            pic.Visible = True
            Me.Controls.Add(pic)
        End Sub    Private Sub pic_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic.MouseDown
            If e.Button = MouseButtons.Right Then Exit Sub
            Dim g As Graphics
            Dim x As Integer
            Dim y As Integer        Dim p As New SolidBrush(Color.Blue)        g = pic.CreateGraphics        x = Int(e.X / 60)
            y = Int(e.Y / 60)
            Dim rc As New Rectangle(x * 60 + 1, y * 60 + 1, 58, 58)        g.FillRectangle(p, rc)
        End Sub
    End Class‘=========================
    Imports System.Drawing
    Public Class pics
        Inherits PictureBox
        Const DEST = 60
        Protected Overrides Sub OnPaint(ByVal pe As System.Windows.Forms.PaintEventArgs)
            Dim g As Graphics = pe.Graphics
            Dim P As New Pen(Color.Brown)
            Dim I As Integer
            g.TranslateTransform(0, 0)        For I = 0 To MyBase.Width Step DEST
                g.DrawLine(P, I, 0, I, Me.Width)
            Next
            '
            For I = 0 To MyBase.Height Step DEST
                g.DrawLine(P, 0, I, Me.Width, I)
            Next    End Sub
    End Class