473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

floating-point textbox

I want to use a Textbox to allow the user to input a floating point number.
In my case it will always be non-negative. Is there an easy way to do this,
or possibly a link to some sample code that shows how to do this?

Thanks in advance! : )

[==Peteroid==]
Nov 17 '05 #1
4 1778

"Peteroid" <pe************@msn.com> wrote in message
news:%2******************@TK2MSFTNGP12.phx.gbl...
I want to use a Textbox to allow the user to input a floating point number. In my case it will always be non-negative. Is there an easy way to do this, or possibly a link to some sample code that shows how to do this?

Thanks in advance! : )

[==Peteroid==]

Peteroid,

I did this a year ago and I don't have the code on hand, but essentially
I subclassed the text box and used the STL stringstream class to place the
float from the text box into the stream and then format the output back to
the text box. By doing this, you have control of the precision of the number
(which is correctly rounded) as well as user mistakes (e.g. "10.2k22") being
corrected to a usable float in the output to the text box ("10.2"). I
believe there is atof() conversion involved as well. If time frees up later,
I will send a source example.

HTH, M
Nov 17 '05 #2
Peteroid wrote:
I want to use a Textbox to allow the user to input a floating point number.
In my case it will always be non-negative. Is there an easy way to do this,
or possibly a link to some sample code that shows how to do this?

As far as I know you will have to pass the input string yourself. However
Char::GetNumericValue() may provide much help, it converts a character digit to its
corresponding number value as a double.
For example

Char::GetNumericValue ('4'); returns 4 as a double.
So parsing string "54.145679" can be done like this:

#using <mscorlib.dll>
int main()
{
using namespace System;

String *s= "54.145679";

double d=0;

for(int i= s->Length-1; i>=0; --i)
{
static long nonDecimals= 0;

if(s->Chars[i]=='.')
{
nonDecimals=1;
continue;
}

if(!nonDecimals)
{
d+= Char::GetNumericValue(s->Chars[i]);
d/= 10;
}

else if(nonDecimals)
{
d+= nonDecimals*Char::GetNumericValue(s->Chars[i]);
nonDecimals*=10;
}
}

Console::WriteLine(d);
}

Of course you should validate the contents of the string to be sure it is a valid decimal
number before the conversion, or check the return value of Char::GetNumericValue() (-1.0
in case of error).
Nov 17 '05 #3
Errata fixed:

Ioannis Vranos wrote:
As far as I know you will have to
parse
the input string yourself.
However Char::GetNumericValue() may provide much help, it converts a
character digit to its corresponding number value as a double.
For example

Char::GetNumericValue ('4'); returns 4 as a double.
So parsing string "54.145679" can be done like this:

#using <mscorlib.dll>
int main()
{
using namespace System;

String *s= "54.145679";

double d=0;

for(int i= s->Length-1; i>=0; --i)
{
static long nonDecimals= 0;

if(s->Chars[i]=='.')
{
nonDecimals=1;
continue;
}

if(!nonDecimals)
{
d+= Char::GetNumericValue(s->Chars[i]);
d/= 10;
}

else if(nonDecimals)
{
d+= nonDecimals*Char::GetNumericValue(s->Chars[i]);
nonDecimals*=10;
}
}

Console::WriteLine(d);
}

Of course you should validate the contents of the string to be sure it
is a valid decimal number before the conversion, or check the return
value of Char::GetNumericValue() (-1.0 in case of error).

Nov 17 '05 #4
#include <tchar.h> //for _tstof()
#include <sstream> //for stringstream
#include <iomanip> //for setprecision() and fixed

LRESULT CALLBACK NewTxtBoxWndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
TCHAR szBuffer[255];
std::basic_stringstream<TCHAR> strm;
double f;

switch(msg)
{
case WM_KILLFOCUS :
GetWindowText(hwnd, szBuffer, 200);
f = _tstof(szBuffer); //convert text to float

//Set format to display numbers as 0.0 (to the nearest tenth)
strm << std::setprecision(1) << std::fixed << f;

//Place formatted number back into text box
SetWindowText(hwnd, strm.str().c_str());

break;
}

return CallWindowProc(OldWndProc, hwnd, msg, wp, lp);
}
then in your main app code, you need to subclass the textbox.

WNDPROC OldWndProc;
oldWndProc = (WNDPROC) SetWindowLong(hwndTextBox, GWL_WNDPROC, (LONG)
NewTxtBoxWndProc);
Regards, M
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: George Hester | last post by:
At the time this suggestion was made I didn't have the wherewithall to even attempt it. But over time I learned enough to make a stab at it. Let just say the foating DIV had to provide the same...
3
by: JHR | last post by:
Hey all, I'm trying to make a sidebar box float to the right of various items, and for those items to wrap if a user shrinks his browser window. Instead, in every browser I've tried except for...
12
by: meltedown | last post by:
I would like the floating divs to float and then the header to come after them , on the left. That's what I thought clearing the floats was for, but in this example, the header is to the right of...
1
by: buliwyf_23 | last post by:
I have this: CSize sz; sz.cx = dwOutX1; sz.cy = dwOutY1; CPoint pt(sz); ClientToScreen(&pt);
6
by: Usenet | last post by:
I might be being silly here. On my links page I've got a whole load of floating boxes, which I'm really pleased with. But then I want the footer to be *below* them. On my current site...
1
by: hendrakieran | last post by:
Hi Guys, I'd like to get some help regarding creating a floating window with the following scenario: * I have index.html which is contains the frameset definition: <html> <head> </head>
2
by: Qiang | last post by:
Those who have used Google notebook may notice that google notebook displays the notes in a small floating window of the browser. I have tried to create a similar floating window, but with no luck....
0
by: gdrenfrew | last post by:
I'd like to know if it is possible to stop the parent application of a floating form becoming active when the controls on the floating form are clicked? I've got a large application, with multiple...
2
by: murali.desikan | last post by:
Hi, The definition of floating literal in the C++ (ISO/IEC 14882:2003) grammar is as follows (Note: I have replaced the "opt" subscript used in the standard with to indicate optional symbol)....
6
by: Jeremy | last post by:
I've got a floating div which becomes visible when a link is clicked. I want the div to be hidden when the user clicks anywhere on the page except for whithin the div. What is the best way to do...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.