Search code examples
c++refreshwxwidgetssudoku

How to refresh in wxWidgets, C++


I'm making a sudoku-game for a class in school and as you might have guessed i'm using wxWidgets with C++.

My problem is that when I want to create a new Board with diffrent values in the squares I don't know how to refresh the program.

I create the board with the createBoard-function:

//Storing a random-generated sudokuboard into "board".
setBoard(board);
// sizers for the layout of bricks and buttons
wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);  // overall layout
// 9 rows and 9 columns with fixed height and width
wxGridSizer *gs = new wxGridSizer(9, 9, 1, 1);
wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);       // button commands

// Define font and font size for the text in the squares
// Define valid input values for the text in the squares
csCharCtrl::Initialize(wxT("123456789"), sz);

// Create the squares
for (int i = 0; i < 81; i++) {
    m_square[i] = CreateSquare(this, i+1, i/9, i%9);
    // install an event handler for Set Focus event for the square
    gs->Add(m_square[i], 0, wxEXPAND);
}
Bind(wxEVT_CHILD_FOCUS, &BoardPanel::OnChildFocus, this);

// buttons commands
// Check command
wxButton *btnnew = new wxButton(this, ID_New, wxT("New board"));
btnnew->Bind(wxEVT_COMMAND_BUTTON_CLICKED, &BoardPanel::OnNew, this);

hbox2->Add(btnnew, 2, wxEXPAND);

vbox->Add(gs, 1, wxALIGN_CENTER | wxALL, 20);
vbox->Add(hbox2, 0, wxALIGN_CENTER | wxALL, 20);


SetSizer(vbox);

}

setBoard generates a new sudoku-game inside a multidimentional vector.

I've got a button that is listening to my clicks and I want it to create a new board by calling setBoard again and refreshing the board on the click.

How do I do that?

Code for my button is simple:

void BoardPanel::OnClick(wxCommandEvent& WXUNUSED(event)){
    //CODE onClick
}

Here is a picture of the program:

enter image description here


Solution

  • I suppose m_square[i] is wxTextCtrl* or something similar. In the OnClick event handler, after setBoard(), I would do something like

    for (int i = 0; i < 81; i++) 
    {
        m_square[i]->SetValue(/*square value text*/);
    }
    

    And that should do it, given that your squares don't change size or anything.

    If your controls did change size after changing their contents, you'd have to also do

    vbox->FitInside(this); //since you probably don't want to resize the main window
    Layout();
    

    given that your event handler seems to be a member of your main panel class.