C
C#4w ago
Sorey

✅ WPF Radio Buttons help needed

for a task for school, i need to work with radio buttons.
void RadioButtons()
{
rdb0.Checked += Rdb0_Checked;
rdb1.Checked += Rdb1_Checked;
rdb2.Checked += Rdb2_Checked;
rdb3.Checked += Rdb3_Checked;
rdb4.Checked += Rdb4_Checked;
}

private void Rdb3_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb2_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb4_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb1_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb0_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}
void RadioButtons()
{
rdb0.Checked += Rdb0_Checked;
rdb1.Checked += Rdb1_Checked;
rdb2.Checked += Rdb2_Checked;
rdb3.Checked += Rdb3_Checked;
rdb4.Checked += Rdb4_Checked;
}

private void Rdb3_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb2_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb4_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb1_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}

private void Rdb0_Checked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}
This is what i have, this works, is this the normal way of using radio buttons or is there a better way? I don't ask to edit my code, but to like steer me in the right direction or to help me understand the use of radio buttons. I have little knowledge at this point of the program, so i would like to have feedback in simple ways ^^'
4 Replies
canton7
canton74w ago
What are you trying to do? Set stpCategory.IsEnabled = true whenever any radio button is checked, regardless of which one is checked? That seems like an odd thing to do.
Sorey
SoreyOP4w ago
what this is supposed to do is enable the next part of code so that input can be delivered depending on what radio button is checked. each button is a different location, does that make sense? and the stpCategory is just all the buttons for input.
canton7
canton74w ago
One thing you could do is to use the same event handler for all of the Checked events:
void RadioButtons()
{
rdb0.Checked += RadioButtonChecked;
rdb1.Checked += RadioButtonChecked;
// ...
}

private void RadioButtonChecked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}
void RadioButtons()
{
rdb0.Checked += RadioButtonChecked;
rdb1.Checked += RadioButtonChecked;
// ...
}

private void RadioButtonChecked(object sender, RoutedEventArgs e)
{
stpCategory.IsEnabled = true;
}
Or even:
void RadioButtons()
{
var radioButtons = new[] { rdb0, rdb1, ... };
foreach (var button in radioButtons)
{
button.Checked += RadioButtonChecked;
}
}
void RadioButtons()
{
var radioButtons = new[] { rdb0, rdb1, ... };
foreach (var button in radioButtons)
{
button.Checked += RadioButtonChecked;
}
}
Sorey
SoreyOP4w ago
Thank you, i will try this as soon as possible! Thank you very much, you actually helped me alot to understand the concept of coding radiobuttons 🙂

Did you find this page helpful?