Click here to Skip to main content
15,881,733 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello, I have a problem.
I'm trying to create a math game in WPF Application. The point is that when I start the game opens a window where there is a choice of four options (addition, multiplication, subtraction and division). Clicking on one of the options opens another window from two options (lower difficulty, higher difficulty). After choosing one of the options another window opens with examples that will be according to which of the options I have previously chosen. My problem is I don't know how to refer to previous radiobutton.I don't give the codes in full because I don't think it's necessary. I hope there's some simple solution I just don't see.

Thank you all for your advice.

What I have tried:

MainWindow.xaml.cs:
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            
        }

        

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (scitani.IsChecked.HasValue && scitani.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();
                this.Close();
            }
            else if (odcitani.IsChecked.HasValue && odcitani.IsChecked.Value)
            {
                uroven lvl = new uroven();
                lvl.Show();

...
uroven.xaml.cs:
public partial class uroven : Window
   {
       public uroven()
       {
           InitializeComponent();
       }

       private void start_Click(object sender, RoutedEventArgs e)
       {
           if (nizka.IsChecked.HasValue && nizka.IsChecked.Value)
           {
               hra game = new hra();
               game.Show();
               this.Close();
           }
           else if (vysoka.IsChecked.HasValue && vysoka.IsChecked.Value)
           {
               hra game = new hra();

...
hra.xaml.cs:
public partial class hra : Window
    {
        Stopwatch mt = new Stopwatch();
        DispatcherTimer posunTimer = new DispatcherTimer();
        decimal trvani = new decimal();
        int suma = new int();
        
        public hra()
        {
            InitializeComponent();
            
            posunTimer.Tick += posunTimer_Tick;
            posunTimer.Interval = TimeSpan.FromSeconds(1);
            
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            vysledekhrac.Focus();
            suma++;
            pocetprikladu.Text = suma.ToString();
            mt.Start();
            timer.Value = 0;
            posunTimer.Start();
            Random prvni = new Random();
            Random druhy = new Random();
            int maxprvni = 10;
            int maxdruhy = 10;
            int prvnic = prvni.Next(1, maxprvni);
            int druhyc = druhy.Next(2, maxdruhy);
            if (MathGame.scitani.IsChecked.HasValue && scitani.IsChecked.Value)
            {
                int total = (prvnic + druhyc);
                    }
            prvnit.Text = prvnic.ToString();
            druhyt.Text = druhyc.ToString();
            vysledek.Text = total.ToString();
        }

in part
C#
<pre>if (MathGame.scitani.IsChecked.HasValue && scitani.IsChecked.Value)
            {
                int total = (prvnic + druhyc);
                    }

I would like to refer to previous radiobuttons and in statemnt if I would set an example type.
Posted
Updated 14-Apr-21 0:36am

1 solution

This is WPF and you should be using bindings to do this. That way your previous settings would just be a property in your code that you easily can get.

Also I would definitly recommend using PRISM to separate your code into different ViewModels.
 
Share this answer
 
Comments
dejf111 14-Apr-21 6:40am    
I wanted to finish this way before I learned with MVVM
Kenneth Haugland 14-Apr-21 6:57am    
Then I would do something like: public uroven(MyClass ref ClassImplementation)
{
InitializeComponent();


}

its either out of ref, cant remember. The point is that you can pass this class in from the constructor as a referance to a variable in your window that "owns" or rather constructs this.
dejf111 20-Apr-21 4:26am    
it doesn't work or I didn't understand you
Kenneth Haugland 20-Apr-21 4:55am    
It works but you have to set it up correctly:
public MainWindow()
{
InitializeComponent();

ClassItems Myinput = new ClassItems();
ChildWindow dlg = new ChildWindow(ref Myinput);
dlg.Show();
}
With class:
public class ClassItems
{
public int MyProperty { get; set; } = 50;
}
With the child window:
public partial class ChildWindow : Window
{
public ChildWindow(ref ClassItems test)
{
InitializeComponent();
test.MyProperty += 55;
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900