如何将文本框值分配给int var? [英] how a textbox value can be assigned to an int var?

查看:113
本文介绍了如何将文本框值分配给int var?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在c#中使用Windows窗体制作应用程序.

我必须这样做

i m making an application using windows form in c#.

i have to do this

int quan = textbox.Text;



但是偏离路线会产生错误,因此请告诉我如何将文本框值分配给int var?



but offcourse this gives error so tell me how a textbox value can be assigned to an int var?

推荐答案

您必须使用int.Parse [ int.TryParse [ ^ ]

You have to use either int.Parse[^] or int.TryParse[^]

int quan = 0;
quan = int.Parse(textbox.Text);

这会将整数值放入"quan"中,如果转换有问题,则会引发异常.

This puts the integer value into "quan" or throws an exception if there are problems with the conversion.

int quan = 0;
if (int.TryParse(textbox.Text, out quan))
   {
   ...
   }

这会将整数值放入"quan"并返回true,或者如果转换存在问题则返回false.

This put the integer value into "quan" and returns true, or returns false if there are problems with the conversion.


int.Parse或int.TryParse将解决问题!

干杯!

--MRB
int.Parse or int.TryParse will do the trick!

Cheers!

--MRB


string a = textbox.text.Tostring();
int ABC =(int)a;
string a = textbox.text.Tostring();
int ABC = (int) a;


这篇关于如何将文本框值分配给int var?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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