2015-08-18 1 views
1

Je sauvegarde les informations sur l'emplacement des modifications de l'algorithme de tri dans un tableau.WPF, texte de surbrillance richtextbox

if (sorting[j] > sorting[j + 1]) 
{ 
     Swap(sorting, j, j + 1); 

     SortingTraceInfo sortInfo = new SortingTraceInfo(); // struct Variable 
     sortInfo.Position = j; // change position save 
     sortInfo.TargetPosition = j + 1; // changed position save 
     sortInfo.SortingNumbers = sorting.ToArray(); // 
     sortingInfos.Add(sortInfo);  
} 

Je connais l'index de la position modifiée. Et il produit le résultat était la richetexttextbox.

Il a appliqué l'index (sortInfo.position) qui a été enregistré dans le résultat (dans richtextbox).

résultat dans richtextbox. Il est appliqué à l'index. Le résultat que je veux est

Sortie d'une ligne et la couleur change chaque fois que vous cliquez sur le bouton.

23 59 59 70 12 92 19 14 77 51 - index> 70 < couleur rouge, 12 couleur bleu

(position = 3, tartgetposition = 4),

23 59 59 12 70 92 19 14 77 51 -> 92 < couleur rouge, 19 couleur bleu

index (position = 5, tartgetposition = 6),

23 59 59 12 70 19 92 14 77 51 -> 92 < couleur rouge, 14 couleur bleu

indice

(position = 6, tartgetposition = 7),

Cependant, je ne ai pas ........

Répondre

1

Si je vous ai bien compris, votre problème est de savoir comment rendre les textes dans un RichTextBox avec des couleurs différentes! Pour ce faire, vous pouvez utiliser un TextRange pour chaque élément Array et appliquer la couleur de la brosse en fonction de la situation de l'élément (Position -> Rouge, TargetPosition -> Bleu, d'autres -> Noir), donc pour la RichTextBox suivante:

<StackPanel> 
    <RichTextBox Name="Output"> 
    </RichTextBox> 
    <Button Content="Next" Click="Next_OnClick"/> 
</StackPanel> 

vous avez besoin à chaque bouton Next Cliquez:

  • Effacer la sortie RichTetex
  • itérer à travers des SortingArray, et créer une base TextRang sur le il em indice
  • Une opération de rupture de boucle doit être exécutée chaque fois qu'une opération d'échange a eu lieu pour visualiser correctement la sortie

    public struct SortingTraceInfo 
    { 
        public int Position; 
        public int TargetPosition; 
        public int[] SortingNumbers; 
    } 
    
    public int[] Sorting = new[] { 23, 59, 59, 70, 12, 92, 19, 14, 77, 51 };   
    public List<SortingTraceInfo> SortingInfos=new List<SortingTraceInfo>(); 
    
    private void Next_OnClick(object sender, RoutedEventArgs e) 
    {    
        Output.Document.Blocks.Clear(); 
        for (int j = 0; j < Sorting.Count()-1; j++) 
        { 
         if (Sorting[j] > Sorting[j + 1]) 
         {     
          //render to RTB 
          for (int i = 0; i < Sorting.Count(); i++) 
          { 
           if (i==j) 
           { 
            //render the number red 
            var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); 
            textRange.Text = Sorting[i] + " "; 
            textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Red);        
           } 
           else if(i==j+1) 
           { 
            //render the number blue 
            var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); 
            textRange.Text = Sorting[i]+ " "; 
            textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); 
    
           } 
           else 
           { 
            //render the number black 
            var textRange = new TextRange(Output.Document.ContentEnd, Output.Document.ContentEnd); 
            textRange.Text = Sorting[i] + " "; 
            textRange.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Black);        
           } 
          } 
    
    
          //Swap(Sorting, j, j + 1); 
          int tmp=Sorting[j]; 
          Sorting[j] = Sorting[j+1]; 
          Sorting[j + 1] = tmp; 
    
          var sortInfo = new SortingTraceInfo(); // struct Variable 
          sortInfo.Position = j; // change position save 
          sortInfo.TargetPosition = j + 1; // changed position save 
          sortInfo.SortingNumbers = Sorting.ToArray(); // 
          SortingInfos.Add(sortInfo); 
    
          //handle one sorting operation one at a time 
          break; 
         }    
        }   
    } 
    

le résultat:

enter image description here

enter image description here

etc ...

+0

Merci. j'essaye ce code! –