如何将整数转换为字符串并在texbox中计算结果 [英] How to convert integer to string and calculate result in texbox

查看:86
本文介绍了如何将整数转换为字符串并在texbox中计算结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有文本框,我不会将文本框中的值相乘,然后使用此文本框在textbox_textChange中显示结果()

接线出现在x变量中

这里我的代码..



我的尝试:



I have textbox and I wont to multiply the value in textbox and then use this textbox to show result in textbox_textChange()
the wiring Appears in x variable
here my code..

What I have tried:

private void textBox1_TextChanged(object sender, EventArgs e)
      {

          int x = int.Parse(textBox1.Text) * 350;

          textBox1.Text = x ;

      }

推荐答案

textBox1.Text = x.ToString() ;



使用 Int32.ToString方法 [ ^ ]或转换.ToString方法 [ ^ ]



注意:使用 Int32.TryParse方法 [ ^ ]以避免格式异常无效


use Int32.ToString Method [^] or Convert.ToString Method [^]

Note: use Int32.TryParse Method [^] to avoid invalid format exception


你应该实际使用 two 文本框:第一个保留的t o用户输入和第二个计算输出。
You should actually use two textboxes: the first reserved to user input and the second to computation output.


这不能简单地使用 TextChanged 事件来完成,因为更改内容会再次举起活动。



通常使用另一个文本框来显示结果或提供一个按钮来执行计算。



你还应该使用 Int32.TryParse方法(String,Int32)(系统) [ ^ ]处理无效输入。



如果你还想使用同一个盒子,你必须使用防护变量:

This can't be simply done using the TextChanged event because changing the content would raise the event again.

It is common to use another text box to show the result or provide a button to perform the calculation.

You should also use the Int32.TryParse Method (String, Int32) (System)[^] to handle invalid input.

If you still want to use the same box, you have to use a guarding variable:
bool selfUpdate = false;

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (!selfUpdate)
    {
        int x;
        if (Int32.TryParse(textBox1.Text, out x))
        {
            x *= 350;
            selfUpdate = true;
            textBox1.Text = x.ToString();
            selfUpdate = false;
        }
    }
}





但请注意,使用上述每项更改时将更新,以便只能输入一位数字或必须粘贴文字。

[/ EDIT]



But note that when using the above each change will update so that only single digits can be entered or text must be pasted.
[/EDIT]


这篇关于如何将整数转换为字符串并在texbox中计算结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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