mercredi 28 avril 2021

How to pass value from xaml to class?

I´m creating WPF Application. In this application I would like to achieve that on the introductory screen the user chooses one option from mathematical operations (addition, subtraction, multiplication and division)this chooses are constructed by radiobutton.In the same window, the user would choose from two levels (low, high) also formed by radibutton,then I would check if-statement which option was selected. The values ​​to be selected would be sent to the class, from which they would then proceed to the next xaml.cs window.The game would run in this window.

MainWindow.xaml:

<Window x:Class="GameofMath.MainWindow"
       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       xmlns:local="clr-namespace:GameofMath"
       mc:Ignorable="d"
       Title="Menu" Height="321" Width="583">
   <Grid>
       <TextBlock Text="Vyber jednu z možností" TextWrapping="Wrap" Margin="10,10,59,250" FontSize="20"/>
       <Button Content="Start" Margin="281,240,10,10" FontSize="16" Click="Button_Click" IsDefault="True"/>

       <RadioButton   x:Name="scitani" x:FieldModifier="public" Content="Sčítání"  Margin="10,60,214,200" GroupName="level" />
       <RadioButton x:Name="odcitani" x:FieldModifier="public" Content="Odčítání" Margin="10,110,214,153" IsThreeState="False" GroupName="level" />
       <RadioButton x:Name="nasobeni" x:FieldModifier="public" Content="Násobení" Margin="10,157,214,109" IsThreeState="False" GroupName="level" />
       <RadioButton x:Name="deleni" x:FieldModifier="public" Content="Dělení" Margin="10,201,227,67" IsThreeState="False" GroupName="level" />
       <GroupBox x:Name="uroven" Header="Vyber jednu z úrovní" Margin="281,30,10,73"/>
       <RadioButton x:Name="nizsi" Content="Nízká" Margin="292,83,-291,180" GroupName="uroven"/>
       <RadioButton x:Name="vyssi" Content="Vyšší" Margin="292,153,-291,104" GroupName="uroven"/>

   </Grid>
</Window>

MainWidow.xaml.cs:

using System;

using System.Windows;


namespace GameofMath
{
   /// <summary>
   /// Interaction logic for MainWindow.xaml
   /// </summary>
   public partial class MainWindow : Window
   {
       public MainWindow()
       {
           InitializeComponent();
       }


       private void Button_Click(object sender, RoutedEventArgs e)
       {
           
           if ( scitani.IsChecked.Value == true && nizsi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           if ( scitani.IsChecked.Value == true &&  vyssi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               
               lvl.Show();
               this.Close();
           }
           else if ( odcitani.IsChecked.Value == true &&  nizsi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           else if ( odcitani.IsChecked.Value == true &&  vyssi.IsChecked.Value == true)
           {

               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               
               lvl.Show();
               this.Close();
           }
           else if ( nasobeni.IsChecked.Value == true && nizsi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           else if ( nasobeni.IsChecked.Value == true&& vyssi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           else if (deleni.IsChecked.Value == true && nizsi.IsChecked.Value == true)
           {
               hra lvl = new hra();
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           else if ( deleni.IsChecked.Value == true &&  vyssi.IsChecked.Value == true)
           {
               hra lvl = new hra();
              
               Vlastnosti vlastnosti = new Vlastnosti();
               lvl.Show();
               this.Close();
           }
           else if (scitani.IsChecked.Value == false &&  odcitani.IsChecked.Value == false &&  nasobeni.IsChecked.Value == false &&  deleni.IsChecked.Value == false)
           {
               MessageBox.Show("Musíš zvolit, jednu z početních operací!");

           }
           else if ( nizsi.IsChecked.Value == false && vyssi.IsChecked.Value == false)
           {
               MessageBox.Show("Musíš zvolit, jednu z úrovní!");
           }
           else 
           {
               MessageBox.Show("Musíš zvolit, jednu z početních operací a úrovní!");
           }

       }
       

   }
}

class:

namespace GameofMath
{
    class Vlastnosti
    {
        public enum Operace
        {
            scitaniNiz= MainWindow.
            scitaniVys,
            odcitaniNiz,
            odcitaniVys,
            nasobeniNiz,
            nasobeniVys,
            deleniNiz,
            deleniVys,

        }
        public static class Class1
        {
           

            static Class1()
            {
                
            }

        }
    }
}

hra.xaml:

<Window x:Class="GameofMath.hra"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GameofMath"
        mc:Ignorable="d"
       Title="GameOfMath" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid>
        <TextBlock x:Name="prvnit" Text="TextBlock" TextWrapping="Wrap" Margin="66,115,641,199" FontSize="48" Height="120" Width="90"/>
        <TextBlock x:Name="znamenko" Text="TextBlock" TextWrapping="Wrap" Margin="209,144,533,220" FontSize="48" Width="58" Height="70"/>
        <TextBlock x:Name="druhyt" Text="TextBlock" TextWrapping="Wrap" Margin="310,119,400,195" FontSize="48" Width="90" Height="120"/>
        <TextBlock Text=" = " TextWrapping="Wrap" Margin="453,0,0,217" FontSize="48" Width="58" Height="70" HorizontalAlignment="Left" VerticalAlignment="Bottom"/>
        <TextBlock x:Name="vysledek" Text="TextBlock" TextWrapping="Wrap" Margin="243,248,480,129" Visibility="Hidden" />
        <TextBlock x:Name="hlaska" TextWrapping="Wrap" Margin="0,0,480,379" FontSize="24"/>
        <Button x:Name="kontrola" Content="Kontrola" Margin="508,369,10,10" FontSize="16" Click="kontrola_Click" IsDefault="True"/>
        <TextBox x:Name="vysledekHrac" TextWrapping="Wrap" Margin="576,122,83,202" FontSize="48" BorderThickness="2,2,2,2"/>
        <TextBlock x:Name="pch" Text="Počet chyb:" TextWrapping="Wrap" Margin="10,317,579,74"/>
        <TextBlock x:Name="pocetchyb" TextWrapping="Wrap" Margin="8,349,688,8" FontSize="24"/>
        <ProgressBar x:Name="timer" Margin="121,369,335,41" Maximum="10"/>
        <Button x:Name="konec" Content="Ukončit" Margin="612,10,10,379" Click="konec_Click"/>
        <TextBlock x:Name="pocetPrikladu" Text="TextBlock" TextWrapping="Wrap" Margin="381,15,247,364"/>
        <Label x:Name="pocetpr" Content="Počet příkladů:" HorizontalAlignment="Left" Margin="293,10,0,0" VerticalAlignment="Top"/>

    </Grid>
</Window>

hra.xaml.cs:


using System.Diagnostics;
using System.IO;

using System.Windows;

using System.Windows.Threading;

namespace GameofMath
{
    /// <summary>
    /// Interakční logika pro hra.xaml
    /// </summary>
    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);

        }
        public 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 (Vlastnosti.Class1.scitani.IsChecked.Value == true)
            {
                znamenko.Text = "-";

            }
            if (odcitani.IsChecked.Value == true)
            {
                znamenko.Text = "-";

            }
            if (nasobeni.IsChecked.Value == true)
            {
                znamenko.Text = "*";
            }
            if (znamenko.Text == "+")
            {
                int total = (prvnic + druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }
            if (znamenko.Text == "-")
            {
                int total = (prvnic - druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }
            if (znamenko.Text == "*")
            {
                int total = (prvnic * druhyc);
                prvnit.Text = prvnic.ToString();
                druhyt.Text = druhyc.ToString();
                vysledek.Text = total.ToString();

            }


        }
        private void posunTimer_Tick(object sender, object e)
        {
            timer.Value += 1;
            if (timer.Value >= timer.Maximum)

            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                suma++;
                pocetchyb.Text = poch.ToString();
                pocetPrikladu.Text = suma.ToString();
                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 (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }


                vysledekHrac.Text = String.Empty;

                if (poch == 4)
                {
                    mt.Stop();
                    timer.Value = 0;
                    posunTimer.Stop();
                    MessageBox.Show("Konec hry! Čas vypršel. ");
                    TimeSpan trvani = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
                    MessageBox.Show("Tvůj čas je " + trvani.ToString("mm\\:ss\\.ff") + "\n" + "Počet příkladů:" + pocetPrikladu.Text + "\n" + "Průměrný čas:" + trvani / suma);
                    if (!File.Exists(path))
                    {
                        StreamWriter str = File.CreateText(path);
                        str.WriteLine(trvani);
                    }
                    else if (File.Exists(path))
                    {
                        var str = new StreamWriter(path);
                        str.WriteLine(trvani);
                    }
                    this.Close();
                }
            }
        }
        private int poch;

        private void kontrola_Click(object sender, RoutedEventArgs e)
        {

            timer.Value += 1;
            posunTimer.Start();
            if (vysledekHrac.Text == vysledek.Text)
            {
                posunTimer.Stop();
                hlaska.Text = "Správně";
                suma++;
                pocetPrikladu.Text = suma.ToString();
                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 (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }

                vysledekHrac.Text = String.Empty;
                timer.Value = 0;
                posunTimer.Start();
            }

            else
            {
                posunTimer.Stop();
                hlaska.Text = "Špatně";
                poch++;
                suma++;
                pocetchyb.Text = poch.ToString();
                pocetPrikladu.Text = suma.ToString();
                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 (znamenko.Text == "+")
                {
                    int total = (prvnic + druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "-")
                {
                    int total = (prvnic - druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }
                if (znamenko.Text == "*")
                {
                    int total = (prvnic * druhyc);
                    prvnit.Text = prvnic.ToString();
                    druhyt.Text = druhyc.ToString();
                    vysledek.Text = total.ToString();

                }

                vysledekHrac.Text = String.Empty;
                timer.Value = 0;
                posunTimer.Start();
                if (poch == 4)
                {
                    mt.Stop();
                    timer.Value = 0;
                    posunTimer.Stop();
                    MessageBox.Show("Konec hry!");
                    konec_Click(sender, e);

                }
            }







        }
        string path = @"C:\\Žebříček.txt";
        private void konec_Click(object sender, RoutedEventArgs e)
        {
            timer.Value = 0;
            posunTimer.Stop();
            if (mt.IsRunning == true)
            {
                mt.Stop();
            }
            TimeSpan trvani = TimeSpan.FromMilliseconds(mt.ElapsedMilliseconds);
            MessageBox.Show("Tvůj čas je " + trvani.ToString("mm\\:ss\\.ff") + "\n" + "Počet příkladů: " + pocetPrikladu.Text + "\n" + "Průměrný čas: " + Math.Round(trvani.TotalSeconds / suma, 4) + " sekund");
            if (!File.Exists(path))
            {
                StreamWriter str = File.CreateText(path);
                str.WriteLine(trvani);
            }
            else if (File.Exists(path))
            {
                var str = new StreamWriter(path);
                str.WriteLine(trvani);
            }

            this.Close();
        }
    }
}


Probably there are a lot of things that would be easier to do but I'm a beginner so please be patient. In the last phase, I want to design a table in which it will be stored.

But my problem now is in sending the value from the if-statement to the class.I tried to invent an enumeration somehow, but I also failed.

Will be happy for any idea.

Thank you

Aucun commentaire:

Enregistrer un commentaire