locked
Struggling with radiobutton "Checked" RRS feed

  • Question

  • I'm having some difficulty (as a newbie), in getting radiobuttons to control the text displayed in a textblock.

    the code that i've written so far, using visual studio 2010 is:

    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();

            }
            private void radioButton1_Checked(object sender, RoutedEventArgs e)
            {
                if (radioButton1.Checked)
                {
                    textBlock19.Text = "4";
                }
                else if (radioButton2.Checked)
                {
                    textBlock19.Text = "3";
                }
                else if (radioButton3.Checked)
                {
                    textBlock19.Text = "2";
                }
                else if (radioButton4.Checked)
                {
                    textBlock19.Text = "1";
                }
            }

        }
    }

    The words Checked all generate an error saying that 'System.Windows.Controls.Primitives.Togglebutton.Checked' can only appear on the left hand side of += or -=

    Thank you for help,

    Best Wishes

    Andrew

    • Moved by Bob Shen Tuesday, September 4, 2012 8:58 AM (From:Visual C# General)
    Saturday, September 1, 2012 8:18 PM

Answers

All replies

  • I think you put it wrong, you only use one event - and this is checked of radioButton1.

    I think you want to set textBlock19`s Text property some values (numbers) depending on which radiobutton is checked.

    This you can do simply by creating one common event for all radioButtons, but using CheckedChanged event in the following way:

            public Form1()
            {
                InitializeComponent();           
              
                RadioButton[] rbs = new[] { radioButton1, radioButton2, radioButton3, radioButton4 };
                foreach (var c in rbs)
                    c.CheckedChanged += new EventHandler(c_CheckedChanged); //common event for all 4 radiobuttons
            }
    
            private void c_CheckedChanged(object sender, EventArgs e)
            {
                RadioButton r = sender as RadioButton;
                if (r != null)
                {
                    if (r.Checked)
                    {
                        switch (r.Name)
                        {
                            case "radioButton1": textBox1.Text = "4"; break;
                            case "radioButton2": textBox1.Text = "3"; break;
                            case "radioButton3": textBox1.Text = "2"; break;
                            case "radioButton4": textBox1.Text = "1"; break;
                        }
                    }
                }
            }


    Mitja

    Saturday, September 1, 2012 8:56 PM
  • This looks like WPF, not WinForms. Checked in a WPF form is the event. What you want to test is the .IsChecked property:

    if (RadioButton1.IsChecked)

    textBlock19.Text = "4";


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    • Proposed as answer by Thorsten Gudera Saturday, September 1, 2012 9:46 PM
    Saturday, September 1, 2012 9:19 PM
  • First of all I'c like to say thankyou for your time in helping me with this - I am very grateful.

    I've got to the point of the code below and am stuck with just one line of it:

         c.IsChecked += new EventHandler(c_IsChecked);

    it generates the errors :'cannot implicitly convert type 'System.EventHandler' to type'bool?'

    and += cannot be used between the operands of System.EventHandler and bool?

    Thanks Again,

    Andrew

    {
        public partial class MainPage : PhoneApplicationPage
        {
            private EventHandler C_IsChecked;
            // Constructor
            public MainPage()
            {
                InitializeComponent();

                RadioButton[] rbs = new[] { radioButton1, radioButton2, radioButton3, radioButton4 };
                foreach (var c in rbs)
                    c.IsChecked += new EventHandler(c_IsChecked);
            }

            private void c_IsChecked(object sender, EventArgs e)
            {
                RadioButton r = sender as RadioButton;
                if (r != null)
                {
                    if (r.IsChecked ?? false)
                    {
                        switch (r.Name)
                        {
                            case "radioButton1": textBlock1.Text = "4"; break;
                            case "radioButton2": textBlock1.Text = "3"; break;
                            case "radioButton3": textBlock1.Text = "2"; break;
                            case "radioButton4": textBlock1.Text = "1"; break;
                        }
                    }
                }
            }
        }
    }

    Saturday, September 1, 2012 10:10 PM
  • IsChecked is a property, not an event. In my example I use CheckedChanged event! Maybe you want to other event, but surely not IsChecked - which is as said , a property.

    Mitja

    • Marked as answer by Annabella Luo Monday, September 10, 2012 3:36 AM
    Saturday, September 1, 2012 10:17 PM
  • Andrew, you had it partially right to begin with. In your original post, you correctly had the event handler like this:

    private void radioButton1_Checked(object sender, RoutedEventArgs e)

    Which, I assume was set up in your xaml, since you didn't show any code for it in your first post. 

    It looks like you're mixing-and-matching pieces of my post and pieces of Mitja's post. All you have to do, in the eventhandler you had in your original post was change this:

    if (RadioButton1.Checked)

    to this:

    if (RadioButton1.IsChecked)


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, September 1, 2012 10:19 PM
  • Thanks again -

    Unfortunately I'm still really stuck - I'm trying to understand things as I go - hence why this is taking me so long.

    Now when I debug the code below I get nullreferenceexception error  from the lines :

    if ((bool)radioButton1.IsChecked )

    if ((bool)radioButton2.IsChecked)

    if ((bool)radioButton3.IsChecked )

    and

    if ((bool)radioButton4.IsChecked )

    the error suggests using the 'new' keyword but I can't work out what the problem is or how i can fix it.

    Code:

    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();

            }
            private void radioButton1_Checked(object sender, RoutedEventArgs e)
            {
                if ((bool)radioButton1.IsChecked )
                {
                    textBlock19.Text = "4";
                }
                else if ((bool)radioButton2.IsChecked)
                {
                    textBlock19.Text = "3";
                }
                else if ((bool)radioButton3.IsChecked)
                {
                    textBlock19.Text = "2";
                }
                else if ((bool)radioButton4.IsChecked)
                {
                    textBlock19.Text = "1";
                }
            }

        }
    }

    Saturday, September 1, 2012 11:23 PM
  • Do you still have those RadioButtons defined in your XAML?


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, September 1, 2012 11:39 PM
  • Hi AndrewBennett,

    You original question is that you add eventhandler incorrect. In your code:

        foreach (var c in rbs) 
                    c.IsChecked += new EventHandler(c_IsChecked);

    you want to add event handler to IsChecked property(bool type), however IsChecked is even not a event, you can't add event hander to a property. Instead of the IsChecked property, you can handle the Checked event of RadioButton to achieve your goal. For detail information about RadioButton, please check out below links for your reference:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.radiobutton.aspx

    http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.checked.aspx

    Bonca's first reply is a clear Answer for your question in face.

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Annabella Luo Monday, September 10, 2012 3:36 AM
    Wednesday, September 5, 2012 9:05 AM
  • You know, I really disagree with marking your own post as an answer. I think these forums should be set up to not allow that. Just my 2 cents ...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Monday, September 10, 2012 4:36 AM
  • Hi BonnieB,

    Thank you for your suggestion, but all code in AndrewBennett's original question is talking about the thing I post. He want to complete the code, and I try to help him complete the code.

    And AndrewBennett, if you have any concerns about the mark, please feel free to unmark this.

    Thank you.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 10, 2012 5:11 AM