Print variable on message box

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Ago
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 02, 2006 3:15 am

Print variable on message box

Post by Ago » Thu Feb 02, 2006 3:18 am

Hello

I am a complete newbie in wxWidget and c++ and i am trying to create my first GUI application. I would like to know if it is possible to print the value of a variable (double, int) on a wxMessageBox and if yes how? And something else if for example i try to use a function like wxMessageBox and i want to enter the first and the third parameter of the function how can i omit the second? ex. I want to show a message, i don't want a caption and i want to show some buttons.
Thank you

cpp
I live to help wx-kind
I live to help wx-kind
Posts: 195
Joined: Wed Sep 28, 2005 9:42 pm

Post by cpp » Thu Feb 02, 2006 5:35 am

Heres an example:

Code: Select all

int myVar = 15;
wxString msg = wxString::Format(wxT("my value is %d"), myVar);
wxMessageBox(msg, wxT("a message"), wxOK | wxCENTRE | xICON_ERROR, this);
with wxString::Format() you can display almost anyting as a string, using the correct format specifyer.

as for ommiting arguments, well, you cant precisely "ommit" them, but you can write their default values.

HTH
Hier Kommt die Sonne...

Ago
In need of some credit
In need of some credit
Posts: 4
Joined: Thu Feb 02, 2006 3:15 am

Post by Ago » Thu Feb 02, 2006 3:09 pm

Unfortunately is not working
It gives me this error and hangs up during execution.

main.cpp:86: warning: cannot pass objects of non-POD type `class wxString' through `...'; call will abort at runtime

I have three double variables one for storing the result of a formula. I want to find the result store it inside the variable convert it to wxString or an array and then print it on a messageBox

toxicBunny
Super wx Problem Solver
Super wx Problem Solver
Posts: 424
Joined: Tue Jul 12, 2005 8:44 pm
Location: Alabama, USA

Post by toxicBunny » Thu Feb 02, 2006 3:46 pm

Ago wrote:Unfortunately is not working
It gives me this error and hangs up during execution.

main.cpp:86: warning: cannot pass objects of non-POD type `class wxString' through `...'; call will abort at runtime

I have three double variables one for storing the result of a formula. I want to find the result store it inside the variable convert it to wxString or an array and then print it on a messageBox
I assume you're using gcc to compile your application since I've seen that warning before. Try changing the call to this.

Code: Select all

wxMessageBox(msg.mb_str(), wxT("a message"), wxOK | wxCENTRE | xICON_ERROR, this);
-Scott
wxMSW 2.6.2, VS 2002, 2003 and 2005, Code::Blocks and mingw, Windows XP Pro

phlox81
wxWorld Domination!
wxWorld Domination!
Posts: 1387
Joined: Thu Aug 18, 2005 7:49 pm
Location: Germany
Contact:

Post by phlox81 » Thu Feb 02, 2006 3:46 pm

For double you need %f instead of %d, and the variable itself.
You cannot use selfdefined types in Format.

djmccorrie
In need of some credit
In need of some credit
Posts: 3
Joined: Mon Feb 20, 2006 9:30 pm

wxString

Post by djmccorrie » Mon Feb 20, 2006 9:42 pm

another similar problem

Code: Select all

wxString msg = wxString::Format( wxT(" User is '%s' "), wxE_UserName->GetValue() );
because Format is defined with variable arguments

Code: Select all

static wxString Format(const wxChar *format, ...);
... can only be replaced by a non-POD type variable ie char* you must use .c_str() on all other wxString variables other than the first (think thats right)?

Code: Select all

wxString msg = wxString::Format( wxT(" User is '%s' "), wxE_UserName->GetValue().c_str() );
this works for me

Post Reply