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.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO;
- using System.Xml.Serialization;
- namespace SYATP2_2
- {
- public partial class Form1 : Form
- {
- int position = -1;
- List<Student> list = new List<Student>();
- List<Student> list_ = new List<Student>();
- List<(string, string)> field;
- string filename = "University.txt";
- ValueTuple<int, int> t;
- bool flag = false;
- Student s;
- public bool CheckList()
- {
- if (comboBox1.Text == "Имя")
- return (list[position].Name.Contains(textBox4.Text));
- else if (comboBox1.Text == "Фамилия")
- return (list[position].Lastname.Contains(textBox4.Text));
- else if (comboBox1.Text == "Факультет")
- return (list[position].Faculty.Contains(textBox4.Text));
- else return false;
- }
- public Form1()
- {
- InitializeComponent();
- }
- public void IsMaster(ref List<Student> list)
- {
- if (list[position].GetType().Name == "Master")
- {
- field = list[position].getField();
- label6.Visible = true;
- textBox5.Visible = true;
- textBox5.Enabled = true;
- button3.Visible = false;
- textBox5.Text = field[3].Item2;
- }
- else
- {
- textBox5.Visible = false;
- label6.Visible = false;
- button3.Visible = true;
- textBox5.Clear();
- }
- }
- public void Findindex()
- {
- int index1 =-1;
- int index2 = -1;
- if (comboBox1.Text == "Имя")
- {
- index1 = list.FindIndex(x => x.Name.Contains(textBox4.Text));
- index2 = list.FindLastIndex(x => x.Name.Contains(textBox4.Text));
- }
- else if (comboBox1.Text == "Фамилия")
- {
- index1 = list.FindIndex(x => x.Lastname.Contains(textBox4.Text));
- index2 = list.FindLastIndex(x => x.Lastname.Contains(textBox4.Text));
- }
- else if (comboBox1.Text == "Факультет")
- {
- index1 = list.FindIndex(x => x.Faculty.Contains(textBox4.Text));
- index2 = list.FindLastIndex(x => x.Faculty.Contains(textBox4.Text));
- }
- t.Item1 = index1;
- t.Item2 = index2;
- }
- public void Selection()
- {
- flag = true;
- position = 0;
- textBox1.Enabled = true;
- textBox2.Enabled = true;
- textBox3.Enabled = true;
- Findindex();
- if (textBox4.Text.Length != 0 && t.Item1 != -1)
- {
- position = t.Item1;
- Output(ref list);
- IsMaster(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- }
- else if ((textBox4.Text.Length != 0 && t.Item1 == -1))
- {
- position=-1;
- textBox1.Clear();
- textBox2.Clear();
- textBox3.Clear();
- textBox5.Clear();
- textBox1.Enabled = false;
- textBox2.Enabled = false;
- textBox3.Enabled = false;
- textBox5.Enabled = false;
- }
- else
- {
- position = 0;
- flag = false;
- Output(ref list);
- IsMaster(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- }
- }
- public void CheckButton(ref List<Student> list)
- { if (flag )
- { if (position >= t.Item2 )
- button2.Enabled = false;
- else button2.Enabled = true;
- if (position <= t.Item1)
- button1.Enabled = false;
- else button1.Enabled = true;
- }
- else if (list.Count <= 1)
- {
- button1.Enabled = false;
- button2.Enabled = false;
- }
- else if (position == list.Count - 1)
- {
- button2.Enabled = false;
- button1.Enabled = true;
- }
- else if (position == 0)
- {
- button2.Enabled = true;
- button1.Enabled = false;
- }
- else
- {
- button2.Enabled = true;
- button1.Enabled = true;
- }
- }
- public void CheckMenu(ref List<Student> list)
- {
- if (list.Count == 0)
- {
- удалитьToolStripMenuItem.Enabled = false;
- изменитьToolStripMenuItem.Enabled = false;
- }
- else
- {
- добавитьToolStripMenuItem.Enabled = true;
- удалитьToolStripMenuItem.Enabled = true;
- изменитьToolStripMenuItem.Enabled = true;
- }
- }
- public void CheckComboBox(ref List<Student> list)
- {
- if (list.Count <= 1) { comboBox1.Enabled = false; textBox4.Enabled = false; }
- else
- {
- comboBox1.Enabled = true; textBox4.Enabled = true;
- }
- }
- public void Output(ref List<Student>list)
- {
- textBox1.Text = list[position].Name;
- textBox2.Text = list[position].Lastname;
- textBox3.Text = list[position].Faculty;
- }
- private void label1_Click(object sender, EventArgs e)
- {
- }
- public static List<Student> loadFile(List<Student> List, string filename)
- {
- FileStream f = new FileStream(filename, FileMode.Open);
- XmlSerializer bf = new XmlSerializer((typeof(List<Student>)));
- return (List<Student>)bf.Deserialize(f);
- f.Close();
- }
- public static void SaveFile(List<Student> List, string filename)
- {
- FileStream f = new FileStream(filename, FileMode.Create);
- XmlSerializer bf = new XmlSerializer((typeof(List<Student>)));
- bf.Serialize(f, List);
- f.Close();
- }
- private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
- Selection();
- }
- private void button2_Click(object sender, EventArgs e)
- {
- position++;
- while (flag && !CheckList() && position < t.Item2+1)
- {
- position++;
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- if (flag && CheckList() && position < t.Item2 + 1)
- {
- Output(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- if (!flag )
- {
- Output(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- }
- private void label5_Click(object sender, EventArgs e)
- {
- }
- private void textBox4_TextChanged(object sender, EventArgs e)
- {
- Selection();
- }
- private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- //Student s;
- Form2 form = new Form2();
- form.ShowDialog();
- if (form.DialogResult == DialogResult.OK)
- {
- if (!textBox1.Enabled || !textBox2.Enabled || !textBox3.Enabled)
- {
- textBox1.Enabled = true;
- textBox2.Enabled = true;
- textBox3.Enabled = true;
- }
- s = form.s;
- if (s.GetType().Name == "Master")
- {
- textBox5.Visible = true;
- label6.Visible = true;
- button3.Visible = false;
- }
- else
- {
- textBox5.Visible = false;
- label6.Visible = false;
- button3.Visible =true;
- }
- list.Add(s);
- position++;
- textBox1.Clear();
- textBox2.Clear();
- textBox3.Clear();
- textBox5.Clear();
- CheckButton(ref list);
- CheckMenu(ref list);
- CheckComboBox(ref list);
- }
- }
- private void загрузитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- position = 0;
- OpenFileDialog openFileDialog1 = new OpenFileDialog();
- // button1.Enabled = false;
- if (openFileDialog1.ShowDialog() == DialogResult.OK)
- {
- filename = openFileDialog1.FileName;
- list = loadFile(list, filename);
- Output(ref list);
- IsMaster(ref list);
- }
- if (!textBox1.Enabled || !textBox2.Enabled || !textBox3.Enabled)
- {
- textBox1.Enabled = true;
- textBox2.Enabled = true;
- textBox3.Enabled = true;
- }
- //if (list.Count() > 1) button2.Enabled = true;
- CheckButton(ref list);
- CheckMenu(ref list);
- CheckComboBox(ref list);
- }
- private void сохранитьКакToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFileDialog saveFileDialog1 = new SaveFileDialog();
- if (saveFileDialog1.ShowDialog() == DialogResult.OK)
- {
- filename = saveFileDialog1.FileName;
- SaveFile(list, filename);
- }
- }
- private void изменитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (list[position] is Master)
- {
- var item = list[position] as Master;
- item.Name = textBox1.Text;
- item.Lastname = textBox2.Text;
- item.Faculty = textBox3.Text;
- item.DiplomaNumber = textBox5.Text;
- list[position] = item;
- }
- else
- {
- s = list[position];
- s.Name = textBox1.Text;
- s.Lastname = textBox2.Text;
- s.Faculty = textBox3.Text;
- list[position] = s;
- }
- Output(ref list);
- IsMaster(ref list);
- }
- private void button1_Click(object sender, EventArgs e)
- {
- position--;
- while (flag && !CheckList() && position >= t.Item1)
- {
- position--;
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- if (flag && CheckList() && position >= t.Item1)
- {
- Output(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- if (!flag)
- {
- Output(ref list);
- CheckButton(ref list);
- CheckMenu(ref list);
- IsMaster(ref list);
- }
- }
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
- }
- private void удалитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- if (flag && position < t.Item2 && position > t.Item1)
- {
- int it1 = t.Item1;
- int it2 = t.Item2--;
- t = (it1, it2);
- }
- else if (flag && position < t.Item2 && position <= t.Item1)
- {
- int it1 = t.Item1++;
- int it2 = t.Item2--;
- t = (it1, it2);
- }
- list.RemoveAt(position);
- CheckButton(ref list);
- CheckMenu(ref list);
- CheckComboBox(ref list);
- if (list.Count == 0)
- {
- textBox1.Clear();
- textBox2.Clear();
- textBox3.Clear();
- textBox5.Clear();
- textBox1.Enabled = false;
- textBox2.Enabled = false;
- textBox3.Enabled = false;
- textBox5.Enabled = false;
- button3.Visible = false;
- return;
- }
- else if (position == list.Count())
- position--;
- Output(ref list);
- }
- private void сохранитьToolStripMenuItem_Click(object sender, EventArgs e)
- {
- SaveFile(list, filename);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void button3_Click(object sender, EventArgs e)
- {
- s = new Master(textBox1.Text, textBox2.Text, textBox3.Text, "");
- list[position] = s;
- IsMaster(ref list);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement