読者です 読者をやめる 読者になる 読者になる

英語学習のためのアプリを作る

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="英語問題" Height="350" Width="816" Initialized="Window_Initialized">
    <Grid>
        <TextBlock x:Name="textblock1" HorizontalAlignment="Left" Margin="42,38,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="111" Height="24" Grid.ColumnSpan="2"/>
        <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="122" Margin="42,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="715" TextChanged="textbox1_TextChanged" FontSize="18"/>
        <Button x:Name="sound" Content="音声" Margin="40,245,0,0" VerticalAlignment="Top" Height="24" IsEnabled="False" Click="sound_Click" HorizontalAlignment="Left" Width="104"/>
        <Button x:Name="next" Content="新しい問題" HorizontalAlignment="Left" Margin="190,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="next_Click"/>
        <Button x:Name="again" Content="やり直し" HorizontalAlignment="Left" Margin="340,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="again_Click"/>
        <Button x:Name="answer" Content="正解をみる" HorizontalAlignment="Left" Margin="490,245,0,0" VerticalAlignment="Top" Width="104" Height="24" Click="answer_Click" IsEnabled="False"/>
    </Grid>
</Window>

Window1.xaml

<Window x:Class="WpfApplication1.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="解答" Height="300" Width="820">
    <Grid>
        <TextBox x:Name="textbox1" HorizontalAlignment="Left" Height="122" Margin="42,87,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="715" FontSize="18">
        </TextBox>
    </Grid>
</Window>

Main.Window.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.IO;
using System.Diagnostics;

namespace WpfApplication1
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        string[] all_data;
        string[] kugiri = { "\r\n" };
        int all_q;
        int q_num;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Initialized(object sender, EventArgs e)
        {
            string txt = File.ReadAllText("english.txt",System.Text.Encoding.GetEncoding("Shift_JIS"));
            all_data = txt.Split(kugiri, StringSplitOptions.RemoveEmptyEntries);
            all_q = all_data.Length;
            next.Focus();
        }

        private void textbox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (textbox1.Text == all_data[q_num])
            {
                MessageBox.Show("正解");
                next.Focus();
            }
        }

        private void next_Click(object sender, RoutedEventArgs e)
        {
            sound.IsEnabled = true;
            answer.IsEnabled = true;
            Random rand = new Random();
            q_num = rand.Next(all_q);
            textblock1.Text = (q_num + 1).ToString();
            textbox1.Text = "";
            textbox1.Focus();
        }

        private void sound_Click(object sender, RoutedEventArgs e)
        {
            string pass;
            pass = (q_num + 1).ToString() + ".mp3";
            Process.Start(pass);
        }

        private void again_Click(object sender, RoutedEventArgs e)
        {
            textbox1.Text = "";
            textbox1.Focus();
        }

        private void answer_Click(object sender, RoutedEventArgs e)
        {
            Window1 win = new Window1();
            win.textbox1.Text = all_data[q_num];
            win.ShowDialog();
        }
    }
}