Home > DeveloperSection > Forums > Radio Button Control Errors In WP8
ezra heywood
ezra heywood

Total Post:145

Points:1019
Posted on    October-20-2014 11:48 PM

 C# C#  Exception 
Ratings:


 1 Reply(s)
 623  View(s)
Rate this:

I am trying to add a radio button check to my project but finding it difficult, can someone kindly help

Xaml

......................

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<ScrollViewer>
<StackPanel>
<TextBlock x:Name="lblQuestion" Height="100" TextWrapping="Wrap" Margin="0,10,0,547"/>
<RadioButton x:Name="radA" Height="100" Margin="0,-570,0,389" />
<RadioButton x:Name="radB" Height="100" Margin="0,-520,0,184"/>
<RadioButton x:Name="radC" Height="100" Margin="0,-460,0,34"/>
<RadioButton x:Name="radD" Height="100" Margin="0,-373,0,-116"/>

<Button x:Name="btnStart" Visibility="Visible" IsEnabled="True" Content="Start" HorizontalAlignment="Left" Margin="70,-70,0,0" VerticalAlignment="Top" Click="Button_Click" Grid.ColumnSpan="2"/>
<Button x:Name="btnSubmit" Visibility="Visible" IsEnabled="True" Content="Submit" HorizontalAlignment="Right" Margin="0,-70,70,0" VerticalAlignment="Top" Grid.Column="1" Grid.ColumnSpan="2" Click="btnSubmit_Click" />

<TextBlock x:Name="txtScore" TextWrapping="Wrap" Height="61" Width="378"/>

</StackPanel>
</ScrollViewer>

</Grid>

Xaml.cs

........................

int score = 0;
int i = -1;
int a = 0;

string[] questions = new string[]
{
"1. Which Use of English Question Paper Type is given to you?", 
"2. According to the writer, the exciting thing about Spaceship Earth is that it ",
"3. The absence of instruction manual in Spaceship Earth has ",
" 4. From the passage, it can be deduced that man ",
"5. The writer's mood in the passage is that of ",
};

string[] answers = new string[] {
"A TypeA ", "B. Type B ", "C. Type C ", "D. Type D ", 
"A has no instruction manual ", "B. is not difficult to maintain ", "C. is peculiar to other automobiles ", "D. is relatively easy to operate ", 
"A made the operation of the Spaceship Earth difficult. ", "B. forced man to devise other means \r\n of travelling in Spacecraft", "C. challenged man's inquisitiveness", "D. made man helpless.", 
"A learns by experiment and deduction ","B. learns by chance and accidentally ","C. is incapable of solving \r\n all his problems ","D. by his nature is in \r\n constant search of knowlege ",
"A non - commital ", "B. pessimism ", "C. optimism ", "D. frOstration ",

};

string[] quizAnswers = new string[] { "A TypeA", "A has no instruction manual", "C. challenged man's inquisitiveness", "A learns by experiment and deduction", "C. optimism ", };

private void Button_Click(object sender, RoutedEventArgs e)
{
if (i < questions.Length)
i++;
//txtScore.Text = score;

lblQuestion.Text = questions[i];

radA.Content = answers[a];
a++;
radB.Content = answers[a];
a++;
radC.Content = answers[a];
a++;
radD.Content = answers[a];
a++;


btnStart.IsEnabled = true;

btnSubmit.IsEnabled = true;


}

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
if (getSelectedAnswer().Equals(quizAnswers[i]))
{
MessageBox.Show("Correct");
score++;
txtScore.Text = Convert.ToString(score);
btnSubmit.IsEnabled = false;


btnStart.IsEnabled = true;
btnStart.Content = "Next";
}

else
{
MessageBox.Show("Incorrect");
score--;
txtScore.Text = Convert.ToString(score);
btnSubmit.IsEnabled = false;


btnStart.IsEnabled = true;
btnStart.Content = "Next";
}

}

string getSelectedAnswer()
{
if (radA.Checked)
return radA.Text.ToString();
if (radB.Checked)
return radB.Text.ToString();
if (radC.Checked)
return radC.Text.ToString();
if (radD.Checked)
return radD.Text.ToString();
return "";
}



lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Posted on    October-20-2014 11:49 PM

I'm not familiar with WP8 programming as such but, based on my knowledge of WPF, I'd guess that your getSelectedAnswer method should be coded as follows:

string getSelectedAnswer()
{
   if (radA.IsChecked) // IsChecked rather than Checked
      return radA.Content.ToString(); // Content rather than Text
   if (radB.IsChecked)
      return radB.Content.ToString();
   if (radC.IsChecked)
      return radC.Content.ToString();
   if (radD.IsChecked)
      return radD.Content.ToString();
   return "";
}

Don't want to miss updates? Please click the below button!

Follow MindStick