WPF計算クイズの作成]をダウンロードする。
<Window x:Class="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" />
<WindowsFormsHost Name="WindowsFormsHost1">
<self:UserControl1 x:Name="myControl" />
</WindowsFormsHost>
</Grid>
</Window>
Class MainWindow
Dim randomizer As New Random
Dim addend1 As Integer
Dim addend2 As Integer
Dim minuend As Integer
Dim subtrahend As Integer
Dim multiplicand As Integer
Dim multiplier As Integer
Dim dividend As Integer
Dim divisor As Integer
Dim timeLeft As Integer
Private Sub startButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles startButton.Click
startButton.IsEnabled = False
StartTheQuiz()
myControl.UC = Me
End Sub
Public Sub 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)
Dim temporaryQuotient As Integer = 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()
End Sub
Public Function CheckTheAnswer() As Boolean
If ((addend1 + addend2 = Val(sum.Text)) AndAlso (minuend - subtrahend = Val(difference.Text)) AndAlso (multiplicand * multiplier = Val(product.Text)) AndAlso (dividend / divisor = Val(quotient.Text))) Then
Return True
Else
Return False
End If
End Function
Public Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
If CheckTheAnswer() Then
myControl.Timer1.Stop()
MessageBox.Show("You got all of the answers right!", "Congratulations!")
startButton.IsEnabled = True
ElseIf timeLeft > 0 Then
timeLeft = timeLeft - 1
timeLabel.Content = timeLeft & " seconds"
Else
myControl.Timer1.Stop()
timeLabel.Content = "Time's up!"
MessageBox.Show("You didn't finish in time.", "Sorry")
sum.Text = Str(addend1 + addend2)
difference.Text = Str(minuend - subtrahend)
product.Text = Str(multiplicand * multiplier)
quotient.Text = Str(dividend / divisor)
startButton.IsEnabled = True
End If
End Sub
End Class
Public Class UserControl1
Public UC As MainWindow
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
UC.Timer_Tick(sender, e)
End Sub
End Class