2022. március 2., szerda

2022.03.02. Programozás - Random felelőválasztó

 Megírtuk a random felelőválasztót WPF-ben.


XAML:

<Window x:Class="RandomSelectWPF.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:RandomSelectWPF"

        mc:Ignorable="d"

        Title="Random felelő választó" Height="740" Width="600">

    <Grid Background="#FFEADFDF" Margin="0,0,0,0">

        <CheckBox x:Name="checkbox1csoport" Content="1. csoport" HorizontalAlignment="Left" Height="26" Margin="104,175,0,0" VerticalAlignment="Top" Width="138" Checked="checkbox1csoport_Checked" FontFamily="Arial Black" FontSize="20" Foreground="#FF1E4ADF" />

        <CheckBox x:Name="checkbox2csoport" Content="2. csoport" HorizontalAlignment="Left" Height="26" Margin="362,175,0,0" VerticalAlignment="Top" Width="136" Checked="checkbox2csoport_Checked" FontFamily="Arial Black" FontSize="20" Foreground="#FF1E4ADF"/>

        <ListBox x:Name="Listbox1csoport" HorizontalAlignment="Left" Height="457" Margin="60,228,0,0" VerticalAlignment="Top" Width="226" FontSize="16" Background="#FFDAC9C9" RenderTransformOrigin="0.5,0.5"  />

        <ListBox x:Name="Listbox2csoport" HorizontalAlignment="Left" Height="457" Margin="317,225,0,0" VerticalAlignment="Top" Width="225" FontSize="16" Background="#FFDAC9C9" Grid.ColumnSpan="2"/>

        <Label x:Name="valasztottFelelo" Content="" HorizontalAlignment="Left" Height="34" Margin="286,102,0,0" VerticalAlignment="Top" Width="256" Background="#FFACCEEE" FontSize="16" FontFamily="Arial Black" Foreground="#FFF93B07" Grid.ColumnSpan="2"/>

        <Button x:Name="felelo" Content="Következő felelő" HorizontalAlignment="Left" Margin="60,104,0,0" VerticalAlignment="Top" Height="30" Width="138" Click="felelo_Click" Background="#FF2F89DF" FontSize="16"/>

        <Label x:Name="cim" Content="Random felelő választó" HorizontalAlignment="Left" Margin="143,35,0,0" VerticalAlignment="Top" FontSize="24" FontFamily="Arial Black" Foreground="#FF1E4ADF"/>

    </Grid>

</Window>



XAML.CS:

using System;
using System.Collections.Generic;
using System.Windows;
using System.IO;

namespace RandomSelectWPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<osztaly> oszt = new List<osztaly>();
        bool valasztas;
        Random rnd = new Random();

        public MainWindow()
        {
            InitializeComponent();
            valasztas = true;
            checkbox1csoport.IsChecked = true;

            foreach (var item in File.ReadAllLines("12b.txt"))
            {
                oszt.Add(new osztaly(item));
            }

            for (int i = 0; i < oszt.Count; i++)
            {
                if (oszt[i].csoport == 1)
                {
                    Listbox1csoport.Items.Add(oszt[i].nev);
                }
                else
                {
                    if (oszt[i].csoport == 2)
                    {
                        Listbox2csoport.Items.Add(oszt[i].nev);
                    }
                }
            }
        }

        private void checkbox1csoport_Checked(object sender, RoutedEventArgs e)
        {
            checkbox2csoport.IsChecked = false;
            valasztas = true;
        }

        private void checkbox2csoport_Checked(object sender, RoutedEventArgs e)
        {
            checkbox1csoport.IsChecked = false;
            valasztas = false;
        }

        private void felelo_Click(object sender, RoutedEventArgs e)
        {
            if (valasztas)
            {
                valasztottFelelo.Content = Listbox1csoport.Items[rnd.Next(Listbox1csoport.Items.Count)];
            }
            else
            {
                valasztottFelelo.Content = Listbox2csoport.Items[rnd.Next(Listbox2csoport.Items.Count)];
            }
        }
    }
}


CLASS:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RandomSelectWPF
{
    public class osztaly
    {
        public int csoport;
        public string nev;

        public osztaly(string sor)
        {
            List<string> atmeneti = sor.Split(";").ToList();
            csoport = Convert.ToInt32(atmeneti[0]);
            nev = atmeneti[1];        
        }            
    }
}


Nincsenek megjegyzések:

Megjegyzés küldése