class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
namespace randomfelelo2
{
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];
}
}
}
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
namespace randomfelelo2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
Random rnd = new Random();
public MainWindow()
{
InitializeComponent();
List<Osztaly> tanulok = new List<Osztaly>();
Chb1.IsChecked = true;
//kezdetben az 1. csoport legyen az aktív (ezt nem feltétlen kell megtenni)
//ezt be lehet állítani a vezérlőelem tulajdonságainál is
foreach (var item in File.ReadAllLines("12b.txt"))
{
tanulok.Add(new Osztaly(item));
}
for (int i = 0; i < tanulok.Count; i++)
{
if (tanulok[i].csoport == 1)
{
Lb1.Items.Add(tanulok[i].nev);
}
else
{
if (tanulok[i].csoport ==2)
{
Lb2.Items.Add(tanulok[i].nev);
}
}
}
}
private void Chb1_Checked(object sender, RoutedEventArgs e)
{
Chb2.IsChecked = false;
}
private void Chb2_Checked(object sender, RoutedEventArgs e)
{
Chb1.IsChecked = false;
}
private void BtnFelelo_Click(object sender, RoutedEventArgs e)
{
if (!(Chb1.IsChecked == false && Chb2.IsChecked == false))
{
if (Chb1.IsChecked == true)
{
LblFelelo.Content = Lbx1.Items[rnd.Next(Lbx1.Items.Count)];
}
else
{
if (Chb2.IsChecked == true)
{
LblFelelo.Content = Lbx2.Items[rnd.Next(Lbx2.Items.Count)];
}
}
}
else
{
MessageBox.Show("Válassz egy csoportot!");
}
}
}
}
MainWindow.xaml
<Window x:Class="randomfelelo2.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:randomfelelo2"
mc:Ignorable="d"
Title="Random felelő választó" Height="610" Width="600">
<Grid Background="#FFC8ECF0">
<Label Content="Random felelő választó" HorizontalAlignment="Center" Margin="0,39,0,0" VerticalAlignment="Top" Background="#FF4A58C0" Foreground="#FFF9F20A" FontSize="16"/>
<Label x:Name="LblFelelo" Content="" HorizontalAlignment="Left" Margin="280,89,0,0" VerticalAlignment="Top" Width="272" Background="#FF98DDDA" Height="31" FontSize="16"/>
<ListBox x:Name="Lb1" HorizontalAlignment="Left" Height="318" Margin="29,218,0,0" VerticalAlignment="Top" Width="236" RenderTransformOrigin="0.5,0.5"></ListBox>
<ListBox x:Name="Lb2" HorizontalAlignment="Left" Height="318" Margin="316,218,0,0" VerticalAlignment="Top" Width="236"/>
<Label Content="1. csoport" HorizontalAlignment="Left" Margin="30,167,0,0" VerticalAlignment="Top"/>
<Label Content="2. csoport" HorizontalAlignment="Left" Margin="324,167,0,0" VerticalAlignment="Top"/>
<CheckBox x:Name="Chb1" Content="" HorizontalAlignment="Left" Margin="104,176,0,0" VerticalAlignment="Top" RenderTransformOrigin="13.602,0.115" Checked="Chb1_Checked"/>
<CheckBox x:Name="Chb2" Content="" HorizontalAlignment="Left" Margin="396,176,0,0" VerticalAlignment="Top" RenderTransformOrigin="13.602,0.115" Checked="Chb2_Checked"/>
<Button x:Name="BtnFelelo" Content="A következő felelő: " HorizontalAlignment="Left" Margin="43,89,0,0" VerticalAlignment="Top" Background="#FF5C85D4" FontSize="16" Click="BtnFelelo_Click"/>
</Grid>
</Window>