2016-04-29 10 views
0

最初の検査では、これは簡単な作業であると推測しましたが(それでもなおです!)、まだ難しいです。 1000個のテキストボックスを持つフォームがあるとします。各テキストボックスにはランダムに分布していますが、多くの場合、一致する文字列が含まれています。例えば、1000個のテキストボックス場合いずれかに見いだすことができる以下:テキストボックス内の一致のハイライトセット

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

私は、テキストボックスをループ、一致するすべての例をどのように識別可能性があり、一致が起こるtextbox.backcolorを強調しますか?バックカラーは正確なマッチでは同じでなければなりませんが、ユニークなマッチのセットごとに異なるはずです。 100種類ものセットがあります!

+0

'Me.Controls( "テキストボックス" &X)'と1000 'からx = 1の場合? –

答えて

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

関連する問題