WPFのRichTextBoxについて
解決したいこと
自作言語のエディタをWPFアプリケーションで作っているのですが、タブを入力して別のファイルに移動した後、タブを入力したファイルに戻ると入力したタブが消えていました。RichTextBoxに消えたタブを表示させる方法を知りたいです。
該当するソースコード
作りかけですが
C#
global using System.Text.RegularExpressions;
using static System.Array;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using UCCScriptRunner;
using System.Runtime.Intrinsics.Arm;
namespace XTec2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static Dictionary<string, string> Codes = [];
public MainWindow()
{
InitializeComponent();
AddFile("Program");
AddFile("A");
// 自作コンパイラ作成用言語
UCCScript ucc = @"First Test End
$Test In <""a"" Is """" End";
ucc.Compile();
Codes["Program"] = @"";/* サンプルコード $@"use rsh.System;
entry Main() <- {{
Disp:>WriteLn(""Hello, World!"");
}};";*/
Codes["A"] = "";
Code.Document.Blocks.Clear();
PushCode(Code, Codes["Program"]);
}
private string RichTextBoxText(RichTextBox Box) => new TextRange(Box.Document.ContentStart, Box.Document.ContentEnd).Text;
private void PushCode(RichTextBox Box, object text) // RichTextBoxにファイルの文字を書き込む
{
Box.Document.Blocks.Clear();
ForEach((text.ToString() ?? "").Replace("\r", "").Split('\n'), s => Box.Document.Blocks.Add(new Paragraph(new Run(s))));
ForEach(Box.Document.Blocks.ToArray(), b => ((Paragraph)b).LineHeight = 0.1d);
}
private void AddFile(string name) // ファイルの追加
{
FileName.Children.Add(CTB(name));
FileSelect.Children.Add(CRB());
}
private RadioButton CRB() // ファイル選択用のラジオボタン
{
RadioButton RB = new()
{
Width = 22.5,
Height = 22.5,
Content = $"{FileSelect.Children.Count}",
IsChecked = FileSelect.Children.Count == 0 ? true : false
};
RB.Checked += RadioButtonChecked;
return RB;
}
private void RadioButtonChecked(object sender, RoutedEventArgs e) // ファイルの選択
{
PushCode(Code, Codes[((TextBox)FileName.Children[int.Parse(((RadioButton)sender).Content.ToString() ?? "")]).Text]);
}
private TextBox CTB(string name) => new() // ファイル名
{
Foreground = new SolidColorBrush(Colors.White),
Text = name,
Background = new SolidColorBrush(Colors.Transparent),
Margin = new Thickness(0, 0, 0, 0),
Height = 20,
Width = 100
};
private void WindowMove(object sender, MouseButtonEventArgs e) => DragMove();
private void WindowClose(object sender, RoutedEventArgs e) => Close();
private void WindowToNomal(object sender, EventArgs e) => WindowState = WindowState == WindowState.Normal ? WindowState = WindowState.Maximized : WindowState = WindowState.Normal;
private void WindowMinimize(object sender, EventArgs e) => WindowState = WindowState.Minimized;
private void SizeChange(object sender, EventArgs e)
{
Code.Height = Height - 40;
Code.Width = (int)(Width * ((float)5 / 11));
DesignBack.Width = Width - 150;
DesignFront.Width = Width - 150;
Canvas.SetLeft(FontSize, Code.Width);
Canvas.SetLeft(FileSelect, Code.Width + 3);
Canvas.SetLeft(FileName, Code.Width + FileSelect.Width + 3);
Canvas.SetLeft(SAVE, Code.Width + FontSize.Width);
Canvas.SetLeft(FileDirectory, SAVE.Width + Code.Width + FontSize.Width);
}
private void FontSizeChange(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
int size = int.Parse(Regex.Match(FontSize.Text, @"([1-9][0-9]?[0-9]?)\s*%").Groups[1].Value);
Code.FontSize = 12 * (size / 100d);
}
}
private void Save(object sender, RoutedEventArgs e) => _ = Codes[((TextBox)FileName.Children[new int[FileSelect.Children.Count].Select((_, i) => i).First(i => ((RadioButton)FileSelect.Children[i]).IsChecked == true)]).Text] = RichTextBoxText(Code);
}
}
Xaml
<Window x:Class="XTec2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="XTec" WindowStyle="None" Height="600" Width="1100" SizeChanged="SizeChange" MinWidth="600" MinHeight="600">
<Window.Background>
<SolidColorBrush Color="#323232"/>
</Window.Background>
<Grid>
<Canvas>
<Label Name="DesignBack" Background="CornflowerBlue"
Canvas.Top="0"
Canvas.Left="150"
Width="950" Height="40"/>
<Label Name="DesignFront" Background="CadetBlue"
Canvas.Top="0"
Canvas.Left="150"
Width="950" Height="30"/>
</Canvas>
<!-- Tittle -->
<Label Background="CadetBlue"
VerticalAlignment="Top" HorizontalAlignment="Left"
Width="150" Height="40"
FontSize="20" Foreground="White" FontWeight="Bold"/>
<Image Source="pack://application:,,,/XTec.png" Width="230" Height="40" VerticalAlignment="Top" HorizontalAlignment="Left"/>
<StackPanel Background="Transparent" MouseLeftButtonDown="WindowMove" Width="Auto" Height="Auto"/>
<Canvas>
<!-- Window -->
<Button Background="CadetBlue"
Canvas.Top="0" Canvas.Right="88"
Width="45" Height="30"
Content="ー"
Foreground="White"
Click="WindowMinimize"
BorderBrush="White"/>
<Button Background="CadetBlue"
Canvas.Top="0" Canvas.Right="44" Margin="0"
Width="45" Height="30"
Content="□"
Foreground="White"
Click="WindowToNomal"
BorderBrush="White"/>
<Button Width="45" Height="30"
Canvas.Top="0" Canvas.Right="0"
Background="CadetBlue" Foreground="White"
Margin="0"
Content="X"
Click="WindowClose"
BorderBrush="White"/>
<!-- Controll -->
<RichTextBox Name="Code"
Width="630" Height="560"
Canvas.Top="40" Canvas.Left="0"
Foreground="White" FontSize="12"
AcceptsTab="True">
<FlowDocument>
<Paragraph LineHeight="0.01"/>
</FlowDocument>
<RichTextBox.Background>
<SolidColorBrush Color="#242424"/>
</RichTextBox.Background>
</RichTextBox>
<TextBox Name="FontSize" Canvas.Top="40" Canvas.Left="630" Width="40" Height="15" Foreground="White" Text="100 %" KeyDown="FontSizeChange">
<TextBox.Background>
<SolidColorBrush Color="#242424"/>
</TextBox.Background>
</TextBox>
<Button Name="SAVE" Content="Save" Foreground="White" Click="Save" Width="50" Height="15" Canvas.Top="40" Canvas.Left="670" FontSize="9">
<Button.Background>
<SolidColorBrush Color="#0058FF"/>
</Button.Background>
</Button>
<TextBox Name="FileDirectory" Canvas.Top="40" Canvas.Left="720" Width="100" Height="15"/>
<!-- Files -->
<StackPanel Name="FileSelect" Canvas.Top="65" Canvas.Left="633" Height="535" Width="20" Background="Transparent" Orientation="Vertical"/>
<StackPanel Name="FileName" Canvas.Top="65" Canvas.Left="653" Height="535" Width="100" Background="Transparent" Orientation="Vertical"/>
</Canvas>
</Grid>
</Window>
自分で試したこと
入力したのがタブかどうかを調べるために、テキストのタブを入力したところ取り出して判定してみましたがFalse
となりました。
0