locked
What was the novice mistake for Radio Button use?

    Question

  • In an first attempt at a Hello World, I don't understand why both of two radio buttons ->IsChecked property returns true.

    I probably don't understand something basic.

    The XAML is:

            <StackPanel HorizontalAlignment="Left" Height="209" Margin="151,55,0,0" VerticalAlignment="Top" Width="290">
                <Button x:Name="Hello" Content="Hello Button" HorizontalAlignment="Left" Width="152" ToolTipService.ToolTip="This button will display the appropriate text below." Click="HelloClick"/>
                <RadioButton x:Name="WorldRadioButton" Content="World RadioButton" HorizontalAlignment="Left" Width="187" GroupName="LocationGroup" IsChecked="True" Click="WorldClick"/>
                <RadioButton x:Name="CityRadioButton" Content="City RadioButton" HorizontalAlignment="Left" Width="187" GroupName="LocationGroup" Click="CityClick"/>
                <TextBlock x:Name="MessageOutput" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock for message" Width="290"/>
            </StackPanel>
    


    The code which executes after the button is clicked is:

    void BlankMetro::BlankPage::HelloClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	this->MessageOutput->Text= "Hello";
    
    	if ( this->WorldRadioButton->IsChecked ) {
    		this->MessageOutput->Text += " World";
    	}
    
    	if ( this->CityRadioButton->IsChecked ) {
    		this->MessageOutput->Text += " City";
    	}
    
    	// Observed result is "Hello World City"
    }

    The intent was that since only one radio button was checked, that either "Hello World" or "Hello City" would be placed in the TextBlock.

    However, the actual text was "Hello World City", which indicates that both IF statements were true.

    I can click on one radio button and the other becomes unchecked.

    Any clues will be helpful.

    Friday, May 18, 2012 2:08 AM

Answers

  • The XAML above still has the XAML event handler listed for Checked. Did you remove the event handler in code but forget to remove it in XAML? The parser will attempt to hook up the event that it sees in XAML to code and will fail if not found.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"
    • Marked as answer by Andrew7Webb Friday, June 01, 2012 2:21 AM
    Friday, May 18, 2012 8:25 PM

All replies

  • RadioButton.IsChecked is a nullable Boolean type so I'm not sure if you have to do something special in your check. Note that in the docs it returns a IReference<bool>^.

    Hope this helps,
    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"

    Friday, May 18, 2012 6:37 AM
  • Ok, I removed my click handlers, and added empty Check handlers from the Xaml by deleting the name of the handler, and by removing the .cpp definition.  Now the program crashes.   I remove the .h entries for the Click, rebuild and it crashes.  I clean and it still crashes,   I exit VS, reload the project and rebuild and it crashes.

    The error was:  Unhandled exception at 0x003145A4 in BlankMetro.exe  0xC0000005: Access violation reading location 0x00000000.

    The error occurred on LoadComponent.

    void BlankPage::InitializeComponent()
    {
        if (_contentLoaded)
            return;
    
        _contentLoaded = true;
    
        // Call LoadComponent on ms-appx:///BlankPage.xaml
        Windows::UI::Xaml::Application::LoadComponent(this, ref new Windows::Foundation::Uri("ms-appx:///BlankPage.xaml"), Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation::Application);

    0038BEA1  call        Windows::Foundation::Uri::Uri (01DD3D1h)  
    0038BEA6  add         esp,8  
    0038BEA9  mov         dword ptr [ebp-1F0h],eax  
    0038BEAF  mov         byte ptr [ebp-4],1  
    0038BEB3  mov         edx,dword ptr [ebp-1F0h]  
    0038BEB9  mov         dword ptr [ebp-208h],edx  
    0038BEBF  mov         byte ptr [ebp-4],2  
    0038BEC3  push        0  
    0038BEC5  mov         eax,dword ptr [ebp-208h]  
    0038BECB  push        eax  
    0038BECC  mov         ecx,dword ptr [this]  
    0038BECF  push        ecx  
    0038BED0  call        Windows::UI::Xaml::Application::LoadComponent (01D6941h)  
    0038BED5  add         esp,0Ch  
    
    And 
    		ECX	00000000	
    		ESP	0333E144	
    		EBP	0333E36C	

    Since ECX is 0, I am guessing that ref new Windows::Foundation::Uri("ms-appx:///BlankPage.xaml") returned 0.

    I guess that implies that something is wrong either in the XAML or in the state surrounding the XAML.   I especially avoided editing the XAML text manually at all in this project.

     The XAML is now:

        <Grid Background="{StaticResource ApplicationPageBackgroundBrush}">
            <StackPanel HorizontalAlignment="Left" Height="209" Margin="151,55,0,0" VerticalAlignment="Top" Width="290">
                <Button x:Name="Hello" Content="Hello Button" HorizontalAlignment="Left" Width="152" ToolTipService.ToolTip="This button will display the appropriate text below." Click="HelloClick"/>
                <RadioButton x:Name="WorldRadioButton" Content="World RadioButton" HorizontalAlignment="Left" Width="187" GroupName="LocationGroup" IsChecked="True" Checked="WorldCheck"/>
                <RadioButton x:Name="CityRadioButton" Content="City RadioButton" HorizontalAlignment="Left" Width="187" GroupName="LocationGroup" Checked="CityCheck"/>
                <TextBlock x:Name="MessageOutput" HorizontalAlignment="Left" TextWrapping="Wrap" Text="TextBlock for message" Width="290"/>
            </StackPanel>
    
        </Grid>



    • Edited by Andrew7Webb Friday, May 18, 2012 12:43 PM Add info.
    Friday, May 18, 2012 12:36 PM
  • The XAML above still has the XAML event handler listed for Checked. Did you remove the event handler in code but forget to remove it in XAML? The parser will attempt to hook up the event that it sees in XAML to code and will fail if not found.

    -mark
    Program Manager
    Microsoft
    This post is provided "as-is"
    • Marked as answer by Andrew7Webb Friday, June 01, 2012 2:21 AM
    Friday, May 18, 2012 8:25 PM
  • I have isolated the problem to one clause in the XAML:  IsChecked="True" 

    <RadioButtonx:Name="WorldRadioButton"Content="World RadioButton"HorizontalAlignment="Left"Width="187"IsChecked="True"Checked="WorldCheck"/>

    does get the error explained above.   I removed the   IsChecked="True"  and the crash went away.  I return the IsChecked="True" and the crash returns.

    void BlankMetro::BlankPage::HelloClick(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	this->MessageOutput->Text= "Hello";
    
    	if ( this->WorldRadioButton->IsChecked->Value ) {
    		this->MessageOutput->Text += " World";
    	}
    
    	if ( this->CityRadioButton->IsChecked->Value ) {
    		this->MessageOutput->Text += " City";
    	}
    
    	// Observed result is "Hello World City"
    }
    
    
    
    void BlankMetro::BlankPage::WorldCheck(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	this->MessageOutput->Text += " w_check";
    }
    
    
    void BlankMetro::BlankPage::CityCheck(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
    	this->MessageOutput->Text += " c_check";
    }

    Quite strange, I happened to try WindowsKey Tab, and noticed a old program running.  After I closed it with Right Close, the problems I had went away.  Perhaps somehow the old program was being invoked again? 


    Friday, May 18, 2012 8:37 PM