wxdialog return strings

This is a discussion on wxdialog return strings within the C++ Programming forums, part of the General Programming Boards category; I've got a wxdialog which produces integers dimxn dimxy and wxString dimcust when OK is pressed. Code: void CustomSizeDialog::OnOKButtonClick(wxCommandEvent& event) ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    27

    wxdialog return strings

    I've got a wxdialog which produces integers dimxn dimxy and wxString dimcust when OK is pressed.

    Code:
    void CustomSizeDialog::OnOKButtonClick(wxCommandEvent& event)
    {
        wxString dimx = TextDimX->GetValue();
        wxString dimy = TextDimY->GetValue();
        int dimxn = wxAtoi(dimx);
        int dimyn = wxAtoi(dimy);
        wxString dimcust;
        dimcust += dimx + wxT(" x ") + dimy;
        Close();
    }
    How do I get it to pass these back to the main program so I can carry on working on the values?

    Thanks
    Last edited by mikeyp; 12-22-2009 at 11:09 AM. Reason: altered code

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    If I'm not mistaken, with wxWidgets you can just provide methods to retrieve those results from a dialog.

    For example the typical usage of a wxFileDialog:

    Code:
    wxFileDialog dlg(blah blah);
    if (dlg.ShowModal() == wxID_OK) {
        wxString selection = dlg.GetPath();
       ...
    }
    However, I suspect the Close call is wrong and you should rather use EndModal to set the return value of ShowModal (e.g wxID_OK) - there should be also something analogous for non-modal dialogs.

    I also guess that you probably don't need to do anything in this handler (possibly you might not even need it as wxWidgets might treat a button with the id of wxOK as the default affirmative button. The controls would still be there even after the dialog is closed, so you can retrieve the values when they are asked for by special methods.

    All in all see the reference for wxDialog and read the discussion of its usage somewhere in the manual.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    So... I've moved the code from the dialog to the main program and set the buttons as a stddialogbuttonsizer. Show it just magically remember my variables? How do I refer to them then?

    I've already looked at the docs for wxdialog but it only refers to simple ok/cancel situations and not to something where someone is using 2 wxtextctrl inputs.

    Code:
    void _labelguiFrame::OnButton4Click(wxCommandEvent& event)
    {
        CustomSizeDialog dialog(this);
        dialog.ShowModal();
        if ( dialog.ShowModal() == wxID_OK ) {
            wxString dimx = TextDimX->GetValue();
            wxString dimy = TextDimY->GetValue();
            int dimxn = wxAtoi(dimx);
            int dimyn = wxAtoi(dimy);
            wxString dimcust;
            dimcust += dimx + wxT(" x ") + dimy;
        }
        //else: dialog was cancelled or some another button pressed
    
    }
    gives:
    error: `TextDimX' was not declared in this scope|
    error: `TextDimY' was not declared in this scope|

    I still don't understand how I'm supposed to pass them from the dialog to the main program.
    Last edited by mikeyp; 12-23-2009 at 03:18 AM.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You shouldn't move the code out of the dialog class. TextDimX and TextDimY should be members of the CustomSizeDialog, which should provide methods like GetDimX and GetDimY which might just retrieve the values from the controls.

    Alternatively, I suppose you could have two int members in CustomSizeDialog where you store the values when the Ok button is pressed and from where they can be retrieved using the GetDimX and GetDimY methods.

    In any case, you shouldn't be attempting to return two values from the dialog. What you should get returned from ShowModal is an integer value telling you how the user closed the dialog (e.g by calling EndModal(some_result) in the handler in case of a modal dialog).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    So really what you're saying is I've chosen the wrong way of going about this problem, and really what I need to do is think of something else? Maybe I should just keep the two boxes in the main frame...

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    ???

    I just think the usage of your dialog should be similar to the usage of the wxFileDialog shown above:

    Code:
    CustomSizeDialog sizeDlg(blah blah);
    if (sizeDlg.ShowModal() == wxID_OK) {
        int x = sizeDlg.GetDimX();
        int y = sizeDlg.GetDimY();
        ...
    }
    assuming the dialog is modal (non-modal dialogs are more of a pain, I think I have posted events when a button is pressed, in which case you can put any extra information in your event class).

    How you implement it is entirely up to you.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    27
    Sorry, I didn't mean that last post to come accross like that, I meant it to sound like a question because I was asking about where you said about the dialog returning one integer value. I only meant was I using the dialog for the wrong thing.
    I see what you mean now, thank you.

    I don't like being a programming newbie, every last thing is a horrible struggle and it's so hard to pick everything you need out of the plethora of information available.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 11:17 AM
  2. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  3. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 01:26 AM
  4. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-21-2003, 12:20 AM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21