同意
用控件数组
这样就可以在button的单击事件中改变控件数组的背景色
用控件数组
这样就可以在button的单击事件中改变控件数组的背景色
解决方案 »
- c#中将sqlite数据库查询的结果,写入到txt文件
- 求打开一个文件得同时,打开另一个文件
- 如何用C#写出这样的xml文件
- 怎样打包能限制软件的使用日期?
- VS2005加Offiec图标
- 100分求DateTimePicker的一个问题。
- 错误提示:找不到类或命名空间!!!
- 求救:如何把一个int类型的数值的二进制数字直接显示出来啊?
- 如何将大图片存进数据库
- 请问image.Save();出现这种错误是怎么回事,有些图片可以但是有些图片就会出现这种错误
- datagrid的数据更新问题
- 哪位高人知道, C#中有没有类似 delphi 的 Format("%d %s", [x, s]) 函数 或者 C 里面的 printf("%d %s", x, s) 函数, 返回一个格式化以后
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 ; } }
}
在Initialize 或者 构造函数中添加维护事件
button.Click += new System.EventHandler(MaintanceFunc) ;
private void MaintanceFunc(object sender, System.EventArgs e)
{
爱干嘛干嘛
}
然后不论你是10×10 还是10×100 都无所谓啊 你所要做地 就是把你的控件加上去
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