Click here to Skip to main content
15,844,903 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

how can i check if there's a duplicate value in array?
VB
Dim array(9) as String

array(0) = Textbox1.Text
array(1) = Textbox2.Text
array(2) = Textbox3.Text
array(3) = Textbox4.Text
array(4) = Textbox5.Text
array(5) = Textbox6.Text
array(6) = Textbox7.Text
array(7) = Textbox8.Text
array(8) = Textbox9.Text
array(9) = Textbox10.Text


if there's a duplicate value or text then the backcolor of the textbox turns to red.


any help will be highly appreciated.
Posted
Updated 6-Dec-12 9:55am
v2

Try this:

C#
C#
IEnumerable<Control> ctls = this.Controls.Cast<Control>();
var txtBoxQuery = from c in ctls
                  where c is TextBox && ctls.Count(x => x.Text == c.Text) > 1
                  select c;
foreach (Control c in txtBoxQuery)
        c.BackColor = Color.Red;



VB
VB
Dim ctls As IEnumerable(Of Control) = Me.Controls.Cast(Of Control)()
Dim txtBoxQuery = From c In ctls Where TypeOf c Is TextBox And ctls.Count(Function(x) x.Text = c.Text) > 1 Select c
For Each c As Control In txtBoxQuery
   c.BackColor = Color.Red
Next
 
Share this answer
 
v7
Comments
Adam R Harris 6-Dec-12 16:25pm    
Nice solution.
 
Share this answer
 
Comments
Adam R Harris 6-Dec-12 13:24pm    
Great answer! I love that site.
joshrduncan2012 6-Dec-12 13:26pm    
Can you rate my answer, please?
Adam R Harris 6-Dec-12 13:33pm    
Way ahead of you man, that's where the 5 stars came from.
joshrduncan2012 6-Dec-12 13:35pm    
Thanks Adam! Sometimes I can't tell when something has been voted on or not.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900