Reputation: 30813
I have a very simple WPF application which only have a RichTextBox and a button declared like this:
<Window x:Class="TestApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:SimpleChessWPF"
mc:Ignorable="d"
Title="MainWindow" Height="670.8" Width="741.8">
<Grid>
<RichTextBox HorizontalAlignment="Left" Height="534" Margin="10,97,0,0" VerticalAlignment="Top" Width="715" Name="TestRtb">
<RichTextBox.Resources>
<Style TargetType="{x:Type Paragraph}">
<Setter Property="Margin" Value="0"/>
</Style>
</RichTextBox.Resources>
</RichTextBox>
<Button Content="Button" HorizontalAlignment="Left" Margin="105,31,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
</Window>
And then I tried to make an event to print out some test texts when the button is pressed like this:
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e) {
TestRtb.Document.Blocks.Clear();
for(int i = 0; i < 64; ++i) {
TestRtb.AppendText("i: " + i.ToString() + "\n");
printBits(i);
printBits(toBitSquare(i));
TestRtb.AppendText("\n");
}
}
private void printBits(int i) {
string text = i.ToBitArrayString();
TestRtb.AppendText(text + "\n");
}
public static int toBitSquare(int square) {
return ((square & ~7) >> 1) | (square & 7);
}
}
'ToBitArrayString()' is my own int
extension. It is basically used to print its bit array representation. And this is what happen when I run my program and press the button:
Everything works as expected, except the first line "\n" is not printed. What happened?
Just in case you want to replicate, my extension functions are as follow:
private static string toBitArrayString(BitArray b, int spacePerByte = 1, int spacePerNibble = 0) {
StringBuilder sb = new StringBuilder();
for (int j = b.Length - 1; j >= 0; --j) {
int i = b.Length - 1 - j;
if (i % 4 == 0 && i > 0 && spacePerNibble > 0)
sb.Append(string.Concat(Enumerable.Repeat(" ", spacePerNibble)));
if (i % 8 == 0 && i > 0 && spacePerByte > 0)
sb.Append(string.Concat(Enumerable.Repeat(" ", spacePerByte)));
sb.Append(b[j] ? 1 : 0);
}
return sb.ToString();
}
public static string ToBitArrayString(this int input, int spacePerByte = 1, int spacePerNibble = 0) {
BitArray b = new BitArray(new int[] { input });
return toBitArrayString(b, spacePerByte, spacePerNibble);
}
Upvotes: 2
Views: 329
Reputation: 324
The Rich Text Box that is shipped with .NET Framework 3.5 SP1 contains a bug. When converting plain text that contains a line break (\r), not to confuse with a new paragraph (\n). Use this working:- TestRtb.AppendText("i: " + i.ToString() + "\r");
Upvotes: 3