如何在一个 TextBox 中进行算术运算? [英] How can i do arithmetic operations in one TextBox?
问题描述
例如我写在 TextBox1
4*5
或 3-2
如何让答案出现在同一个文本框中?
how can make the answer appear in the same textbox ?
我试过了,但还是不行
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
textbox1.text = val(textbox1.text)
end sub
它只显示第一个数字
推荐答案
这是一项相当复杂的任务,在 VB.NET 语言中没有内置支持.第一步是将文本解析为表达式树.第二步是评估该表达式树以确定结果.对于初学者来说,这项任务当然是一项高级任务,但与其自己做,还有其他一些选择.您可以使用现有的第三方库为您完成所有繁重的工作,例如免费的开源 NCalc 库.或者,您可以使用 .NET 的 CodeDom 类 将表达式动态构建为 .NET 库,然后调用它以获取结果.
This is a rather complex task for which there is no built-in support in the VB.NET language. The first step is to parse the text into an expression tree. The second step is to evaluate that expression tree to determine the result. The task is certainly an advanced one for beginners, but rather than doing it yourself, there are a couple other options. You could use an existing third-party library which does all of the heavy lifting for you, such as the free open-source NCalc library. Or, you could use .NET's CodeDom classes to dynamically build the expression as a .NET library and then call it to get the result.
这篇关于如何在一个 TextBox 中进行算术运算?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!