Reputation: 33
I am programming a little sudoku game. I have a 2d-Array and lots of buttons that display numbers. When I click on one of those buttons(sudokuBoxClicked) I get the x and y position of the button from its name. That works for now. At the end of the function, the function changeNumberinSudoku is opened. This function first changes the array-entry to the number I want on that button.text, then it shall change the button.text so that it is no longer null but has the value of the variable changeNumber
As I have 81 buttons in my sudoku, the program has no idea what button.text it should change. So it should change the text-property of the button that triggered the sudokuBoxClicked-function. My question now is basically:
How do i get the object sender to the 2nd function, so that I may change its properties?
To give you a better understanding of what I need, here is the important code:
int changeNumber = 1;
int[,] sudokuMap = new int[9,9];
private void sudokuBoxClicked(object sender, EventArgs e)
{
string nameOfClickedBox = (sender as Button).Name;
int xPositionOfClickedButton = Convert.ToInt32(nameOfClickedBox.Substring(7, 1));
int yPositionOfClickedButton = Convert.ToInt32(nameOfClickedBox.Substring(9, 1));
//The array-position of the clicked box, is contained in its name.
changeNumberinSudoku(xPositionOfClickedButton, yPositionOfClickedButton);
}
private void changeNumberinSudoku(int xPos, int yPos)
{
sudokuMap[xPos, yPos] = changeNumber;
}
Thanks to all the nice people out there who would be able to help me.
-AlexanderLe
Upvotes: 0
Views: 449
Reputation: 1151
Pass the sender as an argument to the second function:
changeNumberinSudoku(xPositionOfClickedButton, yPositionOfClickedButton, (sender as Button));
And change your function signature:
private void changeNumberinSudoku(int xPos, int yPos, Button btn)
{
sudokuMap[xPos, yPos] = changeNumber;
btn.Text = ...
}
Upvotes: 1