Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- namespace binaryfiles
- {
- public class Book
- {
- string author;
- public string Author { get { return author; } }
- string name;
- public string Name { get { return name; } }
- int code;
- public int Code { get { return code; } }
- public Book(string aa, string nn, int cc)
- {
- author = aa;
- name = nn;
- code = cc;
- }
- }
- public class Listing
- {
- string FILE_NAME = "book.data";
- public List<Book> li;
- public Listing()
- {
- li = new List<Book>();
- if (File.Exists(FILE_NAME))
- {
- using (FileStream fs = new FileStream(FILE_NAME, FileMode.Open))
- {
- BinaryReader br = new BinaryReader(fs);
- int k = br.ReadInt32();//количество карточек в файле
- string aa;
- string nn;
- int cc;
- for (int i = 0; i < k; i++)
- {
- aa = br.ReadString();
- nn = br.ReadString();
- cc = br.ReadInt32();
- li.Add(new Book(aa, nn, cc));
- }
- }
- }
- }
- public void Save()
- {
- using (FileStream fs = new FileStream(FILE_NAME, FileMode.Create))
- {
- BinaryWriter bw = new BinaryWriter(fs);
- int k = li.Count;
- bw.Write((int)k);
- for (int i = 0; i < k; i++)
- {
- bw.Write((string)li[i].Author);
- bw.Write((string)li[i].Name);
- bw.Write((int)li[i].Code);
- }
- bw.Close();
- fs.Close();
- }
- }
- public void Search()
- {
- Form dialog = new Form();
- dialog.Size = new Size(300, 350);
- dialog.StartPosition = FormStartPosition.CenterParent;
- Label ll = new Label();
- ll.Location = new Point(10, 10);
- ll.Text = "Введите слово:";
- TextBox tt = new TextBox();
- tt.Location = new Point(10, ll.Top + ll.Height + 5);
- tt.Size = new Size(250, 20);
- Button bbb = new Button();
- bbb.Text = "OK";
- bbb.Location = new Point(120, 250);
- bbb.DialogResult = DialogResult.OK;
- dialog.Controls.Add(ll);
- dialog.Controls.Add(tt);
- dialog.Controls.Add(bbb);
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- string pat;
- pat = tt.Text;
- Console.WriteLine("{0,20} {1,20} {2,20}\n", "Автор:", "Заглавие:", "Шифр: ");
- foreach (Book p in li)
- {
- string s;
- s = p.Author + p.Name + p.Code.ToString();
- if (s.IndexOf(pat) >= 0)
- {
- //Console.WriteLine(pat);
- //Console.WriteLine(s);
- //Console.WriteLine(s.IndexOf(pat));
- Console.WriteLine("{0,20} {1,20} {2,20}\n", p.Author, p.Name, p.Code);
- }
- }
- Console.WriteLine("Итого {0} кн.\n", li.Count);
- }
- }
- public void Print()
- {
- Console.WriteLine("{0,20} {1,20} {2,20}\n", "Автор:", "Заглавие:", "Шифр: ");
- foreach (Book p in li)
- {
- string s;
- s = p.Author + p.Name + p.Code.ToString();
- Console.WriteLine("{0,20} {1,20} {2,20}\n", p.Author, p.Name, p.Code);
- }
- Console.WriteLine("Итого {0} кн.\n", li.Count);
- }
- public void AddBook(string aa, string nn, int cc)
- {
- li.Add(new Book(aa, nn, cc));
- }
- public void Clear()
- {
- li.Clear();
- }
- }
- public class Form1 : Form
- {
- Listing l = new Listing();
- Label la = new Label();
- TextBox tb = new TextBox();
- Label la1 = new Label();
- TextBox tb1 = new TextBox();
- Label la2 = new Label();
- TextBox tb2 = new TextBox();
- Button b0 = new Button();
- Button b1 = new Button();
- Button b2 = new Button();
- Button b3 = new Button();
- Button b4 = new Button();
- Button b5 = new Button();
- Button bb = new Button();
- Button bb1 = new Button();
- public int n = 0;
- public Form1()
- {
- la.Location = new Point(10, 10);
- la.Text = "Автор:";
- tb.Location = new Point(10, la.Top + la.Height + 2);
- tb.Size = new Size(200, 20);
- la1.Location = new Point(10, tb.Top + tb.Height + 10);
- la1.Text = "Заглавие:";
- tb1.Location = new Point(10, la1.Top + la1.Height + 2);
- tb1.Size = new Size(200, 20);
- la2.Location = new Point(10, tb1.Top + tb1.Height + 10);
- la2.Text = "Шифр:";
- tb2.Location = new Point(10, la2.Top + la2.Height + 2);
- tb2.Size = new Size(100, 20);
- b0.Location = new Point(10, 410);
- b0.Text = "Search";
- b0.Click += new EventHandler(b0_Click);
- b1.Location = new Point(b0.Left + b0.Width + 10, 410);
- b1.Text = "Add";
- b1.Click += new EventHandler(b1_Click);
- b2.Location = new Point(b1.Left + b1.Width + 10, 410);
- b2.Text = "Save";
- b2.Click += new EventHandler(b2_Click);
- b3.Location = new Point(b2.Left + b2.Width + 10, 410);
- b3.Text = "Browse";
- b3.Click += new EventHandler(b3_Click);
- b4.Location = new Point(b3.Left + b3.Width + 10, 410);
- b4.Text = "Print";
- b4.Click += new EventHandler(b4_Click);
- b5.Location = new Point(b4.Left + b4.Width + 10, 410);
- b5.Text = "Clear";
- b5.Click += new EventHandler(b5_Click);
- bb.Text = "<";
- bb.Location = new Point(b1.Left + b1.Width + 10, 370);
- bb.Click += new EventHandler(bb_Click);
- bb1.Text = ">";
- bb1.Location = new Point(b2.Left + b2.Width + 10, 370);
- bb1.Click += new EventHandler(bb1_Click);
- this.Size = new Size(550, 500);
- this.StartPosition = FormStartPosition.CenterScreen;
- this.Controls.Add(la);
- this.Controls.Add(tb);
- this.Controls.Add(la1);
- this.Controls.Add(tb1);
- this.Controls.Add(la2);
- this.Controls.Add(tb2);
- this.Controls.Add(b0);
- this.Controls.Add(b1);
- this.Controls.Add(b2);
- this.Controls.Add(b3);
- this.Controls.Add(b4);
- this.Controls.Add(b5);
- this.Controls.Add(bb);
- this.Controls.Add(bb1);
- la.Hide();
- tb.Hide();
- la1.Hide();
- tb1.Hide();
- la2.Hide();
- tb2.Hide();
- bb.Hide();
- bb1.Hide();
- }
- void b0_Click(object sender, EventArgs e)
- {
- bb.Hide();
- bb1.Hide();
- l.Search();
- }
- void b1_Click(object sender, EventArgs e)
- {
- bb.Hide();
- bb1.Hide();
- Form dialog = new Form();
- dialog.Size = new Size(300, 350);
- dialog.StartPosition = FormStartPosition.CenterParent;
- Label l1 = new Label();
- l1.Location = new Point(10, 10);
- l1.Text = "Автор:";
- TextBox t1 = new TextBox();
- t1.Location = new Point(10, l1.Top + l1.Height + 2);
- t1.Size = new Size(200, 20);
- Label l2 = new Label();
- l2.Location = new Point(10, t1.Top + t1.Height + 10);
- l2.Text = "Заглавие:";
- TextBox t2 = new TextBox();
- t2.Location = new Point(10, l2.Top + l2.Height + 2);
- t2.Size = new Size(200, 20);
- Label l3 = new Label();
- l3.Location = new Point(10, t2.Top + t2.Height + 10);
- l3.Text = "Шифр:";
- TextBox t3 = new TextBox();
- t3.Location = new Point(10, l3.Top + l3.Height + 2);
- t3.Size = new Size(100, 20);
- Button b = new Button();
- b.Text = "OK";
- b.Location = new Point(120, 250);
- b.DialogResult = DialogResult.OK;
- dialog.Controls.Add(l1);
- dialog.Controls.Add(t1);
- dialog.Controls.Add(l2);
- dialog.Controls.Add(t2);
- dialog.Controls.Add(l3);
- dialog.Controls.Add(t3);
- dialog.Controls.Add(b);
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- l.AddBook(t1.Text, t2.Text, int.Parse(t3.Text));
- }
- }
- void b2_Click(object sender, EventArgs e)
- {
- bb.Hide();
- bb1.Hide();
- l.Save();
- }
- void b3_Click(object sender, EventArgs e)
- {
- la.Show();
- tb.Show();
- la1.Show();
- tb1.Show();
- la2.Show();
- tb2.Show();
- bb.Show();
- bb1.Show();
- if (l.li.Count > 0)
- {
- tb.Text = l.li[n].Author;
- tb1.Text = l.li[n].Name;
- tb2.Text = (l.li[n].Code).ToString();
- }
- else
- {
- la.Hide();
- tb.Hide();
- la1.Hide();
- tb1.Hide();
- la2.Hide();
- tb2.Hide();
- bb.Hide();
- bb1.Hide();
- }
- }
- void b4_Click(object sender, EventArgs e)
- {
- bb.Hide();
- bb1.Hide();
- l.Print();
- }
- void b5_Click(object sender, EventArgs e)
- {
- l.Clear();
- la.Hide();
- tb.Hide();
- la1.Hide();
- tb1.Hide();
- la2.Hide();
- tb2.Hide();
- bb.Hide();
- bb1.Hide();
- }
- void bb1_Click(object sender, EventArgs e)//>
- {
- if (n < l.li.Count - 1)
- {
- n++;
- tb.Text = l.li[n].Author;
- tb1.Text = l.li[n].Name;
- tb2.Text = (l.li[n].Code).ToString();
- }
- }
- void bb_Click(object sender, EventArgs e)//<
- {
- if (n > 0)
- {
- n--;
- tb.Text = l.li[n].Author;
- tb1.Text = l.li[n].Name;
- tb2.Text = (l.li[n].Code).ToString();
- }
- }
- }
- public class Program
- {
- [STAThread]
- static void Main()
- {
- Form1 f = new Form1();
- Application.Run(f);
- //Listing l = new Listing();
- //l.Print();
- //l.Save();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement