Thread: GUI button

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    13

    GUI button

    Hi, how are y'all?
    I'm a C++ novice and I'm having trouble with what is essentially my first GUI wxWidgets program. The code below works in getting one button on the dialog but in trying to create a second button in the same way, it just crashes on compile.

    Code:
        //Adding button control
        wxButton *button = new wxButton(this, ID_BUTTON_CLICK, "Display");
        
        dialogSizer->Add(textSizer, 0, wxALIGN_CENTER);
        dialogSizer->Add(button,
            0,    //make vertically unstretchable
            wxALIGN_CENTER | wxALL, 10);        //center horizontally and set border width to 10
        
        SetSizer(dialogSizer);        //use dialog sizer for layout
        dialogSizer->Fit(this);    //fit dialog to components
    How would I create a second button like this one?
    Thanks,
    sammacs

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I believe your problem might be because of your button identifier (ID_BUTTON_CLICK). Try changing that, so label them as different id's.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    Hi, thanks for the input. I don't think that is the problem though as I've tried numerous different IDs and setups. Here is the working code for one button. I think my sizers are wrong as well. I'd like the second button directly below or next to the first.

    PersonDialog.h
    Code:
    #ifndef PERSONDIALOG_H
    #define PERSONDIALOG_H
    
    class PersonDialog : public wxDialog
    {
        public:
            //---Constructor. Creates a new dialog
            PersonDialog(wxWindow* parent, const wxString& title);
            
            //---Destructor
            ~PersonDialog();
            
            //---Processes Click Button
            void OnButtonClick(wxCommandEvent &event);
        
        protected:
            DECLARE_EVENT_TABLE()
            
        private:
            wxTextCtrl *name;
            wxTextCtrl *social;
            wxTextCtrl *hours;
            wxTextCtrl *wage;
            
            enum
            {
                ID_BUTTON_CLICK = 1000
            };
            
    };
    PersonDialog.cpp
    Code:
    #include <wx/wx.h>
    #include <string>
    #include "PersonDialog.h"
    
    using namespace std;
    
    PersonDialog::PersonDialog(wxWindow* parent, const wxString& title)
        : wxDialog(parent, -1, title, wxDefaultPosition, wxDefaultSize,
            wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
    {
        //Constructing a vertical sizer
        wxBoxSizer *dialogSizer = new wxBoxSizer(wxVERTICAL);
        
        name = new wxTextCtrl(this, -1);
        social = new wxTextCtrl(this, -1);
        hours = new wxTextCtrl(this, -1);
        wage = new wxTextCtrl(this, -1);
        
        //Constructing a grid sizer with 5 rows and 2 columns (use 10 pixels
        wxFlexGridSizer *textSizer = new wxFlexGridSizer(5, 2, 10, 10);
        
        //Adding static text control
        textSizer->Add(new wxStaticText(this, -1, "Name: "),
            1,    //make vertically stretchable
            wxEXPAND);        //make horizontally stretchable
        textSizer->Add(name, 1, wxEXPAND);
        textSizer->Add(new wxStaticText(this, -1, "Social no: "), 1, wxEXPAND);
        textSizer->Add(social, 1, wxEXPAND);
        textSizer->Add(new wxStaticText(this, -1, "Hours worked: "), 1, wxEXPAND);
        textSizer->Add(hours, 1, wxEXPAND);
        textSizer->Add(new wxStaticText(this, -1, "Hourly wage: "), 1, wxEXPAND);
        textSizer->Add(wage, 1, wxEXPAND);
            
        //Adding button control
        wxButton *button = new wxButton(this, ID_BUTTON_CLICK, "Display");
        
        dialogSizer->Add(textSizer, 0, wxALIGN_CENTER);
        dialogSizer->Add(button,
            0,    //make vertically unstretchable
            wxALIGN_CENTER | wxALL, 10);        //center horizontally and set border width to 10
        
        SetSizer(dialogSizer);        //use dialog sizer for layout
        dialogSizer->Fit(this);    //fit dialog to components
        
    }
    
    PersonDialog::~PersonDialog()
    {
    }
    
    BEGIN_EVENT_TABLE(PersonDialog, wxDialog)
        EVT_BUTTON(ID_BUTTON_CLICK, PersonDialog::OnButtonClick)
    END_EVENT_TABLE()
    
    void PersonDialog::OnButtonClick(wxCommandEvent &event)
    {
        string n = name->GetValue().c_str();
        string s = social->GetValue().c_str();
        string h = hours->GetValue().c_str();
        string w = wage->GetValue().c_str();
        string text = "Hello " + n + " (" + s + "). Your earnings for this week are " + h + "*" + w + ".";
        
        wxMessageDialog *dlg = new wxMessageDialog(this, text.c_str(),
            "Message Dialog", wxOK | wxCENTER | wxICON_INFORMATION, wxDefaultPosition);
        
        dlg->ShowModal();
        dlg->Destroy();
    }
    Here are the other two files needed to run the program:

    PersonDlgApp.h
    Code:
    #ifndef PERSONDLGAPP_H
    #define PERSONDLGAPP_H
    class PersonDialogApp : public wxApp
    {
        public:
            //initialise the application
            
            virtual bool OnInit();
    };
    
    DECLARE_APP(PersonDialogApp)
    
    #endif  //TEXTEDITORAPP_H
    PersonDlgApp.cpp
    Code:
    #include <wx/wx.h>
    #include "PersonDlgApp.h"
    #include "PersonDialog.h"
    
    IMPLEMENT_APP(PersonDialogApp)
    
    bool PersonDialogApp::OnInit()
    {
        PersonDialog *dlg = new PersonDialog(NULL,"Worker details");
        dlg->ShowModal();
        dlg->Destroy();
        
        return true;
    }
    The output is just a test at the moment. I'll do the converting strings to integers stuff later.

    Any help greatly appreciated
    thanks,
    sam

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    Could someone please try and help me. I've made no progress. The full code so far is in the post above.
    Thanks,
    Sam

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Did you try something like this?
    Code:
        wxButton *button2 = new wxButton(this, ID_BUTTON2, "Display");
        
        dialogSizer->Add(button2,
            0,    //make vertically unstretchable
            wxALIGN_CENTER | wxALL, 10);        //center horizontally and set border width to 10

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    13
    Yer that's the sort of thing I've been trying but it doesn't even give compile errors then, it just crashes. Maybe because it's trying to create the button over the first(I don't know).
    sam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GUI Programming...
    By ShadeS_07 in forum C++ Programming
    Replies: 12
    Last Post: 12-28-2008, 04:58 PM
  2. Developing GUI objects in C - advice needed
    By officedog in forum C Programming
    Replies: 14
    Last Post: 10-31-2008, 03:30 PM
  3. elliptical button
    By geek@02 in forum Windows Programming
    Replies: 0
    Last Post: 11-21-2006, 02:15 AM
  4. writing text over a deleted button
    By algi in forum Windows Programming
    Replies: 4
    Last Post: 05-02-2005, 11:32 AM
  5. GUI Programming :: C++ Exclusive
    By kuphryn in forum C++ Programming
    Replies: 5
    Last Post: 01-25-2002, 03:22 PM