Howto set a value in a WxEdit & how to GetValue into an

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
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Howto set a value in a WxEdit & how to GetValue into an

Post by krisje8 »

Ok, I made this code, some of it looks like it should look, some of it is made up by me, because I don't know how to do it.

Code: Select all

int SupV;
    SupV=WxEditLedresistor1->GetValue();
    int LedV;
    LedV=WxEditLedresistor2->GetValue();
    int LedmA;
    LedmA=WxEditLedresistor3->GetValue();
    int Lednr;
    Lednr=WxEditLedresistor4->GetValue();

    int ResV;
    ResV=SupV-LedV;
    int LedA;
    LedA=LedmA/1000;
    int Resistance;
    Resistance=ResV/LedA;
    SetValue()->WxEditLedresistor5=Resistance;
Could someone help me how to do this? I don't need a whole rewrite of the code, a nudge in the right direction is good enough.
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
Sunsawe
Experienced Solver
Experienced Solver
Posts: 54
Joined: Tue Jan 30, 2007 5:04 pm

Post by Sunsawe »

I didn't look at everything, but this seems strange...

Code: Select all

SetValue()->WxEditLedresistor5=Resistance;
I would have done that instead:

Code: Select all

WxEditLedresistor5->SetValue(Resistance);
Assuming that WxEditLedresistor5 is an instance of an object that you have declared before. You will also have to parse Resistance in the expected type of the method setValue ( if it is not an int in your case).
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

OK I did that, and I 'corrected' some of the rest of the code.

Code: Select all

    const Char* SupV;
    SupV=WxEditLedresistor1->GetValue();
    atoi(&SupV);
    const Char* LedV;
    LedV=WxEditLedresistor2->GetValue();
    atoi(&LedV);
    const Char* LedmA;
    LedmA=WxEditLedresistor3->GetValue();
    atoi(&LedmA);
    const Char* Lednr;
    Lednr=WxEditLedresistor4->GetValue();
    atoi(&Lednr);

    int ResV;
    ResV=SupV-LedV;
    int LedA;
    LedA=LedmA/1000;
    int Resistance;
    Resistance=ResV/LedA;
    WxEditLedresistor5->SetValue(Resistance);
Now obviously this doesn't work, but how do I make it work?
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

First Problem is you can't use C++ strings directly with wxTextCtrls. You've to use wxString.

Code: Select all

wxString SupV;
SupV=WxEditLedresistor1->GetValue();
Now atoi returns an Integer. So you need an Integer variable to be declared. So you can't use SupV as char * and then redefine it again as int.

So replace the following code

Code: Select all

    const Char* SupV;
    SupV=WxEditLedresistor1->GetValue();
    atoi(&SupV);
with

Code: Select all

    wxString SupV;
    int iSupV;
    SupV=WxEditLedresistor1->GetValue();
    SupV.ToLong(&iSupV);
Now do the same with others similar parts.

Now as you've converted the string to integer, you need to change it wxString again before feeding it to SetValue(). Change the following code

Code: Select all

    WxEditLedresistor5->SetValue(Resistance);
to

Code: Select all

    WxEditLedresistor5->SetValue(wxString::Printf(wxT("%d"), Resistance));
Also remember to not redefine a variable (with same name) within a scope, i.e., within a function.

Hope this solves your problem.

Regards,

Biplab
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

[quote]
krisje8@localhost ~/CircuitBuddy $ g++ *.cpp `wx-config --libs --cxxflags` -o circuitbuddy
circuitbuddyDlg.cpp: In member function
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

OK. Just change your int variables to long int. That would solve the problem. :)
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

Also change the following line

Code: Select all

WxEditLedresistor5->SetValue(wxString::Printf(wxT("%d"), Resistance));
To

Code: Select all

wxString msg;
msg.Printf(wxT("%d"), Resistance);
WxEditLedresistor5->SetValue(msg);
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
Auria
Site Admin
Site Admin
Posts: 6695
Joined: Thu Sep 28, 2006 12:23 am
Contact:

Post by Auria »

[quote]error: invalid conversion from
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

Ok, it compiles now, but when I press this button (that's when this code is run) it just shuts down my app.
In Konsole I get this error: Floating point exception .
So, the long int should be a float? How do I do this?
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

I read about casting now, and I solved the float error.

Code: Select all

    wxString SupV;
    long int iSupV;
    SupV=WxEditLedresistor1->GetValue();
    SupV.ToLong(&iSupV);
    wxString LedV;
    long int iLedV;
    LedV=WxEditLedresistor2->GetValue();
    LedV.ToLong(&iLedV);
    wxString LedmA;
    long int iLedmA;
    LedmA=WxEditLedresistor3->GetValue();
    LedmA.ToLong(&iLedmA);
    wxString Lednr;
    
    float ResV;
    float fSupV;
    fSupV = float(iSupV);
    float fLedV;
    fLedV = float(iLedV);
    ResV=fSupV-fLedV;
    float LedA;
    float fLedmA;
    fLedmA = float(iLedmA);
    LedA=fLedmA/1000;
    float Resistance;
    Resistance=ResV/LedA;
    wxString Resistancewx;
    Resistancewx.Printf(wxT("%d"), Resistance);
    WxEditLedresistor5->SetValue(Resistancewx);
It compiles, it runs, but when I press the result is 0 (literally).
Why does this happen, and how do I solve it?
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

Ok, it may happen if you enter something like 0.4 in textbox, it will be converted to 0 while making it an int. That could be the problem.

Change your code (one example shown)

Code: Select all

    wxString SupV;
    long int iSupV;
    SupV=WxEditLedresistor1->GetValue();
    SupV.ToLong(&iSupV);
To

Code: Select all

    wxString SupV;
    double iSupV;
    SupV=WxEditLedresistor1->GetValue();
    SupV.ToDouble(&iSupV);
Make similar changes in other parts, too.

Also replace all floats with double to avoid any unnecessary warnings. :)

I hope this would solve your problem.
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

Ok, the code is this now:

Code: Select all

    wxString SupV;
    double iSupV;
    SupV=WxEditLedresistor1->GetValue();
    SupV.ToDouble(&iSupV);
    wxString LedV;
    double iLedV;
    LedV=WxEditLedresistor2->GetValue();
    LedV.ToDouble(&iLedV);
    wxString LedmA;
    double iLedmA;
    LedmA=WxEditLedresistor3->GetValue();
    LedmA.ToDouble(&iLedmA);
    
    double ResV;
    ResV=iSupV-iLedV;
    double LedA;
    LedA=iLedmA/1000;
    double Resistance;
    Resistance=ResV/LedA;
    wxString Resistancewx;
    Resistancewx.Printf(wxT("%d"), Resistance);
    WxEditLedresistor5->SetValue(Resistancewx);
But still, the same problem, 0 is the result...
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

Change the following line

Code: Select all

    Resistancewx.Printf(wxT("%d"), Resistance);
to

Code: Select all

    Resistancewx.Printf(wxT("%f"), Resistance);
%d is to be used for integer.

The result being 0 could be due to other causes, too. Try to check the value of each variable in a Debugger after each calculation.

Otherwise use this simple code. Define the following variable at top but only once.

Code: Select all

    wxString msg;
Then add the last two line of following code (please do change the variable name) after each variable is converted from wxString and after any calculation. (Following code is an example)

Code: Select all

    SupV.ToDouble(&iSupV);
    msg.Printf(wxT("%f"), iSupV);
    wxMessageBox(msg);
This would give you a fair idea about the area where it is getting wrong.
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
krisje8
Earned a small fee
Earned a small fee
Posts: 16
Joined: Sat Oct 28, 2006 1:24 pm
Location: The Netherlands
Contact:

Post by krisje8 »

biplab wrote:Change the following line

Code: Select all

    Resistancewx.Printf(wxT("%d"), Resistance);
to

Code: Select all

    Resistancewx.Printf(wxT("%f"), Resistance);
%d is to be used for integer.
That did the trick thank you!
Before I'll accept your answer, I have some other questions for you :p :
The number comes out like this: 290.000000 .
There are 6 digits behind the dot, can this be reduced to somthing like 2 or 3 digits?
And how can I change the dot into a comma?
And then a final question, totally off-topic: how do I load Images into the app depending on the Resistance ?
"When you say 'I wrote a program that crashed Windows,' people just stare at you blankly and say 'Hey, I got those with the system, *for free*'." - Linus Torvalds
biplab
I live to help wx-kind
I live to help wx-kind
Posts: 194
Joined: Fri Feb 17, 2006 4:16 am
Location: Singapore
Contact:

Post by biplab »

It's nice to know that it's working. :)

You can print 2-3 chars after decimal by changing the following line-

Code: Select all

    Resistancewx.Printf(wxT("%f"), Resistance);
To

Code: Select all

    Resistancewx.Printf(wxT("%.2f"), Resistance);
Here, .2f will direct the computer to print 2 numbers after decimal. Just add the number of digits you want after dot. You can play with it. ;)

To know more about controlling the formatted output of printf function, please read the following article.
You can use wxString's Replace() function to do so. Add another line as I've shown. :)

Code: Select all

    Resistancewx.Printf(wxT("%.2f"), Resistance);
    Resistancewx.Replace(wxT("."), wxT(","));
Regarding Image loading, I didn't understand your problem. You can load image using wxStaticBitmap class. :)
Blog: http://biplab.in

IDE: Code::Blocks
Compilers: GCC, MSVC, etc. ;)
OS: WinXP-SP2 & Linux.
Post Reply