「東雲 忠太郎」の平凡な日常のできごと

「東雲 忠太郎」の平凡な日常のできごと

2024.05.03
XML
カテゴリ: C#.NET


C# WPF で RichTextBox のテキストを取得するためには、RichTextBox の内容を操作する方法を利用します。以下は、RichTextBox のテキストを取得するためのサンプルコードです。


```csharp

using System.Windows;

using System.Windows.Controls;


namespace WpfApp

{

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

        }


        private void Button_Click(object sender, RoutedEventArgs e)

        {

            // RichTextBox のテキストを取得します

            string text = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd).Text;

            MessageBox.Show(text);

        }

    }

}

```


XAML ファイルには、RichTextBox と Button を配置しておきます。


```xml

<Window x:Class="WpfApp.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="450" Width="800">

    <Grid>

        <RichTextBox x:Name="richTextBox" HorizontalAlignment="Left" VerticalAlignment="Top" Height="200" Width="300"/>

        <Button Content="Get Text" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="320,10,0,0" Click="Button_Click"/>

    </Grid>

</Window>

```


この例では、ボタンがクリックされると、RichTextBox の内容が取得されてメッセージボックスに表示されます。 TextRange クラスを使用して、RichTextBox の内容を取得しています。






お気に入りの記事を「いいね!」で応援しよう

Last updated  2024.05.03 16:37:43


【毎日開催】
15記事にいいね!で1ポイント
10秒滞在
いいね! -- / --
おめでとうございます!
ミッションを達成しました。
※「ポイントを獲得する」ボタンを押すと広告が表示されます。
x

© Rakuten Group, Inc.
Create a Mobile Website
スマートフォン版を閲覧 | PC版を閲覧
Share by: