テキストボックスに入力された文字列を取得する - WinUI

WinUI 3 アプリケーションでテキストボックスに入力された文字列を取得するコードを紹介します。

概要

WinUI 3でウィンドウに配置したテキストボックスに入力された値を取得したい場合があります。 テキストボックスの値を取得するには、テキストボックスコントロールのTextプロパティを参照します。

書式

(Microsoft.UI.Xaml.Controls.TextBox のインスタンスオブジェクト).Text

プログラム

WinUI 3 アプリケーションを作成します。

コード

以下のコードを記述します。
MainWindow.xaml
<Window
    x:Class="GetTextBoxValue.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:GetTextBoxValue"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

  <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <TextBox Margin="2,2,2,2" x:Name="TextBox1" PlaceholderText="文字列を入力してね" MinWidth="320"/>
    <Button Margin="2,2,2,2" Style="{StaticResource AccentButtonStyle}" x:Name="myButton" Click="myButton_Click">Exec</Button>
    <TextBlock Margin="2,2,2,2" x:Name="TextBlock1"  Text="TextBlock"/>
  </StackPanel>

</Window>
MainWindow.xaml.cs
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;

// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.

namespace GetTextBoxValue
{
  /// <summary>
  /// An empty window that can be used on its own or navigated to within a Frame.
  /// </summary>
  public sealed partial class MainWindow : Window
  {
    public MainWindow()
    {
      this.InitializeComponent();
    }

    private void myButton_Click(object sender, RoutedEventArgs e)
    {
      string GetValueString = TextBox1.Text;
      TextBlock1.Text = GetValueString;
    }
  }
}

解説

StackPanelのコードが下記です。 Orientation="Vertical" を指定しているため、内部のコントロールは縦方向に配置されます。
 
  <StackPanel Orientation="Vertical" HorizontalAlignment="Center" VerticalAlignment="Center">
    <!-- 中略 --> 
  </StackPanel>

StackPanel内部にはテキストボックス、ボタン、テキストブロックのコントロールを配置します。
テキストボックスには、PlaceholderText 属性を追加しプレースホルダーのテキストを表示します。
ボタンはアクセントカラーのスタイルを有効にしています。クリック時には、ウィンドウクラスの myButton_Click() メソッドを呼び出します。
  <TextBox Margin="2,2,2,2" x:Name="TextBox1" PlaceholderText="文字列を入力してね" MinWidth="320"/>
  <Button Margin="2,2,2,2" Style="{StaticResource AccentButtonStyle}" x:Name="myButton" Click="myButton_Click">Exec</Button>
  <TextBlock Margin="2,2,2,2" x:Name="TextBlock1"  Text="TextBlock"/>

ボタンのクリックで下記コードが実行されます。テキストボックスに入力された文字列の値をGetValueString変数に代入し、 GetValueStringの値をテキストブロックに表示する処理を記述しています。
  private void myButton_Click(object sender, RoutedEventArgs e)
  {
    string GetValueString = TextBox1.Text;
    TextBlock1.Text = GetValueString;
  }

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


上部のテキストボックスに文字列を入力します。入力後[Exec]ボタンをクリックします。


ボタンをクリックすると下部のTextBlockコントロールにテキストボックスに入力した文字列が表示されます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2022-08-10
作成日: 2022-08-09
iPentec all rights reserverd.