Advertisement
k_vychodilova

zvire

Mar 14th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows.Forms;
  8.  
  9. namespace FarmaZviratek
  10. {
  11.     abstract class ZviratkoBase
  12.     {
  13.         public string Jmeno { get; set; }
  14.         public ZviratkoBase(string jmeno)
  15.         {
  16.             Jmeno = jmeno;
  17.         }
  18.  
  19.         abstract public string VratDruh();
  20.         abstract public string VratZvuk();
  21.     }
  22.     class Pejsek : ZviratkoBase
  23.     {
  24.         public Pejsek(string jmeno) : base(jmeno)
  25.         {
  26.  
  27.         }
  28.  
  29.         override public string VratDruh()
  30.         {
  31.             return "Pejsek";
  32.         }
  33.  
  34.         override public string VratZvuk()
  35.         {
  36.             return "haf haf";
  37.         }
  38.  
  39.         public override string ToString()
  40.         {
  41.             return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
  42.         }
  43.     }
  44.  
  45.     class Kocicka : ZviratkoBase
  46.     {
  47.         public Kocicka(string jmeno) : base(jmeno)
  48.         {
  49.  
  50.         }
  51.  
  52.         override public string VratDruh()
  53.         {
  54.             return "Kocicka";
  55.         }
  56.  
  57.         override public string VratZvuk()
  58.         {
  59.             return "mnau mnau";
  60.         }
  61.  
  62.         public override string ToString()
  63.         {
  64.             return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
  65.         }
  66.     }
  67.  
  68.     class Kacenka : ZviratkoBase
  69.     {
  70.         public Kacenka(string jmeno) : base(jmeno)
  71.         {
  72.  
  73.         }
  74.  
  75.         override public string VratDruh()
  76.         {
  77.             return "Kacenka";
  78.         }
  79.  
  80.         override public string VratZvuk()
  81.         {
  82.             return "kač kač";
  83.         }
  84.  
  85.         public override string ToString()
  86.         {
  87.             return $"jmenuju se {Jmeno} jsem {VratDruh()} a delam {VratZvuk()}";
  88.         }
  89.     }
  90.  
  91.     class FarmaForm : Form
  92.     {
  93.         private BindingList<ZviratkoBase> zviratka = null;
  94.         BindingSource bindingSource = null;
  95.  
  96.         private ComboBox comboBoxFarmy = null;
  97.         private RadioButton radioButtonPejsek = null;
  98.         private RadioButton radioButtonKocicka = null;
  99.         private RadioButton radioButtonKacenka = null;
  100.  
  101.         private Button buttonAddZviratko = null;
  102.         private TextBox textBoxJmeno = null;
  103.         private Label labelAktualniZviratko = null;
  104.  
  105.         private TableLayoutPanel tableLayutPanel = null;
  106.         private FlowLayoutPanel flowLayoutPanel = null;
  107.  
  108.        
  109.         private ListBox listBox = null;
  110.        
  111.  
  112.         public FarmaForm()
  113.         {
  114.             zviratka = new BindingList<ZviratkoBase>();
  115.  
  116.             comboBoxFarmy = new ComboBox();
  117.             radioButtonKocicka = new RadioButton() { Text = "Kočička", Checked = true };
  118.             radioButtonPejsek = new RadioButton() { Text = "Pejsek" };
  119.             radioButtonKacenka = new RadioButton() { Text = "Kačenka" };
  120.  
  121.             textBoxJmeno = new TextBox();
  122.             labelAktualniZviratko = new Label();
  123.             labelAktualniZviratko.Dock = DockStyle.Fill;
  124.  
  125.             buttonAddZviratko = new Button() { Text = "Přidej zvířatko", AutoSize = true };
  126.             buttonAddZviratko.Click += ButtonAddZviratko_Click;
  127.             flowLayoutPanel = new FlowLayoutPanel() { FlowDirection = FlowDirection.TopDown, AutoSize = true };
  128.             flowLayoutPanel.Dock = DockStyle.Fill;
  129.             flowLayoutPanel.Controls.Add(radioButtonKocicka);
  130.             flowLayoutPanel.Controls.Add(radioButtonPejsek);
  131.             flowLayoutPanel.Controls.Add(radioButtonKacenka);
  132.  
  133.             listBox = new ListBox();
  134.             listBox.DisplayMember = nameof(ZviratkoBase.Jmeno);
  135.             listBox.Dock = DockStyle.Fill;
  136.             bindingSource = new BindingSource(zviratka, null);
  137.             listBox.DataSource = bindingSource;
  138.             bindingSource.CurrentItemChanged += BindingSource_CurrentItemChanged;
  139.  
  140.             tableLayutPanel = new TableLayoutPanel();
  141.             tableLayutPanel.Dock = DockStyle.Fill;
  142.             tableLayutPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset;
  143.             tableLayutPanel.RowCount = 4;
  144.             tableLayutPanel.ColumnCount = 2;
  145.  
  146.             tableLayutPanel.Controls.Add(comboBoxFarmy, 0, 0);
  147.             tableLayutPanel.Controls.Add(flowLayoutPanel, 0, 1);
  148.             tableLayutPanel.Controls.Add(textBoxJmeno, 0, 2);
  149.             tableLayutPanel.Controls.Add(buttonAddZviratko, 0, 3);
  150.             tableLayutPanel.Controls.Add(labelAktualniZviratko, 1, 2);
  151.             tableLayutPanel.Controls.Add(listBox, 1, 1);
  152.             Controls.Add(tableLayutPanel);
  153.         }
  154.  
  155.         private void BindingSource_CurrentItemChanged(object sender, EventArgs e)
  156.         {
  157.             ZviratkoBase aktualni = bindingSource.Current as ZviratkoBase;
  158.             if(aktualni != null)
  159.             {
  160.                 labelAktualniZviratko.Text = aktualni.ToString();
  161.             }
  162.         }
  163.  
  164.         private void ButtonAddZviratko_Click(object sender, EventArgs e)
  165.         {
  166.             ZviratkoBase noveZviratko = null;
  167.             string jmeno = textBoxJmeno.Text;
  168.             if(string.IsNullOrWhiteSpace(jmeno))
  169.             {
  170.                 return;
  171.             }
  172.  
  173.             if (radioButtonPejsek.Checked)
  174.             {
  175.                 noveZviratko = new Pejsek(jmeno);
  176.             }
  177.             else if (radioButtonKocicka.Checked)
  178.             {
  179.                 noveZviratko = new Kocicka(jmeno);
  180.             }
  181.             else if (radioButtonKacenka.Checked)
  182.             {
  183.                 noveZviratko = new Kacenka(jmeno);
  184.             }
  185.  
  186.             if (zviratka != null)
  187.             {
  188.                 zviratka.Add(noveZviratko);
  189.             }
  190.         }
  191.     }
  192.     class Program
  193.     {
  194.         [STAThread]
  195.         static void Main(string[] args)
  196.         {
  197.             Application.EnableVisualStyles();
  198.             Application.Run(new FarmaForm());
  199.         }
  200.     }
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement