将文本框字符串转换为float? [英] Converting textbox string to float?

查看:659
本文介绍了将文本框字符串转换为float?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上试图在visual studio 2008中编写一个基本的转换器,我有两个文本框,一个从用户输入,一个给出输出结果。当我按下按钮时,我希望第一个文本框中的输入乘以4.35,然后显示在第二个文本框中。这是我的代码在按钮代码到目前为止:

  String ^ i1 = textBox1-& 
float rez =(i1 * 4.35)ToString;
textBox2-> Text = rez;但是我得到这些错误:


f:\microsoft visual studio 9.0 \projects\hellowin\hellowin\Form1.h(148):error C2676:binary'*':'System :: String ^'不定义此运算符或转换为预定义运算符可接受的类型
f:\microsoft visual studio 9.0 \projects\hellowin\hellowin\Form1.h(148):错误C2227: ' - > ToString'的左边必须指向类/ struct / union / generic类型
f:\microsoft visual studio 9.0 \projects\hellowin\hellowin\Form1.h(149):error C2664 :'void System :: Windows :: Forms :: Control :: Text :: set(System :: String ^)':无法将参数1从'float'转换为'System :: String ^'

请帮助我疯狂地想从C ++中的文本框获取一些输入是多么可笑的困难。

解决方案

我已经找到了所有的错误,没有什么有用的东西,

为您修复它,

  String ^ i1 = textBox1-& 
float rez =(float)(Convert :: ToDouble(i1)* 4.35);
textBox2-> Text = rez.ToString();

基本上,你想将字符串转换为实际数字,做数学,返回到显示目的的字符串。


I'm basically trying to write a basic converter in visual studio 2008, and I have 2 text boxes, one which gets input from the user, and one which gives output with the result. When I press the button I want the input from the first textbox to multiply by 4.35 then display in the 2nd textbox. This is my code in the button code so far:

             String^ i1 = textBox1->Text;
             float rez = (i1*4.35)ToString;
             textBox2->Text = rez;

However I'm getting these errors:

f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2676: binary '*' : 'System::String ^' does not define this operator or a conversion to a type acceptable to the predefined operator
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(148) : error C2227: left of '->ToString' must point to class/struct/union/generic type
f:\microsoft visual studio 9.0\projects\hellowin\hellowin\Form1.h(149) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'float' to 'System::String ^'

Please help I'm going insane on how ridiculously difficult it is to get some input from a textbox in C++. I've googled every error I had and nothing useful came up, I've been searching answers for an hour already, please help.

解决方案

Fixing it for you,

         String^ i1 = textBox1->Text;
         float rez = (float)(Convert::ToDouble(i1)*4.35);
         textBox2->Text = rez.ToString();

Basically, you want to convert your string to an actual number, do the math, and then make it back into a string for displaying purposes.

这篇关于将文本框字符串转换为float?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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