I am new to coding and more so c#, on and off have been picking it at. Trying to learn by doing very basic projects in my spare time and am trying to figure out why I am not getting a result when both radios out of two are unchecked. The simple idea is if both radios are unchecked write a simple string to a list. Of course I can easily see both or one or the other and get two written indexes to the list, one for each. I am trying to see why I can't get just one list add if both are unchecked:
foreach (var child in children)
{
if (child.GetType() == typeof(TextBox))
{
UserText.Add("TXT BOX11");
}
if (child.GetType() == typeof(RadioButton))
{
if (((((RadioButton)child).Name == "Radio1") && ((RadioButton)child).IsChecked == false)
&& ((((RadioButton)child).Name == "Radio2") && ((RadioButton)child).IsChecked == false))
{
UserText.Add("Pickles");
}
}
}
You are only ever checking the current child. Since your condition is both ((RadioButton)child).Name == "Radio1"
and ((RadioButton)child).Name == "Radio2"
it will never be true.
You might want to use the CheckedChanged event instead or just access the radiobuttons through their variables. Probably named something like radioButton1
and radioButton2
.
Lastly, if it has to be within the foreach
loop you could set an external variable to keep track.
bool otherButton = false;
foreach (var child in children)
{
// textbox code
if(child is RadioButton rbtn && rbtn.Name == "Radio1" && rbtn.IsChecked == false)
{
if(otherButton)
UserText.Add("Pickles");
else
otherButton = true;
}
if(child is RadioButton rbtn && rbtn.Name == "Radio2" && rbtn.IsChecked == false)
{
if(otherButton)
UserText.Add("Pickles");
else
otherButton = true;
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments