C
C#2y ago
Mekasu0124

✅ [solved] Learning Desktop App WPF Application

Ok. So I've built the front-end part of the welcome screen. I'm learning how to do this so be gentle please lol but it's my math game. I'm taking it from a console based app to an actual desktop app. So my question is, I have 2 sets of radio button selections, a combobox, and 2 buttons.
using System, System.Collections.Generic, System.Linq, System.Text, System.Threading.Tasks, System.Windows, System.Windows.Controls, System.Windows.Data, System.Windows.Documents, System.Windows.Input, System.Windows.Media, System.Windows.Media.Imaging, System.Windows.Navigation, System.Windows.Shapes;

namespace MeksMathGameGUI
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }

private void ExitApplication_CLick(object sender, RoutedEventArgs e) { Environment.Exit(0); }

private void StartGame_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Start Game Button Clicked!");
}
}
}
using System, System.Collections.Generic, System.Linq, System.Text, System.Threading.Tasks, System.Windows, System.Windows.Controls, System.Windows.Data, System.Windows.Documents, System.Windows.Input, System.Windows.Media, System.Windows.Media.Imaging, System.Windows.Navigation, System.Windows.Shapes;

namespace MeksMathGameGUI
{
public partial class MainWindow : Window
{
public MainWindow() { InitializeComponent(); }

private void ExitApplication_CLick(object sender, RoutedEventArgs e) { Environment.Exit(0); }

private void StartGame_Click(object sender, RoutedEventArgs e)
{
Console.WriteLine("Start Game Button Clicked!");
}
}
}
In my StartGame_Click method, I want to be able to capture the input from the radio buttons so that I can pass the needed information to the required functions. How do I get the radio button selections?
4 Replies
Mekasu0124
Mekasu0124OP2y ago
<GroupBox Name="difficultyRadios" Grid.Row="1" Grid.Column="0" Margin="10,0,0,0">
<StackPanel>
<Label Foreground="Silver" FontSize="18">Please Select A Game To Play</Label>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Easy</RadioButton>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Medium</RadioButton>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Hard</RadioButton>
</StackPanel>
</GroupBox>
<GroupBox Name="difficultyRadios" Grid.Row="1" Grid.Column="0" Margin="10,0,0,0">
<StackPanel>
<Label Foreground="Silver" FontSize="18">Please Select A Game To Play</Label>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Easy</RadioButton>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Medium</RadioButton>
<RadioButton Foreground="Silver" FontSize="15" Margin="2">Hard</RadioButton>
</StackPanel>
</GroupBox>
I have these radio buttons inside of a GroupBox with a Name. I have the corresponding code
private string difficultyRadio = "";
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
var radio = (sender as RadioButton);
difficultyRadio = radio.Content.ToString();
}
private void StartGame_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(difficultyRadio.ToString());
}
private string difficultyRadio = "";
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
var radio = (sender as RadioButton);
difficultyRadio = radio.Content.ToString();
}
private void StartGame_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(difficultyRadio.ToString());
}
All I'm trying to do is get the MessageBox to show the text of the selected radio button. I also have another radio button group named gameSelection and i have a ComboBox that I need to get the text of the selected radio button and the selected text of the selected index of the combo box and display them in the MessageBox, but when I click the start game button, the message box is completely empty. What am I doing wrong? My code here is based off this stack overflow which I understand is from 2015, but it was the best I thought I found https://stackoverflow.com/questions/29586150/get-input-from-radio-buttons-wpf-c-sharp
Stack Overflow
Get input from radio buttons wpf C#
Voila XML code <GroupBox x:Name="radioButtons"> <StackPanel Orientation="Horizontal"> <RadioButton Content="Teacher" HorizontalAlignment="Left" Margin="168,171,0,0"
Mekasu0124
Mekasu0124OP2y ago
I got it. nevermind ty
Mayor McCheese
Wpf has a pretty good data binding system, frequently people take advantage of the binding system and use a concept called mvvm, I encourage you to google wpf mvvm and learn about that too.
Mekasu0124
Mekasu0124OP2y ago
xaml code: https://pastebin.com/5j6dUAVD C# code: https://pastebin.com/ahcD3b8W i'm trying to figure out how to get the value of which radio button is selected on button click. In the first group box named difficultyRadio I need to get access to which difficulty the user selected. In the second GroupBox named gameSelection, I need to get access to which game the user selected. Finally, I need to get which item the user selected (if not index 0 or "Select One") so that the program knows how many questions the user wants to answer.Displaying them in the message box is for confirmation for the user. I want the message box to basically pop up and say the difficulty, game, and number of questions the user entered with a confirm button and a back button to go back to the home screen to reselect if the user so chooses. nevermind on this. I'm going back to the basics and learning stuff over again. THanks
Want results from more Discord servers?
Add your server