<Window x:Class="WpfMathQuiz.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" xmlns:self="clr-namespace:WpfMathQuiz" Title="MainWindow" Height="350" Width="525"> <Grid > <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Label Content="Time Left" Grid.Column="2" HorizontalAlignment="Center" Name="Label1" VerticalAlignment="Center" /> <Label Content="" HorizontalAlignment="Center" Name="timeLabel" VerticalAlignment="Center" Grid.Column="3" Grid.ColumnSpan="2" MinWidth="150" /> <Label Content="?" HorizontalAlignment="Center" Name="plusLeftLabel" VerticalAlignment="Center" Grid.Row="1" /> <Label Content="+" HorizontalAlignment="Center" Name="Label4" VerticalAlignment="Center" Grid.Column="1" Grid.Row="1" /> <Label Content="?" HorizontalAlignment="Center" Name="plusRightLabel" VerticalAlignment="Center" Grid.Column="2" Grid.Row="1" /> <Label Content="=" HorizontalAlignment="Center" Name="Label6" VerticalAlignment="Center" Grid.Column="3" Grid.Row="1" /> <Label Content="?" HorizontalAlignment="Center" Name="minusLeftLabel" VerticalAlignment="Center" Grid.Row="2" /> <Label Content="-" HorizontalAlignment="Center" Name="Label8" VerticalAlignment="Center" Grid.Column="1" Grid.Row="2" /> <Label Content="?" HorizontalAlignment="Center" Name="minusRightLabel" VerticalAlignment="Center" Grid.Column="2" Grid.Row="2" /> <Label Content="=" HorizontalAlignment="Center" Name="Label10" VerticalAlignment="Center" Grid.Column="3" Grid.Row="2" /> <Label Content="?" HorizontalAlignment="Center" Name="timesLeftLabel" VerticalAlignment="Center" Grid.Row="3" /> <Label Content="×" HorizontalAlignment="Center" Name="Label12" VerticalAlignment="Center" Grid.Column="1" Grid.Row="3" /> <Label Content="?" HorizontalAlignment="Center" Name="timesRightLabel" VerticalAlignment="Center" Grid.Column="2" Grid.Row="3" /> <Label Content="=" HorizontalAlignment="Center" Name="Label14" VerticalAlignment="Center" Grid.Column="3" Grid.Row="3" /> <Label Content="?" HorizontalAlignment="Center" Name="dividedLeftLabel" VerticalAlignment="Center" Grid.Row="4" /> <Label Content="÷" HorizontalAlignment="Center" Name="Label16" VerticalAlignment="Center" Grid.Column="1" Grid.Row="4" /> <Label Content="?" HorizontalAlignment="Center" Name="dividedRightLabel" VerticalAlignment="Center" Grid.Column="2" Grid.Row="4" /> <Label Content="=" HorizontalAlignment="Center" Name="Label18" VerticalAlignment="Center" Grid.Column="3" Grid.Row="4" /> <TextBox Grid.Column="4" Grid.Row="1" Height="24" HorizontalAlignment="Center" Name="sum" VerticalAlignment="Center" MinWidth="60" /> <TextBox Grid.Column="4" Grid.Row="2" Height="24" HorizontalAlignment="Center" Name="difference" VerticalAlignment="Center" MinWidth="60" /> <TextBox Grid.Column="4" Grid.Row="3" Height="24" HorizontalAlignment="Center" Name="product" VerticalAlignment="Center" MinWidth="60" /> <TextBox Grid.Column="4" Grid.Row="4" Height="24" HorizontalAlignment="Center" Name="quotient" VerticalAlignment="Center" MinWidth="60" /> <Button Content="Start the quiz" Grid.Column="2" Grid.Row="5" HorizontalAlignment="Center" Name="startButton" VerticalAlignment="Center" Click="startButton_Click" /> <WindowsFormsHost Name="WindowsFormsHost1"> <self:UserControl1 x:Name="myControl" /> </WindowsFormsHost> </Grid> </Window>
using System; using System.Windows; namespace WpfMathQuiz { public partial class MainWindow : Window { Random randomizer = new Random(); int addend1; int addend2; int minuend; int subtrahend; int multiplicand; int multiplier; int dividend; int divisor; int timeLeft; public MainWindow() { InitializeComponent(); } private void startButton_Click(object sender, RoutedEventArgs e) { StartTheQuiz(); startButton.IsEnabled = false; myControl.UC = this; } public void StartTheQuiz() { // Fill in the addition problem. addend1 = randomizer.Next(51); addend2 = randomizer.Next(51); plusLeftLabel.Content = addend1.ToString(); plusRightLabel.Content = addend2.ToString(); sum.Text = "0"; // Fill in the subtraction problem. minuend = randomizer.Next(1, 101); subtrahend = randomizer.Next(1, minuend); minusLeftLabel.Content = minuend.ToString(); minusRightLabel.Content = subtrahend.ToString(); difference.Text = "0"; // Fill in the multiplication problem. multiplicand = randomizer.Next(2, 11); multiplier = randomizer.Next(2, 11); timesLeftLabel.Content = multiplicand.ToString(); timesRightLabel.Content = multiplier.ToString(); product.Text = "0"; // Fill in the division problem. divisor = randomizer.Next(2, 11); int temporaryQuotient = randomizer.Next(2, 11); dividend = divisor * temporaryQuotient; dividedLeftLabel.Content = dividend.ToString(); dividedRightLabel.Content = divisor.ToString(); quotient.Text = "0"; // Start the timer. timeLeft = 30; timeLabel.Content = "30 seconds"; myControl.Timer1.Start(); } private bool CheckTheAnswer() { if ((addend1 + addend2 == Convert.ToInt32(sum.Text)) && (minuend - subtrahend == Convert.ToInt32(difference.Text)) && (multiplicand * multiplier == Convert.ToInt32(product.Text)) && (dividend / divisor == Convert.ToInt32(quotient.Text))) { return true; } else { return false; } } public void Timer_Tick(object sender, EventArgs e) { if (CheckTheAnswer()) { myControl.Timer1.Stop(); MessageBox.Show("You got all the answers right!", "Congratulations"); startButton.IsEnabled = true; } else if (timeLeft > 0) { timeLeft--; timeLabel.Content = timeLeft + " seconds"; } else { myControl.Timer1.Stop(); timeLabel.Content = "Time's up!"; MessageBox.Show("You didn't finish in time.", "Sorry"); sum.Text = Convert.ToString( addend1 + addend2); difference.Text = Convert.ToString(minuend - subtrahend); product.Text =Convert.ToString( multiplicand * multiplier); quotient.Text =Convert.ToString( dividend / divisor); startButton.IsEnabled = true; } } } }
using System; using System.Windows.Forms; namespace WpfMathQuiz { public partial class UserControl1 : UserControl { public MainWindow UC; public UserControl1() { InitializeComponent();} private void Timer1_Tick(object sender, EventArgs e) { UC.Timer_Tick(sender, e);} } }