event handler for an array of buttons
Hey,
Firts of this isnt a project in C# but in J# ( since that is what we are getting in programming classes at the moment ).
Anyway most of the things are pretty much the same i guess.
Im creating a game like Minesweeper, i have set up the whole world ( a 2 d array of chars ), and then ive set up a 2 D array of buttons.
My problem now is to get an eventHandler that looks at all members of the 2 d array of buttons.
Here´s the code i have up till now (only the code of Form1 since thats where ill be needing the eventHandler).
Code:
package MineSpotter;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;
/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox nMines_txt;
private System.Windows.Forms.Label label1;
/**
* Required designer variable.
*/
private System.ComponentModel.Container components = null;
private System.Windows.Forms.Button reset_btn;
private System.Windows.Forms.Label info_txt;
private int nMines;
private World w;
private System.Windows.Forms.Button defaultButton[][] = new Button [World.HEIGHT-2][World.WIDTH-2];
public Form1()
{
w = new World(nMines);
InitializeComponent();
initMyButtons();
}
/**
* Clean up any resources being used.
*/
protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}
#region Windows Form Designer generated code
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.reset_btn = new System.Windows.Forms.Button();
this.nMines_txt = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.info_txt = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// reset_btn
//
this.reset_btn.set_Font(new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((ubyte)(System.Byte)(((ubyte)0)))));
this.reset_btn.set_Location(new System.Drawing.Point(352, 288));
this.reset_btn.set_Name("reset_btn");
this.reset_btn.set_Size(new System.Drawing.Size(128, 24));
this.reset_btn.set_TabIndex(0);
this.reset_btn.set_Text("Play Again - Reset");
this.reset_btn.add_Click( new System.EventHandler(this.reset_btn_Click) );
//
// nMines_txt
//
this.nMines_txt.set_Location(new System.Drawing.Point(432, 16));
this.nMines_txt.set_Name("nMines_txt");
this.nMines_txt.set_Size(new System.Drawing.Size(40, 20));
this.nMines_txt.set_TabIndex(3);
this.nMines_txt.set_Text("");
//
// label1
//
this.label1.set_Location(new System.Drawing.Point(360, 16));
this.label1.set_Name("label1");
this.label1.set_Size(new System.Drawing.Size(72, 23));
this.label1.set_TabIndex(6);
this.label1.set_Text("Total Mines:");
//
// info_txt
//
this.info_txt.set_Location(new System.Drawing.Point(360, 48));
this.info_txt.set_Name("info_txt");
this.info_txt.set_Size(new System.Drawing.Size(112, 40));
this.info_txt.set_TabIndex(7);
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(492, 473));
this.get_Controls().Add(this.info_txt);
this.get_Controls().Add(this.label1);
this.get_Controls().Add(this.nMines_txt);
this.get_Controls().Add(this.reset_btn);
this.set_Name("Form1");
this.set_Text("Lamb The Defuser");
this.add_Load( new System.EventHandler(this.Form1_Load) );
this.ResumeLayout(false);
}
#endregion
#region My own button creations
private void initMyButtons() {
//initialise every button
for(int y=0;y<World.HEIGHT-2;y++){
for(int x=0;x<World.WIDTH-2;x++){
this.defaultButton[y][x] = new Button();
}
}
for(int y=0;y<World.HEIGHT-2;y++){
for(int x=0;x<World.WIDTH-2;x++){
this.defaultButton[y][x].set_Location(new System.Drawing.Point((20+(20*y)),((20*x)+20)));
this.defaultButton[y][x].set_Name(""+y+x);
this.defaultButton[y][x].set_Size(new System.Drawing.Size(20, 20));
this.defaultButton[y][x].set_TabIndex(1);
this.get_Controls().Add(this.defaultButton[y][x]);
}
}
}
#endregion
//oproepen wanneer er op een button is geklikt
public void updatePlace(int x,int y) {
defaultButton[y][x].set_Text(""+w.wereld[y+1][x+1]);
}
/**
* The main entry point for the application.
*/
/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}
private void Form1_Load (Object sender, System.EventArgs e) {
info_txt.set_Text("Enter a value between 0 and 100.");
}
private void reset_btn_Click (Object sender, System.EventArgs e) {
setWorld();
}
private void setWorld() {
nMines = Integer.parseInt(nMines_txt.get_Text());
if(nMines>=100 || nMines<=0){
info_txt.set_Text("Incorrect Value, Enter a value between 0 and 100.");
}else{
info_txt.set_Text("");
w.createWorld(nMines);
}
}
}
Even if you tell me how to do it in C# ill be thankfull ( since afterall this is a C# board and not a J# board ).
Thanks in advance,
Ganglylamb