Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace FarmaZviratek
- {
- abstract class ZviratkoBase
- {
- public string Jmeno { get; set; }
- public ZviratkoBase(string jmeno)
- {
- Jmeno = jmeno;
- }
- abstract public string VratDruh();
- abstract public string VratZvuk();
- }
- class Pejsek : ZviratkoBase
- {
- public Pejsek(string jmeno) : base(jmeno)
- {
- }
- override public string VratDruh()
- {
- return "Pejsek";
- }
- override public string VratZvuk()
- {
- return "haf haf";
- }
- public override string ToString()
- {
- return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
- }
- }
- class Kocicka : ZviratkoBase
- {
- public Kocicka(string jmeno) : base(jmeno)
- {
- }
- override public string VratDruh()
- {
- return "Kocicka";
- }
- override public string VratZvuk()
- {
- return "mnau mnau";
- }
- public override string ToString()
- {
- return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
- }
- }
- class Kacenka : ZviratkoBase
- {
- public Kacenka(string jmeno) : base(jmeno)
- {
- }
- override public string VratDruh()
- {
- return "Kacenka";
- }
- override public string VratZvuk()
- {
- return "kač kač";
- }
- public override string ToString()
- {
- return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
- }
- }
- class FarmaForm : Form
- {
- private BindingList<ZviratkoBase> zviratka = null;
- BindingSource bindingSource = null;
- private ComboBox comboBoxFarmy = null;
- private RadioButton radioButtonPejsek = null;
- private RadioButton radioButtonKocicka = null;
- private RadioButton radioButtonKacenka = null;
- private Button buttonAddZviratko = null;
- private TextBox textBoxJmeno = null;
- private Label labelAktualniZviratko = null;
- private TableLayoutPanel tableLayutPanel = null;
- private FlowLayoutPanel flowLayoutPanel = null;
- private ListBox listBox = null;
- public FarmaForm()
- {
- zviratka = new BindingList<ZviratkoBase>();
- comboBoxFarmy = new ComboBox();
- radioButtonKocicka = new RadioButton() { Text = "Kočička", Checked = true };
- radioButtonPejsek = new RadioButton() { Text = "Pejsek" };
- radioButtonKacenka = new RadioButton() { Text = "Kačenka" };
- textBoxJmeno = new TextBox();
- labelAktualniZviratko = new Label();
- labelAktualniZviratko.Dock = DockStyle.Fill;
- buttonAddZviratko = new Button() { Text = "Přidej zvířatko", AutoSize = true };
- buttonAddZviratko.Click += ButtonAddZviratko_Click;
- flowLayoutPanel = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown, AutoSize = true };
- flowLayoutPanel.Dock = DockStyle.Fill;
- flowLayoutPanel.Controls.Add(radioButtonKocicka);
- flowLayoutPanel.Controls.Add(radioButtonPejsek);
- flowLayoutPanel.Controls.Add(radioButtonKacenka);
- listBox = new ListBox();
- listBox.DisplayMember = nameof(ZviratkoBase.Jmeno);
- listBox.Dock = DockStyle.Fill;
- bindingSource = new BindingSource(zviratka, null);
- listBox.DataSource = bindingSource;
- bindingSource.CurrentItemChanged += BindingSource_CurrentItemChanged;
- tableLayutPanel = new TableLayoutPanel();
- tableLayutPanel.Dock = DockStyle.Fill;
- tableLayutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
- tableLayutPanel.RowCount = 4;
- tableLayutPanel.ColumnCount = 2;
- tableLayutPanel.Controls.Add(comboBoxFarmy, 0, 0);
- tableLayutPanel.Controls.Add(flowLayoutPanel, 0, 1);
- tableLayutPanel.Controls.Add(textBoxJmeno, 0, 2);
- tableLayutPanel.Controls.Add(buttonAddZviratko, 0, 3);
- tableLayutPanel.Controls.Add(labelAktualniZviratko, 1, 2);
- tableLayutPanel.Controls.Add(listBox, 1, 1);
- Controls.Add(tableLayutPanel);
- }
- private void BindingSource_CurrentItemChanged(object sender, EventArgs e)
- {
- ZviratkoBase aktualni = bindingSource.Current as ZviratkoBase;
- if(aktualni != null)
- {
- labelAktualniZviratko.Text = aktualni.ToString();
- }
- }
- private void ButtonAddZviratko_Click(object sender, EventArgs e)
- {
- ZviratkoBase noveZviratko = null;
- string jmeno = textBoxJmeno.Text;
- if(string.IsNullOrWhiteSpace(jmeno))
- {
- return;
- }
- if (radioButtonPejsek.Checked)
- {
- noveZviratko = new Pejsek(jmeno);
- }
- else if (radioButtonKocicka.Checked)
- {
- noveZviratko = new Kocicka(jmeno);
- }
- else if (radioButtonKacenka.Checked)
- {
- noveZviratko = new Kacenka(jmeno);
- }
- if (zviratka != null)
- {
- zviratka.Add(noveZviratko);
- }
- }
- }
- class Program
- {
- [STAThread]
- static void Main(string[] args)
- {
- Application.EnableVisualStyles();
- Application.Run(new FarmaForm());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement