Windows Develop Bookmark and Share   
 index > Windows Forms General > Sudoku help needed
 

Sudoku help needed

Hi, I am trying to make a sudoku game but have run into a problem. basically i have a function to create the game view:

private void createView()
{
Table1.Rows.Clear();
for (int rowIndex = 0; rowIndex < State.Board.GetLength(0); rowIndex++)
{

TableRow row = new TableRow();
Table1.Rows.Add(row);

for (int columnIndex = 0; columnIndex < State.Board.GetLength(1); columnIndex++)
{
TableCell cell = new TableCell();
int value = State.Board[columnIndex, rowIndex];

if ((ActiveCell == null) || (ActiveCell.Row != columnIndex) || (ActiveCell.Column != rowIndex))
{
string id = String.Format("BoardButton_{0}_{1}", columnIndex, rowIndex);
if (value >= 10)
{
Image image = new Image();
image.ID = id;
image.ImageUrl = "Images/" + value / 10 + ".jpg";
image.Height = image.Width = 60;
cell.Controls.Add(image);
}
else
{
ImageButton button = new ImageButton();
button.ID = id;
button.ImageUrl = "Images/0.gif";
button.Height = button.Width = 60;
button.CommandArgument = String.Format("{0}:{1}", columnIndex, rowIndex);
button.Click += new ImageClickEventHandler(button_Click);
cell.Controls.Add(button);
}
}
else
{
TextBox editTextBox = new TextBox();
editTextBox.ID = String.Format("BoardEditTextBox_{0}_{1}", columnIndex, rowIndex);
editTextBox.Width = new Unit(30);
cell.Controls.Add(editTextBox);

LinkButton editLinkButton = new LinkButton();
editLinkButton.ID = String.Format("BoardEditLinkButton_{0}_{1}", columnIndex, rowIndex);
editLinkButton.Click += new EventHandler(editLinkButton_Click);
editLinkButton.Text = ">>";
cell.Controls.Add(editLinkButton);
}
row.Cells.Add(cell);
}
}
}

the code for the buttons are as follows:

void editLinkButton_Click(object sender, EventArgs e)
{
Control linkButton = sender as Control;
TableCell cell = linkButton.Parent as TableCell;
int x = ActiveCell.Row;
int y = ActiveCell.Column;
string find = String.Format("BoardEditTextBox_{0}_{1}", x, y);
TextBox editBox = cell.FindControl(find) as TextBox;
State.Board[x, y] = int.Parse(editBox.Text);
ActiveCell = null;
createView();
}


void button_Click(object sender, EventArgs e)
{
ImageButton button = sender as ImageButton;
string[] xySplit = button.CommandArgument.Split(new char[] { ':' });
int x = int.Parse(xySplit[0]);
int y = int.Parse(xySplit[1]);
int value = int.Parse(State.Board[x, y].ToString());
this.ActiveCell = new BoardCell(x, y);
button.ImageUrl = "Images/" + value + ".jpg";
createView();
}

when i number is typed in, the createview() is called and the imageurl is set to 0. i cant think of a way to set the imageurl to the number typed in?

any help would be much apreciated.


Thanks

Andy
Montana47  Friday, December 08, 2006 12:44 AM

Surely if you know the cell X and Y positions you can just say:

((ImageButton)FindControl( String.Format("BoardButton_{0}_{1}", x, y) )).ImageUrl = ....

Paul Louth  Friday, December 08, 2006 12:55 AM
where would that line of code go?
Montana47  Friday, December 08, 2006 1:49 PM

Could I see your entire code-behind for the page? I'm not 100% on how you're creating the view. Normally I'd create the view once from the board state. Then if a button event fires which results in an edit then I'd change the board state, then as the Page_Load fired the controls would be updated. But I need to see how you're handling the state really.

Paul Louth  Friday, December 08, 2006 2:33 PM
how i make the board is quite complicated. It reads from a text file. Do you have live messenger? thanks for your help so far
Montana47  Friday, December 08, 2006 2:56 PM
I don't need to know how you make the board, I need to know what you're doing in your Page_Load/OnLoad, and any other event handlers.
Paul Louth  Friday, December 08, 2006 3:08 PM
oh right ok. heres the page_load:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
State = new MyState();
string path = Server.MapPath(boardPath);
State.LoadGame(path);
contentsView(path);

}
createView();
}

thats all the event handlers i think
Montana47  Friday, December 08, 2006 3:14 PM
anyone got any ideas?
Montana47  Sunday, December 10, 2006 7:32 PM
Remove createView from your button click method.
Paul Louth  Sunday, December 10, 2006 7:46 PM

You can use google to search for other answers

Custom Search

More Threads

• in c# (.NET) how can link to another Html page
• ClickOnce Start Menu Short cuts
• Is Item[string name] a member of Contol.ControlCollection?
• WebBrowser.Navigate IE7 pdf adobe not run in same window
• Disabling Right click in the OwnerDrawn Combobox
• 3D shape using the mouse???
• Select line in RichTextBox
• Menu Item Character Underline
• Outlook Express
• What is the best sample code for smart clients?