Re: Best Practice of verify a string in Edit control of MFC
On Feb 4, 6:34 pm, "Morris" <mor...@online.nospam> wrote:
HI
In a MFC dialog, I have an CEdit control. I need to
1. Verify its value to be a valid float when a button in the dialog is
clicked.
2. Convert the value to a float variable if 1 succeeds
3. Limit the length of the CEdit control to allow only 8 characters.
Your helps are appreciated.
Morris.
DDX_Text with float& should work.
It should be noted that it's a bit substandard WRT conversion: for
example "12.3 abc" may be not what you want, but it will be accepted.
I don't remember if there are any localization issues with DDX_Text
(e.g. what is decimal, and what is thousands separator character) -
there might be. You may need to call setlocale yourself
Recently, I have grown to think that simply going to Windows is also
quite good, e.g.:
COleVariant v(GetWindowText(IDC_XYZ));
HRESULT hr = VariantChangeType(&v, &v, VT_R4);
if (FAILED(hr))
{
if (hr == DISP_E_TYPEMISMATCH)
NotAFloat(); // maybe pDX->Fail() ?
else
OhShitSomethingIsReallyBad(); // Ain't gonna happen, don't
fret ;-)
}
IIRC, localization is in this case taken in account immediately, (IIRC
not so if you rely on MFC, which relies on CRT for that - there, it's
decided on startup).
WRT 8 characters... Why? In ________123, you have a number, although
it's more than 8 characters (by _ I mean space). Don't you rather
worry about the range?
HTH,
Goran.