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

Checking if multiple textboxes have equal value??

P: 5
How can i check if multiple textboxes have the same value??
Sure, i can do a bazillion if and else commands, but im sure it can be done in some easier way.

For example, i have 3 (lets say three to make it easy atm) textboxes where the user inputs a value in each, and it checks if any of these are equal to eachother?

This is something that i have been wondering about for quite some time =)
Nov 7 '06 #1
Share this Question
Share on Google+
7 Replies


100+
P: 1,646
How can i check if multiple textboxes have the same value??
Sure, i can do a bazillion if and else commands, but im sure it can be done in some easier way.

For example, i have 3 (lets say three to make it easy atm) textboxes where the user inputs a value in each, and it checks if any of these are equal to eachother?

This is something that i have been wondering about for quite some time =)
Hi. You can use the Select Case test which would be easier to read but it would only return the first equality and ignore the rest. You would need n - 1 Select blocks for n textboxes. e.g. 4 textboxes...
Expand|Select|Wrap|Line Numbers
  1. Select Case CInt(txt1.Text)
  2.    Case CInt(txt2.Text)
  3.        blah blah blah
  4.    Case CInt(txt3.Text)
  5.       blah blah blah
  6.    Case CInt(txt4.Text)
  7.       blah blah blah
  8. End Select
  9.  
  10. Select Case CInt(txt2.Text)
  11.    Case CInt(txt3.Text)
  12.       blah blah blah
  13.    Case CInt(txt4.Text)
  14.       blah blah blah
  15. End Select
  16.  
  17. Select Case CInt(txt3.Text)
  18.    Case CInt(txt4.Text)
  19.       blah blah blah
  20. End Select
  21.  
Nov 7 '06 #2

Expert 5K+
P: 8,434
Hi. You can use the Select Case test which would be easier to read but it would only return the first equality and ignore the rest. You would need n - 1 Select blocks for n textboxes. e.g. 4 textboxes...
Blah blah blah... :)
If the textboxes are in an array then you could use a couple of nested loops. The advantage is that the code is not dependent on the number of controls. This isn't tested, just typed straight here, but I'm thinking something like
Expand|Select|Wrap|Line Numbers
  1. Dim I as Long, J as long
  2. ' Assuming number of textboxes is in variable Count...
  3. For I = 1 to Count - 1
  4.   For J = I + 1 to Count
  5.     If TextBox(J) = TextBox(I) Then
  6.       ' They are equal - do something.
  7.     End If
  8.   Next
  9. Next
Note, the I + 1 prevents you from repeating the same comparisons. Wouldn't do any harm, it's just inefficient. Note also, I've started my numbering from 1, but in fact constrol arrays normally start with index = 0. I just hate numbering from zero. :)
Nov 7 '06 #3

P: 5
Thanks alot, the arrays worked great :D
Nov 8 '06 #4

P: 1
How can I find out how many textboxes I have? Can you count them during runtime?
Nov 24 '06 #5

Expert 5K+
P: 8,434
How can I find out how many textboxes I have? Can you count them during runtime?
If you mean the number of them in the array, you can just retrieve it from the .Count property. For example, if the text boxes are called Text1, then Text1.Count will tell you the number of occurences. Keep in mind that by default, the indexes start from 0, so if .Count is 3 for instance, then they will be numbered from 0 to 2.

If you mean the number of textboxes in general on the form, then while there are probably better ways, you could try this sample code I just threw into a form in a test project.
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Click()
  2.   Dim c As Control, i As Long
  3.   For Each c In Me.Controls
  4.     If TypeOf c Is TextBox Then
  5.       Debug.Print "Here's one: "; c.Name
  6.       i = i + 1
  7.     End If
  8.   Next
  9.   Debug.Print "Found "; Format(i); ' text box(es).'
  10. End Sub
Nov 25 '06 #6

100+
P: 1,646
How can I find out how many textboxes I have? Can you count them during runtime?
Of course the easy way is to look at the form and count them :)
Nov 25 '06 #7

Expert 5K+
P: 8,434
Of course the easy way is to look at the form and count them :)
Of course, but it's so primitive. :)
Nov 25 '06 #8

Post your reply

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