Reputation: 51
I save the information on the location of the sort algorithm changes into an array.
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);
}
I know the index of the changed position. And it outputs the result was the richtextbox.
It applied the index(sortInfo.position) that was saved in the result(in richtextbox).
result in richtextbox. It is applied to the index. The result I want is
Output one line and color changes each time you click the button.
23 59 59 70 12 92 19 14 77 51 -> 70 < color red, 12 color blue
index ( position = 3, tartgetposition = 4),
23 59 59 12 70 92 19 14 77 51 -> 92 < color red, 19 color blue
index ( position = 5, tartgetposition = 6),
23 59 59 12 70 19 92 14 77 51 -> 92 < color red, 14 color blue
index ( position = 6, tartgetposition = 7),
However, I failed........
Upvotes: 1
Views: 685
Reputation: 9944
If i understood you properly, your problem is how to render texts in a RichTextBox
with different Colors !
To do that you could use a TextRange
for each Array
item and apply brush color based on the item situation (Position -> Red, TargetPosition -> Blue, others -> Black), so for the following RichTextBox
:
<StackPanel>
<RichTextBox Name="Output">
</RichTextBox>
<Button Content="Next" Click="Next_OnClick"/>
</StackPanel>
you need at each Next's Button Click :
RichTetex
Sorting
Array
, and create a TextRang
based on the item indexA loop break operation should be executed each time a swap operation occurred to properly visualize the output
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;
}
}
}
the result :
etc...
Upvotes: 1