Advertisement
Margoshinka

основная форма

Mar 11th, 2022
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.12 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using System.Xml.Serialization;
  12.  
  13. namespace SYATP2_2
  14. {
  15.     public partial class Form1 : Form
  16.     {
  17.         int position = -1;
  18.         List<Student> list = new List<Student>();
  19.         List<Student> list_ = new List<Student>();
  20.         List<(string, string)> field;
  21.         string filename = "University.txt";
  22.        
  23.         ValueTuple<int, int> t;
  24.         bool flag = false;
  25.         Student s;
  26.         public bool CheckList()
  27.         {
  28.            
  29.                 if (comboBox1.Text == "Имя")
  30.                 return (list[position].Name.Contains(textBox4.Text));
  31.             else if (comboBox1.Text == "Фамилия")
  32.                 return (list[position].Lastname.Contains(textBox4.Text));
  33.             else if (comboBox1.Text == "Факультет")
  34.                 return (list[position].Faculty.Contains(textBox4.Text));
  35.             else return false;
  36.         }
  37.  
  38.         public Form1()
  39.         {
  40.             InitializeComponent();
  41.         }
  42.         public void  IsMaster(ref List<Student> list)
  43.         {
  44.             if (list[position].GetType().Name == "Master")
  45.             {
  46.                 field = list[position].getField();
  47.                 label6.Visible = true;
  48.                 textBox5.Visible = true;
  49.                 textBox5.Enabled = true;
  50.                 button3.Visible = false;
  51.                 textBox5.Text = field[3].Item2;
  52.             }
  53.             else
  54.             {
  55.                 textBox5.Visible = false;
  56.                 label6.Visible = false;
  57.                 button3.Visible = true;
  58.                 textBox5.Clear();
  59.             }
  60.         }
  61.         public void Findindex()
  62.         {
  63.             int index1 =-1;
  64.             int  index2 = -1;
  65.             if (comboBox1.Text == "Имя")
  66.             {
  67.                 index1 = list.FindIndex(x => x.Name.Contains(textBox4.Text));
  68.                 index2 = list.FindLastIndex(x => x.Name.Contains(textBox4.Text));
  69.             }
  70.             else if (comboBox1.Text == "Фамилия")
  71.             {
  72.                 index1 = list.FindIndex(x => x.Lastname.Contains(textBox4.Text));
  73.                 index2 = list.FindLastIndex(x => x.Lastname.Contains(textBox4.Text));
  74.             }
  75.  
  76.             else if (comboBox1.Text == "Факультет")
  77.             {
  78.                 index1 = list.FindIndex(x => x.Faculty.Contains(textBox4.Text));
  79.                 index2 = list.FindLastIndex(x => x.Faculty.Contains(textBox4.Text));
  80.             }
  81.                 t.Item1 = index1;
  82.             t.Item2 = index2;
  83.            
  84.  
  85.  
  86.         }
  87.         public void Selection()
  88.         {
  89.             flag = true;
  90.             position = 0;
  91.             textBox1.Enabled = true;
  92.             textBox2.Enabled = true;
  93.             textBox3.Enabled = true;
  94.             Findindex();
  95.             if (textBox4.Text.Length != 0 && t.Item1 != -1)
  96.             {
  97.                 position = t.Item1;
  98.  
  99.                 Output(ref list);
  100.                 IsMaster(ref list);
  101.                 CheckButton(ref list);
  102.                 CheckMenu(ref list);
  103.             }
  104.             else if ((textBox4.Text.Length != 0 && t.Item1 == -1))
  105.                 {
  106.                     position=-1;
  107.  
  108.                     textBox1.Clear();
  109.                     textBox2.Clear();
  110.                     textBox3.Clear();
  111.                     textBox5.Clear();
  112.                     textBox1.Enabled = false;
  113.                     textBox2.Enabled = false;
  114.                     textBox3.Enabled = false;
  115.                     textBox5.Enabled = false;
  116.  
  117.                 }
  118.  
  119.                 else
  120.                 {
  121.                     position = 0;
  122.                 flag = false;
  123.                     Output(ref list);
  124.                 IsMaster(ref list);
  125.  
  126.                
  127.                     CheckButton(ref list);
  128.                     CheckMenu(ref list);
  129.                 }
  130.  
  131.            
  132.         }
  133.        
  134.         public void CheckButton(ref List<Student> list)
  135.         {   if (flag )
  136.             { if (position >= t.Item2 )
  137.                
  138.                 button2.Enabled = false;
  139.             else button2.Enabled = true;
  140.                 if (position <= t.Item1)
  141.                     button1.Enabled = false;
  142.                 else button1.Enabled = true;
  143.             }
  144.             else if (list.Count <= 1)
  145.             {
  146.                 button1.Enabled = false;
  147.                 button2.Enabled = false;
  148.             }
  149.             else if (position == list.Count - 1)
  150.             {
  151.                 button2.Enabled = false;
  152.                 button1.Enabled = true;
  153.             }
  154.             else if (position == 0)
  155.             {
  156.                 button2.Enabled = true;
  157.                 button1.Enabled = false;
  158.             }
  159.             else
  160.             {
  161.                 button2.Enabled = true;
  162.                 button1.Enabled = true;
  163.             }
  164.         }
  165.         public void CheckMenu(ref List<Student> list)
  166.         {
  167.            
  168.              if (list.Count == 0)
  169.             {
  170.                
  171.                 удалитьToolStripMenuItem.Enabled = false;
  172.                 изменитьToolStripMenuItem.Enabled = false;
  173.  
  174.             }
  175.             else
  176.             {
  177.                
  178.                 добавитьToolStripMenuItem.Enabled = true;
  179.                 удалитьToolStripMenuItem.Enabled = true;
  180.                 изменитьToolStripMenuItem.Enabled = true;
  181.  
  182.             }
  183.         }
  184.         public void CheckComboBox(ref List<Student> list)
  185.         {
  186.             if (list.Count <= 1) { comboBox1.Enabled = false; textBox4.Enabled = false; }
  187.             else
  188.             {
  189.                 comboBox1.Enabled = true; textBox4.Enabled = true;
  190.             }
  191.         }
  192.         public void Output(ref List<Student>list)
  193.         {
  194.             textBox1.Text = list[position].Name;
  195.             textBox2.Text = list[position].Lastname;
  196.             textBox3.Text = list[position].Faculty;
  197.            
  198.         }
  199.         private void label1_Click(object sender, EventArgs e)
  200.         {
  201.  
  202.         }
  203.         public static List<Student> loadFile(List<Student> List, string filename)
  204.         {
  205.  
  206.             FileStream f = new FileStream(filename, FileMode.Open);
  207.             XmlSerializer bf = new XmlSerializer((typeof(List<Student>)));
  208.             return (List<Student>)bf.Deserialize(f);
  209.  
  210.             f.Close();
  211.         }
  212.         public static void SaveFile(List<Student> List, string filename)
  213.         {
  214.             FileStream f = new FileStream(filename, FileMode.Create);
  215.             XmlSerializer bf = new XmlSerializer((typeof(List<Student>)));
  216.             bf.Serialize(f, List);
  217.             f.Close();
  218.         }
  219.         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
  220.         {
  221.             Selection();
  222.         }
  223.  
  224.         private void button2_Click(object sender, EventArgs e)
  225.         {
  226.             position++;
  227.             while (flag && !CheckList() && position < t.Item2+1)
  228.             {
  229.                 position++;
  230.                 CheckButton(ref list);
  231.                 CheckMenu(ref list);
  232.                 IsMaster(ref list);
  233.             }
  234.             if (flag && CheckList() && position < t.Item2 + 1)
  235.             {
  236.                 Output(ref list);
  237.                 CheckButton(ref list);
  238.                 CheckMenu(ref list);
  239.                 IsMaster(ref list);
  240.             }
  241.             if (!flag )
  242.             {
  243.                 Output(ref list);
  244.                 CheckButton(ref list);
  245.                 CheckMenu(ref list);
  246.                 IsMaster(ref list);
  247.             }
  248.  
  249.         }
  250.  
  251.         private void label5_Click(object sender, EventArgs e)
  252.         {
  253.  
  254.         }
  255.  
  256.         private void textBox4_TextChanged(object sender, EventArgs e)
  257.         {
  258.             Selection();
  259.         }
  260.  
  261.         private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
  262.  
  263.         {
  264.             //Student s;
  265.            
  266.  
  267.            
  268.             Form2 form = new Form2();
  269.             form.ShowDialog();
  270.             if (form.DialogResult == DialogResult.OK)
  271.             {
  272.                 if (!textBox1.Enabled || !textBox2.Enabled || !textBox3.Enabled)
  273.             {
  274.                 textBox1.Enabled = true;
  275.                 textBox2.Enabled = true;
  276.                 textBox3.Enabled = true;
  277.             }
  278.  
  279.                 s = form.s;
  280.                 if (s.GetType().Name == "Master")
  281.                 {
  282.  
  283.                     textBox5.Visible = true;
  284.                     label6.Visible = true;
  285.                     button3.Visible = false;
  286.                 }
  287.  
  288.                 else
  289.                 {
  290.                     textBox5.Visible = false;
  291.                     label6.Visible = false;
  292.                     button3.Visible =true;
  293.                 }
  294.                     list.Add(s);
  295.                 position++;
  296.                 textBox1.Clear();
  297.                 textBox2.Clear();
  298.                 textBox3.Clear();
  299.                 textBox5.Clear();
  300.                 CheckButton(ref list);
  301.                 CheckMenu(ref list);
  302.                 CheckComboBox(ref list);
  303.             }
  304.         }
  305.  
  306.         private void загрузитьToolStripMenuItem_Click(object sender, EventArgs e)
  307.         {
  308.             position = 0;
  309.             OpenFileDialog openFileDialog1 = new OpenFileDialog();
  310.            // button1.Enabled = false;
  311.             if (openFileDialog1.ShowDialog() == DialogResult.OK)
  312.             {
  313.                 filename = openFileDialog1.FileName;
  314.                 list = loadFile(list, filename);
  315.                 Output(ref list);
  316.                 IsMaster(ref list);
  317.  
  318.             }
  319.            
  320.             if (!textBox1.Enabled || !textBox2.Enabled || !textBox3.Enabled)
  321.             {
  322.                 textBox1.Enabled = true;
  323.                 textBox2.Enabled = true;
  324.                 textBox3.Enabled = true;
  325.             }
  326.             //if (list.Count() > 1) button2.Enabled = true;
  327.             CheckButton(ref list);
  328.             CheckMenu(ref list);
  329.             CheckComboBox(ref list);
  330.         }
  331.  
  332.         private void сохранитьКакToolStripMenuItem_Click(object sender, EventArgs e)
  333.         {
  334.             SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  335.  
  336.             if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  337.             {
  338.                 filename = saveFileDialog1.FileName;
  339.                
  340.                 SaveFile(list, filename);
  341.  
  342.             }
  343.         }
  344.  
  345.         private void изменитьToolStripMenuItem_Click(object sender, EventArgs e)
  346.         {
  347.  
  348.  
  349.             if (list[position] is Master)
  350.             {
  351.  
  352.                 var item = list[position] as Master;
  353.                 item.Name = textBox1.Text;
  354.                 item.Lastname = textBox2.Text;
  355.                 item.Faculty = textBox3.Text;
  356.                 item.DiplomaNumber = textBox5.Text;
  357.                 list[position] = item;
  358.             }
  359.  
  360.  
  361.  
  362.             else
  363.             {
  364.                 s = list[position];
  365.                 s.Name = textBox1.Text;
  366.                 s.Lastname = textBox2.Text;
  367.                 s.Faculty = textBox3.Text;
  368.                
  369.  
  370.                 list[position] = s;
  371.             }
  372.                    
  373.                 Output(ref list);
  374.             IsMaster(ref list);
  375.            
  376.         }
  377.  
  378.         private void button1_Click(object sender, EventArgs e)
  379.         {
  380.             position--;
  381.  
  382.  
  383.             while (flag && !CheckList() && position >= t.Item1)
  384.             {
  385.                 position--;
  386.                 CheckButton(ref list);
  387.                 CheckMenu(ref list);
  388.                 IsMaster(ref list);
  389.  
  390.             }
  391.             if (flag && CheckList() && position >= t.Item1)
  392.             {
  393.                 Output(ref list);
  394.                 CheckButton(ref list);
  395.                 CheckMenu(ref list);
  396.                 IsMaster(ref list);
  397.             }
  398.             if (!flag)
  399.             {
  400.                 Output(ref list);
  401.                 CheckButton(ref list);
  402.                 CheckMenu(ref list);
  403.                 IsMaster(ref list);
  404.             }
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.         }
  412.  
  413.         private void textBox1_TextChanged(object sender, EventArgs e)
  414.         {
  415.                
  416.                
  417.         }
  418.  
  419.         private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
  420.         {
  421.            
  422.             if (flag && position < t.Item2 && position > t.Item1)
  423.             {
  424.                 int it1 = t.Item1;
  425.                 int it2 = t.Item2--;
  426.                 t = (it1, it2);
  427.             }
  428.             else if (flag && position < t.Item2 && position <= t.Item1)
  429.             {
  430.                 int it1 = t.Item1++;
  431.                 int it2 = t.Item2--;
  432.                 t = (it1, it2);
  433.             }
  434.             list.RemoveAt(position);
  435.             CheckButton(ref list);
  436.             CheckMenu(ref list);
  437.             CheckComboBox(ref list);
  438.             if (list.Count == 0)
  439.             {
  440.                
  441.                 textBox1.Clear();
  442.                 textBox2.Clear();
  443.                 textBox3.Clear();
  444.                 textBox5.Clear();
  445.                 textBox1.Enabled = false;
  446.                 textBox2.Enabled = false;
  447.                 textBox3.Enabled = false;
  448.                 textBox5.Enabled = false;
  449.                 button3.Visible = false;
  450.  
  451.                 return;
  452.             }
  453.             else if (position == list.Count())
  454.            
  455.                 position--;
  456.                
  457.                
  458.            
  459.            
  460.             Output(ref list);
  461.  
  462.  
  463.  
  464.         }
  465.  
  466.         private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
  467.         {  
  468.            
  469.            SaveFile(list, filename);
  470.         }
  471.  
  472.         private void Form1_Load(object sender, EventArgs e)
  473.         {
  474.  
  475.         }
  476.  
  477.         private void button3_Click(object sender, EventArgs e)
  478.         {
  479.             s = new Master(textBox1.Text, textBox2.Text, textBox3.Text, "");
  480.             list[position] = s;
  481.             IsMaster(ref list);
  482.         }
  483.     }
  484. }
  485.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement