444,219 Members | 2,242 Online Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

# Formula in a text box

 P: n/a I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of a counter, and also have the user input the formula to be added up. More explicitly, suppose a user wants to add up 1 + 1/2 + 1/3 + 1/4 + ... + 1/100 Thus, the user would enter 1 as the start value and 100 for the end value and this user would also enter the formula 1/n, in a text box, as the summation formula (note that n would be a variable). Suppose a user wants to find the sum of 1/2^2 + 1/3^2 + 1/4^2 + ... +1/100^2. Then, the user would input 2 as the start value, 1000 as the end value and also input the formula 1/n^2. VB doesn't seem to like having the user enter the formula. I have tried something like this Dim n as integer Dim s as double s = 0 For n = start.text to final.text s = s + input.text Next Print s I have also tried to convert input.text to double (inside of the For loop), and VB throws up an unhandled exception. Since I am not very well versed in VB, I am not certain that I have explained the situation in the best way. So, any help or pointers would be very much appreciated. Thanks, Brian Apr 2 '06 #1
11 Replies

 P: n/a txtInput.text format 1,100,2 ( I think this may be roughly what you wanted ) Private Sub btnGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click Dim parts() As String Dim total As Double Dim power As Integer parts = txtInput.Text.Split(",") power = Convert.ToInt32(parts(2)) For i As Integer = Convert.ToInt32(parts(0)) To Convert.ToInt32(parts(2)) total += (1 / i) ^ power Next txtOutput.Text = total.ToString End Sub HTH -- ( OHM ) - One Handed Man AKA Terry Burns - http://TrainingOn.net "Brian VanPelt" wrote in message news:tv********************************@4ax.com...I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of a counter, and also have the user input the formula to be added up. More explicitly, suppose a user wants to add up 1 + 1/2 + 1/3 + 1/4 + ... + 1/100 Thus, the user would enter 1 as the start value and 100 for the end value and this user would also enter the formula 1/n, in a text box, as the summation formula (note that n would be a variable). Suppose a user wants to find the sum of 1/2^2 + 1/3^2 + 1/4^2 + ... +1/100^2. Then, the user would input 2 as the start value, 1000 as the end value and also input the formula 1/n^2. VB doesn't seem to like having the user enter the formula. I have tried something like this Dim n as integer Dim s as double s = 0 For n = start.text to final.text s = s + input.text Next Print s I have also tried to convert input.text to double (inside of the For loop), and VB throws up an unhandled exception. Since I am not very well versed in VB, I am not certain that I have explained the situation in the best way. So, any help or pointers would be very much appreciated. Thanks, Brian Apr 2 '06 #2

 P: n/a Hi Brian, Your project isn't as easy as it sounds. If the formula to be entered were one of a fixed (and hopefully small) set, then it would be ok, but if it is completely dependent on the user... then it can get quite complicated coding such a routine. The problem would occur at this point : (Considering the 1/1 + 1/2 + 1/3.... example) For i = startVal To endVal Dim thisFormula As String = formula.Replace("n", i.ToString()) ' The following line will always throw an exception... Dim thisValue As Single = CSng(thisForm) Next The problem is how to parse a uncalculated expression such as "1/1" from a string into a value. The only way to do this seems to be to parse out individual values and operators separately using Regular Expressions, and then to manually perform the calculation. I believe VB 6.0 had the Eval function which could do something like this easily, but haven't found anything like it in .NET. Below are some links that do this... You will realize how much code it involves ! You might consider using pre-built functionality such as this... 1. 2. A similar question was asked in these forums : Hope this helps, Regards, Cerebrus. Apr 3 '06 #3

 P: n/a On 2 Apr 2006 19:20:54 -0700, "Cerebrus" wrote: Hi Brian,Your project isn't as easy as it sounds. If the formula to be enteredwere one of a fixed (and hopefully small) set, then it would be ok, butif it is completely dependent on the user... then it can get quitecomplicated coding such a routine.The problem would occur at this point : (Considering the 1/1 + 1/2 +1/3.... example)For i = startVal To endVal Dim thisFormula As String = formula.Replace("n", i.ToString()) ' The following line will always throw an exception... Dim thisValue As Single = CSng(thisForm)NextThe problem is how to parse a uncalculated expression such as "1/1"from a string into a value.The only way to do this seems to be to parse out individual values andoperators separately using Regular Expressions, and then to manuallyperform the calculation. I believe VB 6.0 had the Eval function whichcould do something like this easily, but haven't found anything like itin .NET.Below are some links that do this... You will realize how much code itinvolves ! You might consider using pre-built functionality such asthis...1. 2. A similar question was asked in these forums :Hope this helps,Regards,Cerebrus. Thanks for the response. I am beginning to find out just how difficult this is. Imagine a user entering sqrt(n) into the formula box, or log(n). I originally asked someone in a VB6 group how they'd do it, and they repsonded that I'd need to add a script control component. But, of course I couldn't find that component in VB.net. It appears that with such a scripting idea, the user would be able to send variables to the program. So, I was investigating the Windows Script Host, but I am afraid that I just can't do anything with it just yet (read, I don't know how to use it). I don't know if this is any help, but this arises out of a desire to create a small program for my calc students to use to estimate the sum of an infinite series (so may may go as high as into the millions). In any event, I am going to investigate those links and thank you for the information. I also want to thank OHM for his reply as well. Brian Apr 3 '06 #4

 P: n/a Brian, Your question is in my idee easy to do, have a look at this sample on our website. http://www.vb-tips.com/default.aspx?...1-2b03e1a439ae I hope this helps, Cor "Brian VanPelt" schreef in bericht news:tv********************************@4ax.com...I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. For example, I would like the user to input the beginning and end values of a counter, and also have the user input the formula to be added up. More explicitly, suppose a user wants to add up 1 + 1/2 + 1/3 + 1/4 + ... + 1/100 Thus, the user would enter 1 as the start value and 100 for the end value and this user would also enter the formula 1/n, in a text box, as the summation formula (note that n would be a variable). Suppose a user wants to find the sum of 1/2^2 + 1/3^2 + 1/4^2 + ... +1/100^2. Then, the user would input 2 as the start value, 1000 as the end value and also input the formula 1/n^2. VB doesn't seem to like having the user enter the formula. I have tried something like this Dim n as integer Dim s as double s = 0 For n = start.text to final.text s = s + input.text Next Print s I have also tried to convert input.text to double (inside of the For loop), and VB throws up an unhandled exception. Since I am not very well versed in VB, I am not certain that I have explained the situation in the best way. So, any help or pointers would be very much appreciated. Thanks, Brian Apr 3 '06 #5

 P: n/a Hi again ! Cor, you're simply the coolest person !!! I've just tried it using the MS Script control and it works ! How come those poor guys involved in the discussions (see links I posted) didn't know about this. But then I'd never heard about this control before too. Many thanks... Brian, this will do the job for you with no extra effort. Regards, Cerebrus. Apr 3 '06 #6

 P: n/a "Brian VanPelt" schrieb:I am not a very experienced VB user, but I was trying to make a form that a user could input a formula for summation. Hand-made: MathLib Dynamic compilation: Build a Custom .NET "EVAL" Provider Runtime Compilation (A .NET eval statement) Evaluation of numeric expressions using J#: -- M S Herfried K. Wagner M V P V B Apr 3 '06 #7

 P: n/a you should throow vb.net out the window and build an ACCESS application. it can do this shit easily; there are extensive query builders / helpers keep your data in a database instead of making people enter it over and over again Apr 5 '06 #8

 P: n/a schrieb: keep your data in a database instead of making people enter it over and over again Huh?! I am wondering where the relation to the OP's post is... -- M S Herfried K. Wagner M V P V B Apr 5 '06 #9

 P: n/a Excel is a friggin disease. Apr 6 '06 #10

 P: n/a sorry wrong post. ACCESS has some really nifty built-in tools for expression builders Apr 6 '06 #11

 P: n/a Access is a custom build tool for a target market of ordinary users like yourself. .NET is for real coders that make use of it's almost infinate customability as a strength. ( OHM ) - One Handed Man AKA Terry Burns - http://TrainingOn.net Apr 7 '06 #12

### This discussion thread is closed

Replies have been disabled for this discussion. 