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.
Hi guys i'm a beginner level wxwidgets programmer. i recently embarked on a small project that calculates your GPA based on Grades and Credit load and i stumbled upon a little bug, I use a 'OnTextCtrltext' function to automatically grab Values once input. my problem is in the case of the user making an error and deciding to click the backspace button, the previous values already previously calculated and the clearing of whats been previously input won't be recorded. I've tried looking up a function that reads a OnBackspace event but to no avail. here's some code to help explain what i mean.
You could catch the wxEVT_CHAR event (-> wxKeyEvent), but that shouldn't be necessary. The wxEVT_TEXT event should always be called when the value of the text field changes, that means also in response to backspace.
But i think there is a more serious flaw in the code: totalgrades gets incremented each time the value of the text field changes. E.g. if you change the value from A to B, it will increase over and over again. That can't be right. You need to keep a copy of the old, unmodified value at all time and then calculate the new value based on that.
On a side note: There is a lot of duplicate code in the small code fragment you've shown. That's usually a good indicator that things can get simplified.
// create upercase version of the string
wxString grade_str = TextCtrl27->GetValue().Upper();
// is string is not empty, get the first char
const char grade = grade_str.len() > 0 ? grade_str[0] : 0;
int factor = 0;
switch( grade )
{
case 'A': factor = 4; break;
case 'B': factor = 3; break;
case 'C': factor = 2; break;
case 'D': factor = 1; break;
// what about E?
case 'F': factor = 0; break;
}
totalgrades_new = totalgrades_old + (factor * credit26);
doublemax wrote: ↑Tue Apr 09, 2019 3:47 pm
You could catch the wxEVT_CHAR event (-> wxKeyEvent), but that shouldn't be necessary. The wxEVT_TEXT event should always be called when the value of the text field changes, that means also in response to backspace.
But i think there is a more serious flaw in the code: totalgrades gets incremented each time the value of the text field changes. E.g. if you change the value from A to B, it will increase over and over again. That can't be right. You need to keep a copy of the old, unmodified value at all time and then calculate the new value based on that.
My solution is much simpler and easier to follow.
Besides it is much better design - enter all the data required and then calculate. No calculator I know including a simple hand-help will do math on the fly - only whe the user press '=' sign on the keyboard.
doublemax wrote: ↑Tue Apr 09, 2019 3:47 pm
On a side note: There is a lot of duplicate code in the small code fragment you've shown. That's usually a good indicator that things can get simplified.
// create upercase version of the string
wxString grade_str = TextCtrl27->GetValue().Upper();
// is string is not empty, get the first char
const char grade = grade_str.len() > 0 ? grade_str[0] : 0;
int factor = 0;
switch( grade )
{
case 'A': factor = 4; break;
case 'B': factor = 3; break;
case 'C': factor = 2; break;
case 'D': factor = 1; break;
// what about E?
case 'F': factor = 0; break;
}
totalgrades_new = totalgrades_old + (factor * credit26);
That depends on the circumstances. Sometimes it might be useful to perform immediate updates when a value changes. Sometimes it makes sense to do it only when the focus changes. And sometimes you have an explicit "Apply" (or similar) button.
Without knowing the bigger picture, i didn't want to make a suggestion in any direction.
ONEEYEMAN wrote: ↑Tue Apr 09, 2019 2:18 pm
Hi,
This is a VERY BAD design.
You should have a button on you frame which will read "Calculate" or some something like this.
In the button pres event handler you will check whether all fields required hasw the values and then calculate the needed value.
You can filter the characters types in the EVT_TEXT event handler, but not the calculation.
Thank you.
Thanks Sir. What you said works but i guess you could say i'm trying to make the program as appealing as possible. If all else fails I'll get back to it.
Thanks Again.
doublemax wrote: ↑Tue Apr 09, 2019 3:47 pm
You could catch the wxEVT_CHAR event (-> wxKeyEvent), but that shouldn't be necessary. The wxEVT_TEXT event should always be called when the value of the text field changes, that means also in response to backspace.
But i think there is a more serious flaw in the code: totalgrades gets incremented each time the value of the text field changes. E.g. if you change the value from A to B, it will increase over and over again. That can't be right. You need to keep a copy of the old, unmodified value at all time and then calculate the new value based on that.
On a side note: There is a lot of duplicate code in the small code fragment you've shown. That's usually a good indicator that things can get simplified.
// create upercase version of the string
wxString grade_str = TextCtrl27->GetValue().Upper();
// is string is not empty, get the first char
const char grade = grade_str.len() > 0 ? grade_str[0] : 0;
int factor = 0;
switch( grade )
{
case 'A': factor = 4; break;
case 'B': factor = 3; break;
case 'C': factor = 2; break;
case 'D': factor = 1; break;
// what about E?
case 'F': factor = 0; break;
}
totalgrades_new = totalgrades_old + (factor * credit26);
Thanks Sir. Guess the switch function could work there too. thanks again for the extra functions, can't wait to test them out.
doublemax wrote: ↑Tue Apr 09, 2019 3:47 pm
You could catch the wxEVT_CHAR event (-> wxKeyEvent), but that shouldn't be necessary. The wxEVT_TEXT event should always be called when the value of the text field changes, that means also in response to backspace.
But i think there is a more serious flaw in the code: totalgrades gets incremented each time the value of the text field changes. E.g. if you change the value from A to B, it will increase over and over again. That can't be right. You need to keep a copy of the old, unmodified value at all time and then calculate the new value based on that.
On a side note: There is a lot of duplicate code in the small code fragment you've shown. That's usually a good indicator that things can get simplified.
// create upercase version of the string
wxString grade_str = TextCtrl27->GetValue().Upper();
// is string is not empty, get the first char
const char grade = grade_str.len() > 0 ? grade_str[0] : 0;
int factor = 0;
switch( grade )
{
case 'A': factor = 4; break;
case 'B': factor = 3; break;
case 'C': factor = 2; break;
case 'D': factor = 1; break;
// what about E?
case 'F': factor = 0; break;
}
totalgrades_new = totalgrades_old + (factor * credit26);
Tried this, multiple errors, fixed some but got stuck at this:" error: switch quantity not an integer"