xamarin radio button

CodingNewUserCodingNewUser INMember ✭✭

Hi,
i Am trying to create a survey form dynamically
foreach (var item in Settings.VOCQuestionDetailsSettings.RespondantQuestionnare)
{

                myGrid.Children.Add(new Label { Text = item.Questions, TextColor = Color.Black, HorizontalOptions = LayoutOptions.FillAndExpand, FontAttributes = FontAttributes.Bold }, 0, rowCount);
                rowCount++;
                for (int i = 0; i < item.Choices.Count; i++)
                {
                    if (item.QuestionType == "Radio")
                    {
                        myGrid.Children.Add(new CustomRadioButton
                        {
                            HorizontalOptions = LayoutOptions.FillAndExpand,
                            VerticalOptions = LayoutOptions.FillAndExpand,
                            Text = item.Choices[i],
                            TextColor = Color.Black,
                            FontSize = 14,
                        }, 0, rowCount);
                        rowCount++;
                    }

}
}

IF The question type is radio i am displaying the radio buttons.However now i need to disable other radio options if i select one.
Please help

Tagged:

Best Answers

Answers

  • EricSchmeckEricSchmeck DEMember ✭✭

    Hi, just store all radio buttons in a list and if one is checked, set all other radio buttons unchecked in the list.

  • CodingNewUserCodingNewUser INMember ✭✭

    Hi Eric,
    Could you help me with the code a bit.

  • EricSchmeckEricSchmeck DEMember ✭✭

    First you need a list:
    var radioButtonsList = new List<CustomRadioButton>();

    When you create a new CustomRadioButton, put it in the list:
    radioButtonsList.Add(customRadioButton);

    And you need an event, like Tapped or Clicked or CheckedChanged. I don't know your CustomRadioButton class.
    customRadioButton.CheckedChanged += (sender, args) =>
    {
    if(!customRadioButton.IsChecked)
    return;

    foreach (var radioButton in radioButtonsList)
    {
    if(radioButton != this)
    radioButton.IsChecked = false;
    }
    };

  • CodingNewUserCodingNewUser INMember ✭✭
    Hi I tried as you suggested:-
    
    public ObservableCollection<CustomRadioButton> Items;   
    
      for (int i = 0; i < item.Choices.Count; i++)
                        {
                            if (item.QuestionType == "Radio")
                            {
                                CustomRadioButton button = new CustomRadioButton
                                {
                                    HorizontalOptions = LayoutOptions.FillAndExpand,
                                    VerticalOptions = LayoutOptions.Fill,
                                    Text = item.Choices[i],
                                    TextColor = Color.Black,
                                    FontSize = 14,
                                    CheckedChanged= CheckedChanged
                                };
                                Items.Add(button);
                                myGrid.Children.Add(button, 0, rowCount);
                                rowCount++;
                            }
    
      private void CheckedChanged(object sender, EventArgs<bool> e)
            {
                if (e.Value == false)
                {
                    return;
                }
    
                var selectedItem = sender as CustomRadioButton;
    
                if (selectedItem == null)
                {
                    return;
                }
    
                foreach (var item in Items)
                {
                    if (item != this) //This line keeps giving an error screenshot attached
                        button.Checked= false;
                }
            }
    
  • CodingNewUserCodingNewUser INMember ✭✭

    namespace XLabs.Forms.Controls
    {

    public class CustomRadioButton : View
    {
    
        public static readonly BindableProperty CheckedProperty;
    
        public static readonly BindableProperty FontNameProperty;
    
        public static readonly BindableProperty FontSizeProperty;
     .
        public static readonly BindableProperty TextColorProperty;
    
        public static readonly BindableProperty TextProperty;
    
        public EventHandler<EventArgs<bool>> CheckedChanged;
    
        public CustomRadioButton();
    
    
        public bool Checked { get; set; }
    
        public string FontName { get; set; }
    
        public double FontSize { get; set; }
    
        public int Id { get; set; }
    
        public string Text { get; set; }
    
        public Color TextColor { get; set; }
    }
    

    }

  • CodingNewUserCodingNewUser INMember ✭✭

    Thank you so much Eric, that worked, but a small doubt how do i group the radio buttons question wise. Currently the radio button in previous question is disabled once i select one in next question

  • EricSchmeckEricSchmeck DEMember ✭✭
    edited May 2018

    You need a new property in your CustomRadioButton class, like GroupName or something like this
    public string GroupName { get; set; }

    and then you can do this:
    foreach (var item in Items.Where(x => x.GroupName == selectedItem.GroupName))
    {
    if (item != selectedItem)
    item.Checked= false;
    }
    so this does only affect the radio buttons with the same GroupName.

  • CodingNewUserCodingNewUser INMember ✭✭

    the custom radio button is a nuget package that i am using and is read only.I cannot add the property.

  • CodingNewUserCodingNewUser INMember ✭✭

    Thank you so much for the support throughout Eric. :)
    It is working now.

  • EricSchmeckEricSchmeck DEMember ✭✭

    Glad to hear! :)

Sign In or Register to comment.