By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,659 Members | 1,945 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,659 IT Pros & Developers. It's quick & easy.

Excluding a textbox from a calculation if input = ""

P: 3
I have a VB.NET program I am creating that sums 7 textboxes and displays their total in a total textbox.

code:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

total.text = (Convert.ToInt32(rt1.Text) + Convert.ToInt32(rt2.Text) + Convert.ToInt32(rt3.Text) + Convert.ToInt32(rt4.Text) + Convert.ToInt32(rt5.Text) + Convert.ToInt32(rt6.Text) + Convert.ToInt32(rt7.Text))

What I want it to do is exclude a textbox from the calculation if a user doesn't enter anything in the field. So if they enter values for rt1 to rt6 but do not enter a value in textbox rt7, then exclude rt7 from the calc. I could do it the long way by accounting for every possible combinatin of the 7 textboxes but that would be a pain!
Any ideas?
Mar 16 '07 #1
Share this Question
Share on Google+
10 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I have a VB.NET program I am creating that sums 7 textboxes and displays their total in a total textbox.

code:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

total.text = (Convert.ToInt32(rt1.Text) + Convert.ToInt32(rt2.Text) + Convert.ToInt32(rt3.Text) + Convert.ToInt32(rt4.Text) + Convert.ToInt32(rt5.Text) + Convert.ToInt32(rt6.Text) + Convert.ToInt32(rt7.Text))

What I want it to do is exclude a textbox from the calculation if a user doesn't enter anything in the field. So if they enter values for rt1 to rt6 but do not enter a value in textbox rt7, then exclude rt7 from the calc. I could do it the long way by accounting for every possible combinatin of the 7 textboxes but that would be a pain!
Any ideas?
I'm betting the wonderful people in the VB forum might know (or might know enough to direct you to the forum that does know (possibly .NET). The VB forum is on the right hand side of your screen.
Mar 16 '07 #2

P: 3
I have a VB.NET program I am creating that sums 7 textboxes and displays their total in a total textbox.

code:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

total.text = (Convert.ToInt32(rt1.Text) + Convert.ToInt32(rt2.Text) + Convert.ToInt32(rt3.Text) + Convert.ToInt32(rt4.Text) + Convert.ToInt32(rt5.Text) + Convert.ToInt32(rt6.Text) + Convert.ToInt32(rt7.Text))

What I want it to do is exclude a textbox from the calculation if a user doesn't enter anything in the field. So if they enter values for rt1 to rt6 but do not enter a value in textbox rt7, then exclude rt7 from the calc. I could do it the long way by accounting for every possible combinatin of the 7 textboxes but that would be a pain!
Any ideas?
Mar 16 '07 #3

MMcCarthy
Expert Mod 10K+
P: 14,534
I'm betting the wonderful people in the VB forum might know (or might know enough to direct you to the forum that does know (possibly .NET). The VB forum is on the right hand side of your screen.
This question is being moved to the .NET forum.

ADMIN
Mar 16 '07 #4

SammyB
Expert 100+
P: 807
I have a VB.NET program I am creating that sums 7 textboxes and displays their total in a total textbox.

code:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

total.text = (Convert.ToInt32(rt1.Text) + Convert.ToInt32(rt2.Text) + Convert.ToInt32(rt3.Text) + Convert.ToInt32(rt4.Text) + Convert.ToInt32(rt5.Text) + Convert.ToInt32(rt6.Text) + Convert.ToInt32(rt7.Text))

What I want it to do is exclude a textbox from the calculation if a user doesn't enter anything in the field. So if they enter values for rt1 to rt6 but do not enter a value in textbox rt7, then exclude rt7 from the calc. I could do it the long way by accounting for every possible combinatin of the 7 textboxes but that would be a pain!
Any ideas?
Why not replace Convert.ToInt32 with a Val function of your own that does the Convert.ToInt32 inside of a try-catch. That way, if anything goes wrong, your Val returns a 0, otherwise it returns the integer value of the string.

Let us know if you need more help that this. --Sam
Mar 16 '07 #5

Expert 100+
P: 1,892
I have a VB.NET program I am creating that sums 7 textboxes and displays their total in a total textbox.

code:

Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click

total.text = (Convert.ToInt32(rt1.Text) + Convert.ToInt32(rt2.Text) + Convert.ToInt32(rt3.Text) + Convert.ToInt32(rt4.Text) + Convert.ToInt32(rt5.Text) + Convert.ToInt32(rt6.Text) + Convert.ToInt32(rt7.Text))

What I want it to do is exclude a textbox from the calculation if a user doesn't enter anything in the field. So if they enter values for rt1 to rt6 but do not enter a value in textbox rt7, then exclude rt7 from the calc. I could do it the long way by accounting for every possible combinatin of the 7 textboxes but that would be a pain!
Any ideas?
Why don't you just use some if statements to check if there is input? Also, I would use the Val() function Sam mentioned or are you wanting to only calculate integers?
Mar 17 '07 #6

P: 9
Here's one way you could do it.

Expand|Select|Wrap|Line Numbers
  1.      'How many ints can we have?
  2.      Dim intArray(6) As Integer
  3.         Dim Ctrl As New Control
  4.         Dim i As Integer = 0
  5.         Dim total
  6.         'Loop through the textbox controls looking for ints to add
  7.         For Each Ctrl In Me.Controls
  8.             If TypeOf Ctrl Is TextBox Then
  9.                 If Ctrl.Text <> String.Empty Then
  10.                     intArray(i) = CInt(Ctrl.Text)
  11.                     i = i + 1
  12.                 End If
  13.             End If
  14.         Next
  15.         'Sum the array so that we can stick it int the total box
  16.         For Each MyInts As Integer In intArray
  17.             total += MyInts
  18.         Next
  19.         TextBox7.Text = total
Provided, if you have other textbox controls that you don't wish to sum, you'll need to catch for those. But that snippet should get you close.

# Anthony Towry
Mar 17 '07 #7

P: 9
Expand|Select|Wrap|Line Numbers
  1.         TextBox7.Text = total
This would more likely be TextBox8 in the example, but you get the idea.
Mar 17 '07 #8

P: 3
Thanks for the help guys. That did work, but what I decide to do to make it easier on the user (and less confusing), is have them check a box if the condition was true, and then add 1 to the total if the checkbox is checked. I was making it way to difficult. I thank you for the help though.
Mar 19 '07 #9

Expert 100+
P: 1,892
That control array Patrick posted doesn't like a bad option.
Mar 19 '07 #10

100+
P: 168
Create temporary variables for the values in every text box, ala:

Dim t1, t2 As String
Dim v1, v2 As Int32

t1 = TextBox1.Text
t2 = TextBox2.Text

If (t1 = "") Then
v1 = 0
Else
v1 = Convert.ToInt32(t1)
End If

If t2 = "" Then
v2 = 0
Else
v2 = Convert.ToInt32(t2)
End If

TextBox3.Text = v1 + v2
Mar 19 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.