2016-04-29 57 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