Form problem... Topic is solved

If you are using wxDev-C++ for your wxWidgets design, please ask your questions here instead of in IDE Related.
Post Reply
rocco.g
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Feb 25, 2006 9:48 pm
Location: Italy
Contact:

Form problem...

Post by rocco.g »

Hi!
I'm italian, so i'm sorry for my English.

I did a program that add the string written in the first form at the second one and show the result in the third form when the button is pressed.

But there is a little problem, to do this i used this function:


Code: Select all

    WxButton1 = new wxButton(this, LboxTest_Add, wxT("Somma"), wxPoint(170,124), wxSize(75,25), 0, wxDefaultValidator);
    
    WxEdit2 = new wxTextCtrl(this, LboxTest_AddText, wxT("Num 1"), wxPoint(20,30), wxSize(121,21), 0, wxDefaultValidator );
    
    WxEdit1 = new wxTextCtrl(this, ID_WXEDIT1, wxT(""), wxPoint(20,80), wxSize(121,21), 0, wxDefaultValidator );

    WxEdit3 = new wxTextCtrl(this, ID_WXEDIT3, wxT("Num 2"), wxPoint(20,55), wxSize(121,21), 0, wxDefaultValidator );

}

void Project1Frm::Project1FrmClose(wxCloseEvent& event)
{
    Destroy();
}
 
 
void Project1Frm::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
{
    wxString s = WxEdit2->GetValue();
    
   
    
    wxString a = WxEdit3->GetValue();
  

    WxEdit1->AppendText(s+a);
}
but i would like to sum the integer typed in the form 1 with the integer typed in the form 2 and to show the result in the third form and not to use the strings... what i have to do in order to sum or do some other mathematical operations instead to use string characters?

Can u help me?
i searched on google and on the ufficial wx reference, but i didn't find anything usefull...

please help me...

thanks and sorry for my English...
Last edited by rocco.g on Fri Apr 14, 2006 1:16 pm, edited 1 time in total.
Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T »

Have you looked at using wxString::ToDouble or ::ToLong? To convert the strings into floating point or integer numbers. Then multiply them and put the final value back into the editbox?

Sof.T
rocco.g
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Feb 25, 2006 9:48 pm
Location: Italy
Contact:

Post by rocco.g »

hi! thanks for ur answer!

i already tried to use this function:
wxString::ToLong bool
ToLong(long *val, int base = 10) const


but AppendText works with the strings and not with the integer, so i have always the string and not the integer...

i tried to convert the first string s, by doing this:


Code: Select all

wxString s = WxEdit2->GetValue();
    
    bool ToLong(long *s, int base = 10);
   
    
   // wxString a = WxEdit3->GetValue(); disabled for this test
  

   // WxEdit1->AppendText(s+a);  disabled for this test
    
    WxEdit1->AppendText(s);
but it displays always only the string... and do not do the sum if i convert also the second string... it do only the sum of the two strings...

:cry:
elmo
Experienced Solver
Experienced Solver
Posts: 56
Joined: Mon Dec 26, 2005 9:23 pm
Location: Poland, Warsaw

Re: Integer and Maths

Post by elmo »

Code: Select all

 
void Project1Frm::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
{
    long b,c;

    wxString s = WxEdit2->GetValue();
    wxString a = WxEdit3->GetValue();
  
    s->ToLong(&b);
    a->ToLong(&c);

    WxEdit1->AppendText( wxString::Format(wxT("%ld"), b+c) );
}
Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T »

Just posting a reply when I noticed Elmo got there first :lol:

Can't better his solution, it is very neat :wink:

Sof.T
rocco.g
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Feb 25, 2006 9:48 pm
Location: Italy
Contact:

Post by rocco.g »

thanks!!!

now it works!

but i had to use this:

Code: Select all

s.ToLong(&b); 
a.ToLong(&c); 
it works correctly... but it is correct?
Sof_T
Can't get richer than this
Can't get richer than this
Posts: 864
Joined: Thu Jul 28, 2005 9:48 pm
Location: New Forest, United Kingdom
Contact:

Post by Sof_T »

s.ToLong(&b);
a.ToLong(&c);
Yes it is correct. I think it was a typing error on elmo's part.

It is possible to leave out the assignment to temporary strings like so:

Code: Select all

    long b,c;

    WxEdit2->GetValue().ToLong(&b);
    WxEdit3->GetValue().ToLong(&c);

    WxEdit1->AppendText( wxString::Format(wxT("%ld"), b+c) );
Sof.T
rocco.g
Knows some wx things
Knows some wx things
Posts: 37
Joined: Sat Feb 25, 2006 9:48 pm
Location: Italy
Contact:

Post by rocco.g »

ok!!!

thanks a lot to all!!!


really... thanks!!!

:wink:
elmo
Experienced Solver
Experienced Solver
Posts: 56
Joined: Mon Dec 26, 2005 9:23 pm
Location: Poland, Warsaw

Post by elmo »

Sof_T wrote:Yes it is correct. I think it was a typing error on elmo's part.
Yes, my mistake. I almost always create new instances with new operator and work on pointers.
MACKRACKIT
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 03, 2005 7:12 pm

Post by MACKRACKIT »

I am lerning also. I think I understand with the exception of how do you get 1.2 + 1.2 = 2.4 type of output?

I played around with double and float instead of long. Double gives strange answers and I can not get float to compile.

Also, I do not understand

(wxT("%ld")

I have read about wxT in the help files, but can not find "%ld"

Thanks
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina »

Also, I do not understand

(wxT("%ld")
The "d" means integer (aka type int). The "l" modifies it to a long integer (aka type long). By long integer, I mean that if a normal integer has a maximum value of 2^31 -1 (32 bits), then a long integer has a maximum value of 2^63 -1 (64 bits).

For double and float to work, you need to change the output string to

wxT("%f") for float (32 bits) and wxT("%lf") for double (which is a long or double-sized float -- usually 64 bits).


This is standard C/C++ format and should be taught the first day of any intro to programming course (or any intro to C/C++ book on the market).

Do a google search for "printf C" and you'll see the %d, %f, %s format definitions.

-Tony
Everybody's got something to hide except for me and my monkey.
MACKRACKIT
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 03, 2005 7:12 pm

Post by MACKRACKIT »

I thank you for your answer.

I did not know where to look as I have not had a course in C or C++. I do have two books and one of them does not mention "printf".

I am a forty something electrical engineer trying to learn C/C++. Have been using different flavors of basic for years though. Most likely will not go back to school.

Do you recomend a good book or two??

Thank you and every one else for your help.
tbreina
Moderator
Moderator
Posts: 1289
Joined: Thu Nov 25, 2004 7:38 pm
Location: Coronado, CA

Post by tbreina »

http://www.cplusplus.com/ref/cstdio/printf.html

Here's a good resource for formatting strings.

-Tony
Everybody's got something to hide except for me and my monkey.
MACKRACKIT
In need of some credit
In need of some credit
Posts: 6
Joined: Wed Aug 03, 2005 7:12 pm

Post by MACKRACKIT »

Thank you,
Dave
emarti
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 210
Joined: Sat May 07, 2005 8:24 pm
Location: Eskisehir, TURKEY
Contact:

Post by emarti »

For string to integer, you may use wxAtoi.
- T U R K E Y ?
- I love this country!

WebSites:
http://mebt.sourceforge.net/
http://wxquran.sourceforge.net/
Post Reply