Page 1 of 1

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

Posted: Sat Feb 10, 2007 7:03 pm
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.

Posted: Sat Feb 10, 2007 7:17 pm
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).

Posted: Sun Feb 11, 2007 11:16 am
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?

Posted: Sun Feb 11, 2007 2:42 pm
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

Posted: Thu Feb 15, 2007 2:22 pm
by krisje8
[quote]
krisje8@localhost ~/CircuitBuddy $ g++ *.cpp `wx-config --libs --cxxflags` -o circuitbuddy
circuitbuddyDlg.cpp: In member function

Posted: Thu Feb 15, 2007 2:28 pm
by biplab
OK. Just change your int variables to long int. That would solve the problem. :)

Posted: Thu Feb 15, 2007 2:34 pm
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);

Posted: Thu Feb 15, 2007 9:01 pm
by Auria
[quote]error: invalid conversion from

Posted: Fri Feb 16, 2007 6:13 pm
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?

Posted: Fri Feb 16, 2007 6:52 pm
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?

Posted: Fri Feb 16, 2007 7:39 pm
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.

Posted: Sat Feb 17, 2007 8:05 am
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...

Posted: Sat Feb 17, 2007 8:24 am
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.

Posted: Sat Feb 17, 2007 10:10 am
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 ?

Posted: Sat Feb 17, 2007 10:28 am
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. :)