如何从TextBox(编辑控件)获取int值(数字)作为输入。 [英] How to get a int value(number) as a Input from TextBox(Edit Control).

查看:369
本文介绍了如何从TextBox(编辑控件)获取int值(数字)作为输入。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从编辑框和代码内部给出数字输入我想要读取我给出的值。



如果我使用getwindowText它正在变为一个CString,但我想作为一个int。





有人知道吗??

I want to give numerical Input from Edit Box and Inside the code I want to read the value which I have given.

If I use getwindowText It's getting as a CString , but I want as a int.


Does anybody know ??

推荐答案

C标准库提供了两个将字符串转换为整数的函数: atoi [ ^ ]和 strtol [ ^ ]。



示例:

The C standard library provides two functions to convert strings to integers: atoi[^] and strtol[^].

Example:
CString str = control.GetWindowText();
int n = _tstoi(str.GetString());


此外Jochen说你可能会考虑使用 std :: stringstream std :: strstream 进行转换。尽管在1998年被弃用[1] strstream 在这方面相当不错,因为你不需要复制数据。类似于:



In addition to what Jochen said you might like to consider using std::stringstream or std::strstream to do the conversion. Despite being deprecated in 1998 [1] strstream is quite good at this sort of thing as you don't need to copy the data. Something like:

int number = 0;
std::strstream( number_string.GetString() ) >> number;





其中 number_string CString 效果很好。



[1]但它仍然没有从标准中移除,可能是因为像我这样的人喜欢它用于从固定大小的缓冲区中提取数据。



where number_string is a CString works quite well.

[1] But it still hasn't been removed from the standard, probably 'cause people like me like it around for extracting data from fixed size buffers.


这篇关于如何从TextBox(编辑控件)获取int值(数字)作为输入。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆