2016-04-29 54 views
0

第一次检查时,我推测这将是一个简单的任务(也许它仍然是!),但我有困难。假设我有一个包含1000个文本框的表单,其中每个文本框都包含随机分布的字符串,但在很多情况下匹配字符串。突出显示文本框内的几组匹配

AAAA-XXXX 
AAAA-XXXX 
BBBB-XXXX 
BBBB-XXXX 
CCCC-XXXX 
CCCC-XXXX 
    ... 

如何可能我遍历文本框,确定所有匹配的例子,并着重比赛发生textbox.backcolor:例如,下面的可以在任何一个如果1000个文本框可以找到?背景颜色对于完全匹配应该是相同的,但对于每个独特的匹配组来说不同。可能有多达100个不同的套件!

+0

'对于x = 1至1000'用'Me.Controls( “文本框” &X)'? –

回答

1

就刮起一起工作的测试项目,这是我会采取那种方法。它避免了将每个文本框与每个其他文本框进行比较。我评论了这一切为你:)

Private Sub SetBoxColors() 

    'The keys are textbox texts, the values are the number of times it occurs 
    Dim UniqueTextsAndUsage As New Dictionary(Of String, Integer) 

    Dim FirstInstanceTextBoxes As New List(Of TextBox) 

    'The keys are textbox texts, the values are the colour for the box 
    Dim UniqueColors As New Dictionary(Of String, System.Drawing.Color) 

    'Iterate over all the text boxes 
    ' Substitute Me for your Form instance if necessary 
    For Each TBox As Control In Me.Controls 

     'Skip things that aren't textboxes 
     If Not TypeOf TBox Is TextBox Then 
      Continue For 
     End If 

     'If we have seen this textbox text before 
     If UniqueTextsAndUsage.ContainsKey(TBox.Text) Then 

      'Increase the usage 
      UniqueTextsAndUsage(TBox.Text) += 1 

      If UniqueTextsAndUsage(TBox.Text) = 2 Then 

       'This is the second usage, generate a colour for this set of boxes 
       UniqueColors.Add(TBox.Text, GenerateColor(UniqueColors.Count + 1)) 

      End If 

      'Colour this textbox 
      ' (it won't get the first instance of each unique string) 
      TBox.BackColor = UniqueColors(TBox.Text) 

     Else 

      'We have NOT seen this textbox text before 

      'Add the first occurence of the text 
      UniqueTextsAndUsage.Add(TBox.Text, 1) 

      'Mark this textbox as one we may have to colour later 
      FirstInstanceTextBoxes.Add(TBox) 

     End If 

    Next 

    'Colour all the first instances 
    For Each TBox As TextBox In FirstInstanceTextBoxes 

     'Check there are sufficient uses of this text 
     If UniqueTextsAndUsage(TBox.Text) > 1 Then 
      TBox.BackColor = UniqueColors(TBox.Text) 
     End If 

    Next 

End Sub 

Private Function GenerateColor(Id As Integer) As System.Drawing.Color 

    'Needs more thought - often too dark 
    Dim KnownColourByIdNumber As System.Drawing.KnownColor = Id 
    Return System.Drawing.Color.FromKnownColor(KnownColourByIdNumber) 

End Function 

的GenerateColor功能需要更多的思想,使其产生更好的一些颜色,但我留给你。

您可能还需要将每个框设置为DefaultControl颜色或任何其调用的重置,然后在SetBoxColors顶部运行该框。

Matching text colours

+0

感谢您的精彩回复!我从中学到了很多东西,它几乎正是我需要它做的。我会努力适应我的形式。谢谢! – MalLav

1

你可以做到这一点

Dim strText As String 
    For Each TextBox1 As System.Windows.Forms.TextBox In Me.Controls 
     strText = TextBox1.Text 
     For Each TextBox2 As System.Windows.Forms.TextBox In Me.Controls 
      If strText = TextBox2.Text AndAlso TextBox2.Name <> TextBox1.Name Then 
       TextBox2.BackColor = Color.Red ' or whatever you want to use 
       TextBox1.BackColor = Color.Red 
      End If 

     Next 
    Next 
+0

感谢您的帮助!我从相似的角度来解决这个问题 - 这将正确地识别比赛,但比赛怎么能被赋予一种独特的颜色?无论textbox.text如何,您示例中的所有匹配都会设置背景色。再次感谢! – MalLav