|
I just compiled and ran that code, it works. But you already knew that.
Thank you Mihai.
The string to be converted will be extracted from a user dialog. The user may make an error in entering the number. Is there a method that will validate the string as being properly formatted or in some other way detect an input error to I can tell the user?
Thanks again.
Thanks for your time
|
|
|
|
|
Hi, for this task you should check out Ben Hanson's CFilterEdit[^] class. I use in one of my projects and I am gratefull to the author.
Regards,
Mihai Moga
|
|
|
|
|
RE: CFileterEDit, That class does look good. I downloaded it and the demo.
The file BaseEdit.h contains this reference:
#include <boost/regex.hpp>
This include file in not contained in either of the downloads. BaseEDit.h is the only file containing this include and it has no previous includes that lead me to the file.
Where do I find this file?
Thanks for your time
|
|
|
|
|
In order to recompile that project, you should consider downloading the boost::regex library from Boost's Official Page/[^].
Regards,
Mihai Moga
|
|
|
|
|
Hello Mahai,
I have installed the Boost environment, I have added FloatEdit and BaseEdit to my project, and they will compile. Now I see method
double CFloatEdit::GetValue () const
in file FloatEdit.cpp
But I don't see how to apply it to my dialog box.
One of the fields that I want filtered has the ID:
IDC_X_POSITION_EDIT
and string in the box can be accessed via the variable name as follows:
GBE_x_position_string.GetWindowTextW( str );
That puts the value into CString names str.
Can you tell me how to use this FloatEdit to check the inputs to this field?
Thanks for your time
|
|
|
|
|
Well, FloatEdit will do it for you, it filters data so that you would have a valid floating value/string. It's enought to subclass you CEdit control with this one.
Regards,
Mihai Moga
|
|
|
|
|
Thanks for your reply, but while the documentation tells that than I can use this class to filter the data, I am not able to see how to apply the class to do that. How do I use it?
Given that the text edit tool in my dialog has the id of
IDC_X_POSITION_EDIT
and the assigned variable of
GBE_x_position_string
with example usage: GBE_x_position_string.GetWindowTextW( str );
and variable str containg the string from said edit text tool:
Will you show me the line of code that invokes a method of FloatEdit to
1. validate the string as suitable for converting to float or double
and/or
2. provide the foat or double equvalent.
Thanks for your time
|
|
|
|
|
Hint: atof returns 0.0 whenever the conversion fails.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
But what if the value entered is 0.0. That may well be a valid entry. I could compare the source string with "0.0" to account for that one case, but that just seems ugly.
Thanks for your time
|
|
|
|
|
No it's NOT ugly and it is the way my suggestion pointed to. You have to deal only with 1 case and that's much more simpler than the general validation. Anyway you have to accomplish carefully your task. For instance 0 is a valid entry too (and, depending on your level of user-noise tolerance, also 0.00 , 0.000 ,... are).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Hello Pallini,
I really do appreciate the time you spent answering my question. But we do disagree on something. Elegant is something like this.
CString source = "1.23e6";
double target;
int status;
status = source.ConvertToFloat( &target );
If atof returns 0.0 in the double variable for an error, this it must also write 0.0 to the variable when the text is 0, or 0.0, or any of several other conditions as you have noted. In this event, either before the convert or after, we must have additonal code to check for any text string that would result in 0.0 to determine if we have an error, or if we really have a 0. Having to writing extra code to do that is indeed downright ugly.
Maybe I have missunderstood you. If so please help me out.
Thanks for your time
|
|
|
|
|
No, you haven't misunderstood me.
I mean atof , returning 0 on error, gives you the support to implement the elegant solution you proposed: it's a starting point.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Unfair vote[^]
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Already balanced, sir.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thank you [Military tone] Sirrrr! [/Military tone]
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
You can also use the functions isalfa () or isdigit ().
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Hello Nelek,
I had forgotton about those. But then I would need to write some code that checks the string one char at a time.
I think that the best way is to use the CFilterEdit. But that requires getting and installing the Boost package mentioned elsewhere. It does seem to be far better than any than any thing else I have see.
But think about this for a minute. All user interface with dialogs involve the user typing in a string and the programmer converting at needed. The interface between the dialog and the remainder of the program is a string, period. The very idea that any programmer needs to go to this amount of effort just to get a string ( CString ) converted to an integer, float, double or what have you, is just down right absurd.
Am I the only one that recognizes this. If so, maybe I am the crazy one. Let me know.
Meanwhile, following Nelek's note in his/her signature:
I am grateful for each and every one of you that responded. Maybe this will be documented here or somewhere so others can find it.
Indeed, which I get this straight, I will write it up and add it to my site here:
www.bkelly.ws/software
I am very much open to suggestions and if you have a page written, put your name in there showing credit to you and I will be happy to post it and give you credit.
But first, I want to get my program working.
Thank you.
Thanks for your time
|
|
|
|
|
bkelly13 wrote: in his/her signature
His :P
Actually the MFC let you set a member variable for a CEdit as number typ and it gives the error message when you are giving letters by itself. Just put the member variable as double insteads of CEdit*
Problem... when using APIs this can't be used.
But there is another way to do it, with the DDX or DDV (don't remember it well). I read in a manual that the checking can be automated using one of them and you can use the same validation for more than one control. But not sure about it because I was lazy and I used the other option (member variable as the type of number I needed)
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Have you considered a masked-edit control? Several exist here on CP's site.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
i have a GUI in which i will have to indicate my status and progresses and so i have embedded the progress bar in my statusbar using the following code inside a button click(show progress),but when i use this code twice,i mean if i click the showprogress button twice i get error in wincore at line 703...and the error and code are ..
void AFXAPI AfxHookWindowCreate(CWnd* pWnd)
{
_AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData();
if (pThreadState->m_pWndInit == pWnd)
return;
#ifndef _WIN32_WCE
if (pThreadState->m_hHookOldCbtFilter == NULL)
{
pThreadState->m_hHookOldCbtFilter = ::SetWindowsHookEx(WH_CBT,
_AfxCbtFilterHook, NULL, ::GetCurrentThreadId());
if (pThreadState->m_hHookOldCbtFilter == NULL)
AfxThrowMemoryException();
}
ASSERT(pThreadState->m_hHookOldCbtFilter != NULL);
#endif // !_WIN32_WCE
ASSERT(pWnd != NULL);
ASSERT(pWnd->m_hWnd == NULL);
ASSERT(pThreadState->m_pWndInit == NULL);
pThreadState->m_pWndInit = pWnd;
}
void Ctest::OnBnClickedButton4()
{
m_bar.Create(this);
m_bar.SetIndicators(indicators,2);
CRect rect;
GetClientRect(&rect);
m_bar.SetPaneInfo(0,progressbarn, SBPS_NORMAL,rect.Width()-100);
m_bar.SetPaneInfo(1,progressbarm,SBPS_STRETCH ,0);
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,
progressbarn);
pPrgCtrl->Create(WS_CHILD | WS_VISIBLE, CRect(10, 10, 288, 35), this,0x16);
pPrgCtrl->SetParent(&m_bar);
CRect crPaneRect;
m_bar.GetStatusBarCtrl().GetRect( 1, &crPaneRect );
crPaneRect.DeflateRect( 0, 2, 2, 2 );
pPrgCtrl->MoveWindow( crPaneRect );
s="showing progress ";
m_bar.SetPaneText(0,s);
}
what should be done??
modified on Saturday, June 21, 2008 5:40 AM
|
|
|
|
|
sanjayvenkat wrote: ,i mean if i click the showprogress button twice i get error in wincore at line 703...and the error and code are ..
That's because you are trying to create m_bar twice.
sanjayvenkat wrote: m_bar.Create(this); //We create the status bar
Try this...
if( !m_Bar.GetSafeHwnd() )
m_bar.Create(this);
Nibu babu thomas
Microsoft MVP for VC++
Code must be written to be read, not by the compiler, but by another human being.
Programming Blog: http://nibuthomas.wordpress.com
|
|
|
|
|
Hi All,
Is it possible to change width of a combo box at run time ?
ex:
I have data like
One
two
Thirteen
ThousandFourty
in the combo box.
whenever user select a string from above, combo box should be resized with the length of selected string.
Thanks..
Sheshidar P
|
|
|
|
|
Can you use of MoveWindow ?
|
|
|
|
|
|
I have a CTreeCtrl object in my dialog, this control is initialized as follows:
m_imgList.Create(16, 16, ILC_COLOR32, 1, 4);
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
m_imgList.Add(&bitmap, RGB(0, 0, 0));
bitmap.DeleteObject();
bitmap.LoadBitmap(IDB_BITMAP2);
m_nFolderImgNdx = m_imgList.Add(&bitmap, RGB(0, 0, 0));
bitmap.DeleteObject();
m_wndTree.SetImageList(&m_imgList, TVSIL_STATE);
hNewItem = m_wndTree.InsertItem(
TVIF_IMAGE | TVIF_PARAM | TVIF_STATE | TVIF_TEXT,
_T(""), 1, -1, nState,
TVIS_EXPANDED | TVIS_STATEIMAGEMASK,
(LPARAM)pData, hItem, TVI_LAST);
Later in the code I update each item of the tree control - text and image.
m_wndTree.SetItemImage(hItem, 0, -1);
However the tree control item does not update its image icon. It always display's its 1st image even I set the 0th image during the update. Any idea what is the problem ?
Thanks,
Abyss
|
|
|
|
|