Mykro Posted December 6, 2003 Posted December 6, 2003 I have a form with 3 text boxes.. txtDiameter.text txtCircumference.text, txtArea.text and btncompute to compute the value entered in txtDiameter that will be displayed in txtCircumference.text and txtArea.text I have a code module: Module CircGeometry Public Sub CircleGeometry(ByVal diameter As Double, ByRef Circumference As Double, _ ByRef Area As Double) Circumference = Math.PI * diameter Area = Math.PI * diameter ^ 2 / 4 End Sub End Module And here is the btnCompute click event: Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim C As Double Dim A As Double Dim D As Double D = Val(txtDiameter.Text) Call CircleGeometry(D, C, A) txtCircumference.Text = Format(C, "0.00") txtArea.Text = Format(A, "0.00") End Sub End Class What I need help on is this: .. How can I code this so that no matter what text box I enter a value it is calculated in the other text boxes simutaneously.. I don't how to code this... Quote
*Experts* DiverDan Posted December 6, 2003 *Experts* Posted December 6, 2003 How about this: if txtDiameter.Text <> Nothing then 'compute from diameter elseif txtArea.Text <> Nothing then 'compute from area elseif txtCircumference <> nothing then 'compute from circumference end if you'll probably need to expand the if statements to also watch for only one entry in the three boxes and, of course, restrict the boxes to a numeric entry only. Hope that helps Dan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Mykro Posted December 6, 2003 Author Posted December 6, 2003 How about this: if txtDiameter.Text <> Nothing then 'compute from diameter elseif txtArea.Text <> Nothing then 'compute from area elseif txtCircumference <> nothing then 'compute from circumference end if you'll probably need to expand the if statements to also watch for only one entry in the three boxes and, of course, restrict the boxes to a numeric entry only. Hope that helps Dan Thanks for the code But.. It don't work.. Maybe you don't quite understand what I want to do.. instead of entering a value in txtdiameter I could put a value in txtArea instead and it will compute and put the correct answers in the other text boxes. Or put a value in txtcircumference and it will calculate for the other text boxes. Quote
Mothra Posted December 7, 2003 Posted December 7, 2003 If you use a Button to fire the calculation method, you try this in the Button's Click event... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click If Not TextBox1.Text = "" And _ TextBox2.Text = "" And _ TextBox3.Text = "" Then 'Calculate whatever ElseIf Not TextBox2.Text = "" And _ TextBox1.Text = "" And _ TextBox3.Text = "" Then 'Calculate whatever ElseIf Not TextBox3.Text = "" And _ TextBox1.Text = "" And _ TextBox2.Text = "" Then 'Calculate whatever End If End Sub This way, it will only do your calculations if there are no other values entered. Quote Being smarter than you look is always better than looking smarter than you are.
Mykro Posted December 7, 2003 Author Posted December 7, 2003 If you use a Button to fire the calculation method, you try this in the Button's Click event... Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles Button1.Click If Not TextBox1.Text = "" And _ TextBox2.Text = "" And _ TextBox3.Text = "" Then 'Calculate whatever ElseIf Not TextBox2.Text = "" And _ TextBox1.Text = "" And _ TextBox3.Text = "" Then 'Calculate whatever ElseIf Not TextBox3.Text = "" And _ TextBox1.Text = "" And _ TextBox2.Text = "" Then End If End Sub This way, it will only do your calculations if there are no other values entered. I'll try your code.. In the mean time this was what I came up with.. just need to clear the text boxes before the next calculation. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click Dim Circumference As Double Dim Area As Double Dim Diameter As Double Diameter = Val(txtDiameter.Text) 'convert to number Area = Val(txtArea.Text) 'convert to number Circumference = Val(txtCircumference.Text) 'convert to number If txtDiameter.Text <> "" Then Circumference = Math.PI * Diameter Area = 3.14 * Diameter ^ 2 / 4 ElseIf txtCircumference.Text <> "" Then Area = ((Circumference / 3.14) ^ 2) * 3.14 / 4 Diameter = Circumference / 3.14 ElseIf txtArea.Text <> "" Then Circumference = 2 * Math.Sqrt(Area / 3.14) * 3.14 Diameter = 2 * Math.Sqrt(Area / 3.14) Else MessageBox.Show("You Must Enter a Number!", "No Numeric Entry!", MessageBoxButtons.OK, MessageBoxIcon.Hand) txtDiameter.Focus() End If txtCircumference.Text = Format(Circumference, "0.00") 'format to display in txt box txtArea.Text = Format(Area, "0.00") 'format to display in txt box txtDiameter.Text = Format(Diameter, "0.00") 'format to display in txt box End Sub Private Sub frmCircle_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load txtDiameter.Focus() End Sub Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click txtDiameter.Text = "" txtCircumference.Text = "" txtArea.Text = "" txtDiameter.Focus() End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Quote
*Experts* DiverDan Posted December 7, 2003 *Experts* Posted December 7, 2003 Hum, looks like my original example. But...what happens if the user enters values in two or all boxes? How are you handling a non-numeric entry - it's better to restrict the text box to only numeric entries than display a message box. Along with txtArea.Text = "' you might use txtArea.Clear() or txtArea.Text = Nothing....they're basically all the same. Here's an example of restricting a text box that you might include in you program rather than the message box: 'Positive Numbers, Dot and Backspace Only Private Sub PositiveNumbersDotOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _ txtVolts.KeyPress, txtValue.KeyPress If AscW(e.KeyChar) = 8 Then 'backspace e.Handled = False Exit Sub End If If Not AscW(e.KeyChar) < 46 And Not AscW(e.KeyChar) >= 58 Or AscW(e.KeyChar) = 8 Then If sender.Text.IndexOf(".") >= 0 Then If sender.SelectedText.Length >= 1 Then e.Handled = False ElseIf e.KeyChar = "." Then e.Handled = True End If End If Else : e.Handled = True End If End Sub also look into the error provider control...it's pretty cool. Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Mykro Posted December 8, 2003 Author Posted December 8, 2003 Hum, looks like my original example. But...what happens if the user enters values in two or all boxes? How are you handling a non-numeric entry - it's better to restrict the text box to only numeric entries than display a message box. Along with txtArea.Text = "' you might use txtArea.Clear() or txtArea.Text = Nothing....they're basically all the same. Here's an example of restricting a text box that you might include in you program rather than the message box: 'Positive Numbers, Dot and Backspace Only Private Sub PositiveNumbersDotOnly_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles _ txtVolts.KeyPress, txtValue.KeyPress If AscW(e.KeyChar) = 8 Then 'backspace e.Handled = False Exit Sub End If If Not AscW(e.KeyChar) < 46 And Not AscW(e.KeyChar) >= 58 Or AscW(e.KeyChar) = 8 Then If sender.Text.IndexOf(".") >= 0 Then If sender.SelectedText.Length >= 1 Then e.Handled = False ElseIf e.KeyChar = "." Then e.Handled = True End If End If Else : e.Handled = True End If End Sub also look into the error provider control...it's pretty cool. Hey Dan.... Could you do me a favor and comment each line of your code..I'm a little fuzzy with it, and good commentation would help me out alot...Thanks :D Quote
*Experts* DiverDan Posted December 8, 2003 *Experts* Posted December 8, 2003 (edited) Firstly, opps I forgot to DirectCast sender in my example and that is not a good habit, sorry. It should be DirectCast(sender, TextBox).Text and DirectCast(sender, TextBox).SelectedItem. The code looks at the keyborad entered keys (KeyChar) and allows only specific keys from being entered into the textboxes. AscW(e.KeyChar) 48 through 58 are the keyboard number keys 0 through 9 while AscW(e.KeyChar) 8 is the keyboard backspace key. Obviously a decimaled number can only contain one dot and the code looks for an instance of the dot in the sender.Text.IndexOf(".") section. If a dot is present then a positive number will be returned referencing its location in the textbox.Text string, otherwise it will return -1 if no dot is present. The SelectedText.Length allows the user to select (highlight) text that contains a dot and either delete it or enter another value. If this section of code was not present then once a dot was entered, the user would not be able to select (hightlight) the textbox and enter a decimal, ie .95. e.Handled = False allows keyboard entry while e.Handled = True stops the keyboard entry. I personally prefer restricting text box entries over annoying message boxes popping up on the screen with a beep. But if the text box can't be restricted then try the ErrorProvider1 control. It flashes a nice red circle with a white little exclamation point in the center, flashes for a bit and displays an appropriate tooltip. Much more preferred by myself. I think that about covers the documentation. You should still check for a double textbox entry. Dan Edited December 8, 2003 by DiverDan Quote Member, in good standing, of the elite fraternity of mentally challenged programmers. Dolphins Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.