Click here to Skip to main content
15,868,139 members
Please Sign up or sign in to vote.
2.60/5 (4 votes)
See more:
Suppose I have one textbox, I enter the value in text box 13+15 I press tab key and i want to result in this textbox dynamically multiplication division subtraction etc supose enter value in textbox 15*2 result only show in text box 30 how to do it plz replay me
Posted
Updated 13-Jun-14 3:33am
v3
Comments
Debabrata_Das 13-Jun-14 11:05am    
Hi Sandip, May I ask if you are a student? The intention behind my question is that if you're a student and you need to build a project with the functionality, then using any third-party library would not be an ideal solution. You have to write a parser logic which will read the string and calculate the output.

-DD

Try this, I hope it can be a simple way.
C#
public object DoCalculate(string expression)
{
    object result = null;
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Val",typeof(object),expression));
    foreach (DataRow item in dt.Rows)
    {
        result= item["val"];
    }
    return result;
}


you can cast the result accordingly.
 
Share this answer
 
Use http://csharpeval.codeplex.com/[^]
you can install it via nuget https://www.nuget.org/packages/ExpressionEvaluator[^]
then
C#
var expression = new CompiledExpression("1 + 2");
var result = expression.Eval();
// get the value as 3


Above will solve your main Task. But if you need to build your own expression evaluator check below articles/ resources
C# Expression Evaluator[^]

other than your main Task you need to detect Tab key press.
Detect TabKey[^]
Once you capture the event do the above evaluation and display the result.
 
Share this answer
 
v2
try this.. :)

first download NCalc - 1.3.8[^] and add reference to your project and then try this.. :)

C#
string myExpr = Textbox1.Text; // (2+3)*15
decimal result = Convert.ToDecimal(new NCalc.Expression(myExpr).Evaluate());


math / function / boolean /string expression evaluator[^]

Evaluate C# string with math operators [duplicate][^]

C# Math calculator [duplicate][^]

State of the Art Expression Evaluation[^]

Best and shortest way to evaluate mathematical expressions[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900