Advertisement
StevanovicMilan

Vežba 10, Zadatak 2

Dec 1st, 2017
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.26 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.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows.Forms;
  11.  
  12. namespace Vezba_10_2017
  13. {
  14.     public partial class Form1 : Form
  15.     {
  16.         private BindingList<Kontakt> kontakti;
  17.  
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.  
  22.             kontakti = new BindingList<Kontakt>();
  23.  
  24.             Kontakt k = new Kontakt();
  25.             k.Prezime = "Stevanovic";
  26.             k.Ime = "Milan";
  27.             k.Adresa = "Mistake Island, USA";
  28.             k.BrojTelefona = "021345678";
  29.             kontakti.Add(k);
  30.  
  31.             dataGridView1.DataSource = kontakti;
  32.         }
  33.  
  34.         private void toolStripButton1_Click(object sender, EventArgs e)
  35.         {
  36.             OpenFileDialog ofd = new OpenFileDialog();
  37.             ofd.Filter = "Vezba 10 datoteke (*.v10)|*.v10";
  38.             if(ofd.ShowDialog() == DialogResult.OK)
  39.             {
  40.                 kontakti.Clear();
  41.                 using (FileStream stream = new FileStream(ofd.FileName, FileMode.Open))
  42.                 {
  43.                     using (BinaryReader reader = new BinaryReader(stream))
  44.                     {
  45.                         while (reader.BaseStream.Position < reader.BaseStream.Length)
  46.                         {
  47.                             Kontakt c = new Kontakt();
  48.                             c.Prezime = reader.ReadString();
  49.                             c.Ime = reader.ReadString();
  50.                             c.Adresa = reader.ReadString();
  51.                             c.BrojTelefona = reader.ReadString();
  52.                             kontakti.Add(c);
  53.                         }
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.         private void toolStripButton2_Click(object sender, EventArgs e)
  60.         {
  61.             SaveFileDialog sfd = new SaveFileDialog();
  62.             sfd.Filter = "Vezba 10 datoteke (*.v10)|*.v10";
  63.             if(sfd.ShowDialog() == DialogResult.OK)
  64.             {
  65.                 using (FileStream stream = new FileStream(sfd.FileName, FileMode.Create))
  66.                 {
  67.                     using (BinaryWriter writer = new BinaryWriter(stream))
  68.                     {
  69.                         foreach(Kontakt c in kontakti)
  70.                         {
  71.                             writer.Write(c.Prezime);
  72.                             writer.Write(c.Ime);
  73.                             writer.Write(c.Adresa);
  74.                             writer.Write(c.BrojTelefona);
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.  
  81.         private void izlazIzProgramaToolStripMenuItem_Click(object sender, EventArgs e)
  82.         {
  83.             Close();
  84.         }
  85.  
  86.         private void dodajKontaktToolStripMenuItem_Click(object sender, EventArgs e)
  87.         {
  88.             Form2 addForm = new Form2();
  89.             if(addForm.ShowDialog() == DialogResult.OK)
  90.             {
  91.                 Kontakt k = addForm.Kontakt;
  92.                 kontakti.Add(k);
  93.             }
  94.         }
  95.  
  96.         private void izmeniKontaktToolStripMenuItem_Click(object sender, EventArgs e)
  97.         {
  98.             if (dataGridView1.CurrentRow.DataBoundItem == null) return;
  99.  
  100.             Kontakt k = (Kontakt)dataGridView1.CurrentRow.DataBoundItem;
  101.             Form2 addForm = new Form2();
  102.             addForm.Kontakt = k;
  103.             addForm.ShowDialog();
  104.             if(addForm.DialogResult == DialogResult.OK)
  105.             {
  106.                 k.Ime = addForm.Kontakt.Ime;
  107.                 k.Prezime = addForm.Kontakt.Prezime;
  108.                 k.Adresa = addForm.Kontakt.Adresa;
  109.                 k.BrojTelefona = addForm.Kontakt.BrojTelefona;
  110.  
  111.                 dataGridView1.Refresh();
  112.             }
  113.         }
  114.  
  115.         private void obrisiKontaktToolStripMenuItem_Click(object sender, EventArgs e)
  116.         {
  117.             foreach (DataGridViewRow item in this.dataGridView1.SelectedRows)
  118.             {
  119.                 dataGridView1.Rows.RemoveAt(item.Index);
  120.             }
  121.             dataGridView1.Refresh();
  122.         }
  123.  
  124.         private void obrisiSveKontaktToolStripMenuItem_Click(object sender, EventArgs e)
  125.         {
  126.             kontakti.Clear();
  127.             dataGridView1.Refresh();
  128.         }
  129.  
  130.         private void oAplikacijiToolStripMenuItem_Click(object sender, EventArgs e)
  131.         {
  132.             MessageBox.Show("Jednostavan telefonski imenik");
  133.         }
  134.     }
  135. }
  136.  
  137.  
  138.  
  139. using System;
  140. using System.Collections.Generic;
  141. using System.ComponentModel;
  142. using System.Data;
  143. using System.Drawing;
  144. using System.Linq;
  145. using System.Text;
  146. using System.Threading.Tasks;
  147. using System.Windows.Forms;
  148.  
  149. namespace Vezba_10_2017
  150. {
  151.     public partial class Form2 : Form
  152.     {
  153.         public Kontakt Kontakt { get; set; }
  154.         public Form2()
  155.         {
  156.             InitializeComponent();
  157.         }
  158.  
  159.         private void button1_Click(object sender, EventArgs e)
  160.         {
  161.             Kontakt = new Kontakt();
  162.             Kontakt.Ime = textBox1.Text;
  163.             Kontakt.Prezime = textBox2.Text;
  164.             Kontakt.Adresa = textBox3.Text;
  165.             Kontakt.BrojTelefona = textBox4.Text;
  166.  
  167.             DialogResult = DialogResult.OK;
  168.             Close();
  169.         }
  170.  
  171.         private void button2_Click(object sender, EventArgs e)
  172.         {
  173.             DialogResult = DialogResult.Cancel;
  174.             Close();
  175.         }
  176.  
  177.         private void Form2_Load(object sender, EventArgs e)
  178.         {
  179.             textBox1.Text = Kontakt.Ime;
  180.             textBox2.Text = Kontakt.Prezime;
  181.             textBox3.Text = Kontakt.Adresa;
  182.             textBox4.Text = Kontakt.BrojTelefona;
  183.         }
  184.     }
  185. }
  186.  
  187.  
  188.  
  189. using System;
  190. using System.Collections.Generic;
  191. using System.Linq;
  192. using System.Text;
  193. using System.Threading.Tasks;
  194.  
  195. namespace Vezba_10_2017
  196. {
  197.     public class Kontakt
  198.     {
  199.         public string Prezime { get; set; }
  200.         public string Ime { get; set; }
  201.         public string Adresa { get; set; }
  202.         public string BrojTelefona { get; set; }
  203.  
  204.         public Kontakt()
  205.         {
  206.             Prezime = string.Empty;
  207.             Ime = string.Empty;
  208.             Adresa = string.Empty;
  209.             BrojTelefona = string.Empty;
  210.         }
  211.     }
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement