Jump to content

Calculate textbox values


Recommended Posts

I have an application that well have a simple calculator built in. I'd like to make it as easy as possible for myself here by storing the problem the user is calculating in a read only textbox. When the user hits equal, it should take the user's mathematic expression from the textbox and actually calculate it to return an answer.

Example

User puts in 2 + 2, Textbox control reads "2+2" (Appends on each character as the user types it like in Windows calculator, User clicks the equal button and the text property from textbox control is calculated and returns an answer.

The long way I can think of solving this is going character by character and converting the values, but this would require much more code and time to develop. So is there an easy way to calculate mathematically whats in a textbox? I use vb.net but c# developers feel free to comment since it should be similar...

Link to comment
Share on other sites

If you want the textbox update to happen in the browser real time without a forms submission, you're gonna need to do it with javascript. Are you planning on a post submission?

Writing that in JS is fairly straightforward. And using eval() on the final string will let you execute the expression as a JS command. If you're planning on vb executing it, see if it supports an eval() style string evaluator.

Link to comment
Share on other sites

Ok to clarify here it is a vb.net windows forms application. Its not only a calculator, but a calculator integrated into it would make things nice for the user without having to hunt down windows calculator in the start menu or open more windows in thier taskbar than what they need.

Would this work?, taking the text property, having it convert to a decimal if it calculated a mathematic expression passed in, and assigning to a variable for output?

decAnswer = CDec(TextBox1.Text)

If its only a number than thats not an issue but i'm not sure if vb would calculate the text as an expression like in the case 2+2 to make 4 and convert it to a decimal :(

Link to comment
Share on other sites

Looks very interesting, a little tweaking here and there but it looks almost like the "rough draft" I was working on earlier today. I think for the purpose of getting it done I'm going to have it calculate as the user types stuff in, but having a function like this well be very useful for other projects or for my app anyway when I'm done with it. Thanks for posting that link.

Would it make any difference if I learned C#? I guess I need to stay away from .net all together but what languages would provide flexibility like this that VB can't? I'm stuck on VB at the moment but in the next two years I should know C# as well and maybe Java with the classes I'm taking, so by all means I'm more than willing to learn a new language if it provides additional benefits.

Link to comment
Share on other sites

Would it make any difference if I learned C#? I guess I need to stay away from .net all together but what languages would provide flexibility like this that VB can't? I'm stuck on VB at the moment but in the next two years I should know C# as well and maybe Java with the classes I'm taking, so by all means I'm more than willing to learn a new language if it provides additional benefits.

Don't let me turn you off to anything. I hate VB because it was Microsoft's lame and late attempt of getting in on the web's server side. Biggest flaw, no dynamic includes. And then sticking it into .net was like putting a Model T's engine in a new Vette. I do C# cause it pays. But then, I do a lot of things for money I wouldn't do on my own.

From the grand scheme of code management and deployment, C# is not a bad way to do things. It's major flaw is the lack of data persistence. Having to re-instantiate every object needed for every request creates unnecessary overhead on the web and db machines and load on your network. It's biggest strength is being able to maintain a business rules layer of compiled objects which your devs can access in their web layer development. This provides code lock down and protection for complex environments. There are a lot of C# jobs out there. Not a bad way to make a living.

If you learn C# and Java, PHP's not that far off for you. And if you're looking to make a living at web or internet app development, the broader your skills, the better off you will be. Might be worth looking into Ruby and ColdFusion as well. You could learn CF in a weekend, it's cake to work with. Look through Monster or Dice and you will see lots of dev jobs where knowledge of multiple platforms is required.

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...

Ok with the way I was writing it I would've had a gigantic function or 5-6 functions and no support for ()'s. I think I'll stick with the code from the link than. Now to convert it to C# since I've gotten into that as well lately...

Update: somethings up with the function. I had a whole bunch of not delcared errors until I imported System.Math but I still got one more error for the single line using "m_Primatives". Its not declared ovbiously. I wonder what its there for or if theres another namespace I need imported.

C# definetly does have an advantage over VB, paticuarly that being of size of the compiled assembly. On a few test apps I've thrown together the C# versions were only half the size of the vb ones. Anyone know why that is if its all supposed to compile to MSIL anyway?

Link to comment
Share on other sites

  • 2 weeks later...

Something....

Cthobs is bang on the money with eval, there is an eval function built into VB and it works near enough the same was as the javascript one.

However, that's not why my post ;)

If you use the ASP>NET ajax toolkit, there's 2 components in there that will A) Allow you to do your real time in page update and B) Have a Calulator connected to your text box.

You'll find everything you need to know in both VB and C# here.

http://www.asp.net/ajax/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...