In a nutshell -
theGame[i][j].Click += clicker.buttonclickHandler(theGame, new ClickArgs(i, j))
the above line is giving me fits. Some additional info ... theGame is an array of buttons, the size of which is set at run time. What I am looking to do, is to have an event handler which can access elements of this array whenever one of the buttons is pressed, Hiding or Showing them.
As I understand it, the line performs a subscription ... so that clicker is now listening for the Click event from the specific button. This process is then repeated for each button in a for loop. The error I get is that the return type of my buttonclickHandler, which is void, is incorrect (should be System.EventHandler). However, System.EventHandler can't take custom arguments ... and I [believe] I need custom arguments in order to record which button was pressed.
I can post the full source if necessary, but it seems to me it's a bit long. Oh ... clicker is an instance of a the class that contains the buttonclickHandler method. And I do have the delegate setup in what I believe to be the correct way ...
Some things I've tried -
1) Well, I tried changing the return type to System.EventHandler ... however, this gave an error when I tried to pass ClickArgs instead of the generic EventArgs
2) I tried using new <Class Name>.buttonclickHandler( ...) ... this gave me an error that I was trying to use buttonclickHandler as a type instead of a method. I still think I may need something like this, as having the same instance subscribed to all of the buttons click events doesn't seem right to me.
3) I tried having the handler in the same class as the above line of code. This didn't seem to help, besides which the above is in a Form class that I didn't want to instantiate again ... perhaps it should be better partitioned.
4) At one time, through some manipulations, I got it to sort of work ... the event handler triggered, but apparently only when the button was created. After, when I clicked buttons, the handler did not trigger.
On second thought, here is most of my source ... some is in other files, and doesn't seem germane to the specific issue
public delegate void ClickDelegate(object sender, ClickArgs e);
public partial class gameForm : Form
{
//Padding is size of a form beyond what is needed for the buttons
//Offset is the position on the screen
const int GAME_FORM_PADDING_X = 40;
const int GAME_FORM_PADDING_Y = 40;
const int BUTTON_FOOTPRINT_X = 60;
const int BUTTON_FOOTPRINT_Y = 30;
const int GAME_FORM_OFFSET_Y = 200;
const int GAME_FORM_OFFSET_X = 200;
const int BUTTON_SIZE_X = 50;
const int BUTTON_SIZE_Y = 20;
int rows, cols, total;
bool human;
Button[][] theGame;
public gameForm()
{
InitializeComponent();
}
public gameForm(int height, int width, bool _human)
{
//The basic component initializtion
InitializeComponent();
//Set these form variables
rows = height;
cols = width;
human = _human;
total = rows * cols;
this.SetBounds(GAME_FORM_OFFSET_X, GAME_FORM_OFFSET_Y,
width * BUTTON_FOOTPRINT_X + 2*GAME_FORM_PADDING_X,
height * BUTTON_FOOTPRINT_Y + 2*GAME_FORM_PADDING_Y);
createGameBoard();
}
public void createGameBoard()
{
int i, j, x, y;
//Create an instance of the class that does the event handling
ClickHandlerClass clicker = new ClickHandlerClass();
//Create the delegate to handle button presses
ClickDelegate handler = clicker.buttonclickHandler;
//Create the game board
theGame = new Button[rows][];
for (i = 0; i < rows; i++)
{
theGame[i] = new Button[cols];
for (j = 0; j < cols; j++)
{
//Create the button; it knows its position in the full grid
theGame[i][j] = new Button();
//Calculate button positions
x = GAME_FORM_PADDING_X + j*BUTTON_FOOTPRINT_X;
y = GAME_FORM_PADDING_Y + i * BUTTON_FOOTPRINT_Y;
theGame[i][j].SetBounds(x, y,
BUTTON_SIZE_X, BUTTON_SIZE_Y);
//Display button indexes (incremented for looks)
theGame[i][j].Text = (i + 1) + "," + (1 + j);
//Subscribe the buttons to the handler that plays the game
theGame[i][j].Click += clicker.buttonclickHandler(theGame, new ClickArgs(i, j));
//Add the buttons to the forms' control list
this.Controls.Add(theGame[i][j]);
}
}
}
}
public class ClickHandlerClass
{
//Button clicks play the actual game - removing all buttons with a
//higher index in either row or column than the clicked button.
public void buttonclickHandler(object sender, ClickArgs e)
{
int i,j;
i = e.Row;
j = e.Col;
MessageBox.Show("Click fired for button " + i + ", " + j);
//sender.Visible = false;
}
}

New Topic/Question
Reply




MultiQuote





|