Search code examples
c++wxwidgets

Disable Button Functionality (Wx Widgets)


So I'm using the WxDev frame to develop the GUI of our program. So it has two buttons, "Upload" and "Analyze".

The sequence should be a user uploads an image. Once it's uploaded, he has the capability of clicking the "Analyze" button. Now, once he clicks the analyze, I would want to disable it. The button will only be then enabled once there is a new image to analyze.

Here's the code for the buttons I have although it might be irrelevant.

UPLOAD:

void NBA_Jersey_RecognitionFrm::WxButton1Click(wxCommandEvent& event)
{
 openIMGFileDialog->ShowModal();
    if (openIMGFileDialog->GetPath().IsEmpty())
    {
        return;
    }

    imageopen = imgFile.LoadFile(openIMGFileDialog->GetPath(), wxBITMAP_TYPE_ANY);

    int h = imgFile.GetHeight();
    int w = imgFile.GetWidth();

    pic.Create(w,h);

        for (int x = 0; x < w; x++)
        {
             for (int y = 0; y < h; y++)
            {
                pic.SetRGB(x, y, 240, 240, 240);
            }
        }

    if (300 >= (h*300/w))
    {       
        displayIMG->SetBitmap(pic.Scale(300,h*300/w));
        displayIMG->SetBitmap(imgFile.Scale(300,h*300/w));
    } 
    else
    {
        displayIMG->SetBitmap(pic.Scale(w*300/h,300));
        displayIMG->SetBitmap(imgFile.Scale(w*300/h,300));
    }

}

Here's an excerpt of my ANALYZE button:

void NBA_Jersey_RecognitionFrm::WxButton1Click0(wxCommandEvent& event)
{
    ofstream resultsFile;

    stringstream ss;
    string s;

    int height = imgFile.GetHeight();
    int width = imgFile.GetWidth();

    RedVal = new int* [width];
    GreenVal = new int* [width];
    BlueVal= new int* [width];

    for(int i=0; i<width; i++) {
        RedVal[i] = new int[height];
        GreenVal[i] = new int[height];
        BlueVal[i] = new int[height];
    }


    //int RedVal[width][height];
    //int GreenVal[width][height];
    //int BlueVal[width][height];

    resultsFile.open("results.txt");
    //resultsFile << "x,y,Red,Green,Blue \n";


    for(int h=0; h<height; h++) {
        for(int w=0; w<width; w++) {

            RedVal[w][h]=imgFile.GetRed(w,h);
            GreenVal[w][h]=imgFile.GetGreen(w,h);
            BlueVal[w][h]=imgFile.GetBlue(w,h);

            //ss << h << "," << w << "," << RedVal[0][h] << "," << GreenVal[0][h] << "," << BlueVal[0][h] <<"\n";
            //resultsFile << ss.str();
            //resultsFile << h << "," << w << "," << RedVal[w][h] << "," << GreenVal[w][h] << "," << BlueVal[w][h] <<"\n";
        }

    }

Is there a built-in function in WxDev C++ about disabling a button? Or what should I add to the code I have? Thank you.


Solution

  • You can disable a button (and most controls) like so:

    void NBA_Jersey_RecognitionFrm::WxButton1Click0(wxCommandEvent& event)
    {
        m_analyzeButton->Enable(false);
        ...
    

    Then to re-enable it:

    void NBA_Jersey_RecognitionFrm::WxButton1Click(wxCommandEvent& event)
    {
     openIMGFileDialog->ShowModal();
        if (openIMGFileDialog->GetPath().IsEmpty())
        {
            return;
        }
        m_analyzeButton->Enable(true);
        ...
    

    Replace 'm_analyzeButton' with your name for the button.