i have a little dificulties in the area of object-oriented programming i guess...
i am making a chess-like game
i created the classes "board" and "square" (the square inherit from picturebox)
now - in the constructor of the board i create 8X8 squares inside it

all the program is ran from the form1.cs

inside the board when i create new 8X8 squares i add them -

this.square[i, j].Click += new System.EventHandler(this.Square_Click);

and in the same place under that i place the -

private void Square_Click(object sender, EventArgs e)

the problem is, i want that things inside form1.cs will change when i for example click on one of the squares but the thing is that the things inside the Square_CLick doesnt "know" the things that inside form1.cs

for example - i create button1 in form1.cs.
from the form1.cs i can for example change its Text Button1.Text etc.
but from the Square_Click i cant do that when i click on the button1...

how can i do that?
thanks in advance - and sorry if its complicated to understand - rough english + new in C#

5 Years
Discussion Span
Last Post by galhajaj

YOu didnt start coding the correct way. You have to put buttons into an array, and loop through them and create a common event for them, like this:

//on form load on in constructor:
Button[] btns = new Buttons[]{button1, button2, button2, button4, button5, button6, button7, button8 };
foreach(Button btn in btns)
     btn.Click += new EventHanlder(buttons_Click);

private void buttons_Click(object sender, EventArgs e)
    Button button = sender as Button;
    //clicked button is not in "button" variable.
    string buttonName = button.Name;
    string butonText = button.Text;
    //you can now opperate wtih them..

@mitja : you probably missed this :) : http://www.daniweb.com/software-development/csharp/threads/363377
@galhajaj : Hi! Me again :)
As Mitja says, do something in your Square click handler.

private void Square_Click(object sender, EventArgs e)
    Square SquareClickedOn = sender as Square;
//The sender object contains the object that was clicked on
//In this case a square, because you programmed it that way.
//Now SquareClickedOn can be used like you want
//you could print his Name or whatever you defined previously for it

Oh, I see, I sure did miss it. Lately I wasnt around much. I can see there is a whole other thread for this issue. So I wont even interrupt.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.