Advertisement
Guest User

Untitled

a guest
Dec 9th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 37.16 KB | None | 0 0
  1. //КЛАСС ФОРМЫ
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.IO;
  11. using System.Windows.Forms;
  12. using System.Xml.Serialization;
  13. using System.Xml;
  14. using System.Data.OleDb;
  15. using System.IO.Compression;
  16. using Microsoft.VisualBasic;
  17. using Microsoft.Office.Interop.Excel;
  18. using iTextSharp.text;
  19. using System.Collections;
  20. using Word = Microsoft.Office.Interop.Word;
  21. using PdfSharp.Pdf;
  22. using PdfSharp.Drawing;
  23.  
  24.  
  25. namespace lab5_3sem
  26. {
  27. public partial class Form1 : Form
  28. {
  29. List<Student> students = new List<Student>();
  30. public Form1()
  31. {
  32. this.KeyPreview = true;
  33. thelist = new Students();
  34. Student Student1 = new Student { Name = "", Faculty = "", Course = "", Birthday = "" };
  35. InitializeComponent();
  36. }
  37. string[] FilePath;
  38. private void button1_Click(object sender, EventArgs e) //load
  39. {
  40. listBox1.Items.Clear();
  41. openFileDialog1.Multiselect = true;
  42. openFileDialog1.FileName = "";
  43. openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  44. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  45. {
  46. FilePath = openFileDialog1.FileNames;
  47. var some = from t in FilePath select Path.GetFileNameWithoutExtension(t).ToString();
  48. listBox1.Items.AddRange(some.ToArray());
  49. }
  50. }
  51. private void toolStripMenuItem2_Click(object sender, EventArgs e) //нечетные строки, 1a
  52. {
  53. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  54. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  55. saveFileDialog1.RestoreDirectory = true;
  56. if (saveFileDialog1.ShowDialog() == DialogResult.OK && listBox1.SelectedIndex >= 0)
  57. {
  58. string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  59. string fin = "";
  60. //var tt = (from string t in listBox1.Items select t).Aggregate((str1, str2) => (str1 + Environment.NewLine + str2));
  61. //var s = (from string t in page where () / 2 == 0 select t);
  62. for (int i = 0; i < page.Count(); i += 2)
  63. fin += page[i] + Environment.NewLine;
  64. File.WriteAllText(saveFileDialog1.FileName, fin);
  65. //File.WriteAllText(saveFileDialog1.FileName, s.ToString());
  66. }
  67. }
  68.  
  69. private void toolStripMenuItem3_Click(object sender, EventArgs e) //строки, в которых нет цифр, 1b
  70. {
  71. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  72. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  73. saveFileDialog1.RestoreDirectory = true;
  74. string fin = "";
  75. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  76. {
  77. bool no_num_in_lines = true;
  78. foreach (int index in listBox1.SelectedIndices)
  79. {
  80. string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
  81. for (int i = 0; i < page.Count(); i++)
  82. {
  83. foreach (char item in page[i])
  84. if (char.IsNumber(item))
  85. {
  86. no_num_in_lines = false;
  87. break;
  88. }
  89. if (no_num_in_lines)
  90. fin += page[i] + Environment.NewLine;
  91. no_num_in_lines = true;
  92. }
  93. }
  94. saveFileDialog1.ShowDialog();
  95. string filename = saveFileDialog1.FileName;
  96. File.WriteAllText(filename, fin);
  97. }
  98. }
  99. private void toolStripMenuItem4_Click(object sender, EventArgs e) //все строки кроме пустых, 1с
  100. {
  101. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  102. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  103. saveFileDialog1.RestoreDirectory = true;
  104. string fin = "";
  105. foreach (int index in listBox1.SelectedIndices)
  106. {
  107. string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
  108. for (int i = 0; i < page.Count(); i++)
  109. if (page[i].Length != 0)
  110. fin += page[i] + Environment.NewLine;
  111. }
  112. saveFileDialog1.ShowDialog();
  113. string filename = saveFileDialog1.FileName;
  114. File.WriteAllText(filename, fin);
  115. }
  116.  
  117. private void listBox1_SelectedIndexChanged(object sender, EventArgs e) //2b
  118. {
  119. if (listBox1.SelectedIndex >= 0)
  120. {
  121. string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  122. int fin = 0;
  123. for (int i = 0; i < page.Count(); i += 2)
  124. fin += page[i].Length;
  125. listBox2.Items.Clear();
  126. //string[] a = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  127. for (int i = 0; i < page.Count(); i++)
  128. listBox2.Items.Add(page[i]);
  129. label1.Text = string.Format("{0}\n{1}\n{2}", fin, listBox2.Items.Count, string.Format("{0}", listBox2.Items[listBox2.Items.Count - 1]).Length);
  130. }
  131. }
  132. private void строкиВОбратномПорядкеToolStripMenuItem_Click(object sender, EventArgs e)
  133. {
  134. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  135. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  136. saveFileDialog1.RestoreDirectory = true;
  137. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  138. {
  139. string fin = "";
  140. foreach (string item in listBox2.Items)
  141. fin = item + Environment.NewLine + fin;
  142. saveFileDialog1.ShowDialog();
  143. string filename = saveFileDialog1.FileName;
  144. File.WriteAllText(filename, fin);
  145. }
  146. }
  147.  
  148. private void сохранитьВыделенныеСтрокиToolStripMenuItem_Click(object sender, EventArgs e)
  149. {
  150. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  151. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  152. saveFileDialog1.RestoreDirectory = true;
  153. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  154. {
  155. string fin = "";
  156. foreach (string item in listBox2.SelectedItems)
  157. fin += item + Environment.NewLine;
  158. saveFileDialog1.ShowDialog();
  159. string filename = saveFileDialog1.FileName;
  160. File.WriteAllText(filename, fin);
  161. }
  162. }
  163. private void button4_Click(object sender, EventArgs e) //3c, сохранение файла
  164. {
  165. listBox2.Sorted = true;
  166. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  167. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  168. saveFileDialog1.RestoreDirectory = true;
  169. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  170. {
  171. string fin = "";
  172. foreach (string item in listBox2.Items)
  173. fin += item + Environment.NewLine;
  174. saveFileDialog1.ShowDialog();
  175. string filename = saveFileDialog1.FileName;
  176. File.WriteAllText(filename, fin);
  177. }
  178. }
  179. ////////////////////////////////////////// 3 num //////////////////////
  180.  
  181. private Students thelist; //см инициализацию в Form1
  182. bool IsXml;
  183. bool priority;
  184.  
  185. private void button3_Click(object sender, EventArgs e)//открытие txt/xml файла для 2 задания
  186. {
  187. listBox3.Items.Clear();
  188. thelist.Thelist.Clear();
  189. openFileDialog1.Multiselect = true;
  190. openFileDialog1.FileName = "";
  191. openFileDialog1.Filter = "txt files (*.txt)|*.txt|xml files (*xml)|*.xml";
  192. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  193. {
  194. string h = openFileDialog1.FileName;
  195. if (Path.GetExtension(openFileDialog1.FileName) == ".txt")
  196. {
  197. IsXml = false;
  198. string text = File.ReadAllText(openFileDialog1.FileName, Encoding.GetEncoding(1251));
  199. string[] lines = text.Split('\n');
  200. foreach (string line in lines)
  201. {
  202. string[] words = line.Split(' ');
  203. if (words.Length == 7)
  204. {
  205. string fin = words[0] + ' ' + words[1] + ' ' + words[2];
  206. listBox3.Items.Add(fin);
  207. thelist.Add(fin, words[3], words[4], words[5], words[6]);
  208. }
  209. }
  210. }
  211. else
  212. {
  213. IsXml = true;
  214. using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate))
  215. {
  216. XmlSerializer formatter;
  217. formatter = new XmlSerializer(typeof(Students));
  218. thelist = (Students)formatter.Deserialize(fs);
  219. //var grouped = thelist.Thelist.GroupBy(worker => worker.Name).Select(group => new Student { Name = group.Key });
  220. //listBox3.Items.AddRange(grouped.ToArray());
  221. var som = from t in thelist.Thelist select t.Name.ToString();
  222. listBox3.Items.AddRange(som.ToArray<string>());
  223. }
  224. }
  225. }
  226. }
  227.  
  228. private void listBox3_SelectedIndexChanged(object sender, EventArgs e) //вывод инфы из листбокса3
  229. {
  230. if (listBox3.SelectedIndex != -1)
  231. {
  232. priority = true;
  233. textBox1.Text = thelist.Thelist[listBox3.SelectedIndex].Name;
  234. textBox2.Text = thelist.Thelist[listBox3.SelectedIndex].Faculty;
  235. textBox3.Text = thelist.Thelist[listBox3.SelectedIndex].Course;
  236. textBox4.Text = thelist.Thelist[listBox3.SelectedIndex].Birthday;
  237. textBox5.Text = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
  238. priority = false;
  239. }
  240. }
  241.  
  242. private void SaveFile(bool IsXml, string Path) //сохранение файла после изменения
  243. {
  244. File.Delete(openFileDialog1.FileName);
  245. if (!IsXml)
  246. {
  247. foreach (Student stud in thelist.Thelist)
  248. File.AppendAllText(openFileDialog1.FileName, stud.Name + " " + stud.Faculty + " " + stud.Course + " " + stud.Birthday + " " + stud.Birthplace + Environment.NewLine, Encoding.GetEncoding(1251));
  249. }
  250. else
  251. {
  252. using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate))
  253. {
  254. XmlSerializer formatter;
  255. formatter = new XmlSerializer(typeof(Students));
  256. formatter.Serialize(fs, thelist);
  257. }
  258. }
  259. }
  260. private void textBox1_TextChanged(object sender, EventArgs e) // редактирование файла
  261. {
  262.  
  263. if (listBox3.SelectedIndex != -1 && priority == false)
  264. {
  265. string h = openFileDialog1.FileName;
  266. h = h.Substring(h.LastIndexOf('.') + 1); // Получаем расширение файла
  267. if (textBox1.Text != "") thelist.Thelist[listBox3.SelectedIndex].Name = textBox1.Text;
  268. if (textBox2.Text != "") thelist.Thelist[listBox3.SelectedIndex].Faculty = textBox2.Text;
  269. if (textBox3.Text != "") thelist.Thelist[listBox3.SelectedIndex].Course = textBox3.Text;
  270. if (textBox4.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthday = textBox4.Text;
  271. if (textBox5.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthplace = textBox5.Text;
  272. SaveFile(IsXml, openFileDialog1.FileName);
  273. }
  274. }
  275. private void deleting(object sender, EventArgs e)
  276. {
  277. thelist.Thelist.RemoveAt(listBox3.SelectedIndex);
  278. listBox3.Items.RemoveAt(listBox3.SelectedIndex);
  279. }
  280. private void Create(object sender, EventArgs e)
  281. {
  282. listBox3.Items.Add(textBox6.Text);
  283. thelist.Thelist.Add(new Student(textBox6.Text, "факультет", "курс", "дата рождения", "место рождения"));
  284. }
  285.  
  286. private void SaveAsDoc(object sender, EventArgs e)
  287. {
  288. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  289. saveFileDialog1.OverwritePrompt = true;
  290. saveFileDialog1.DefaultExt = ".doc";
  291. saveFileDialog1.Filter = "Word 2007 Documents (*.doc)|*.doc";
  292. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  293. File.WriteAllText(saveFileDialog1.FileName, thelist.Thelist[listBox3.SelectedIndex].Name+' ' + thelist.Thelist[listBox3.SelectedIndex].Faculty + ' ' + thelist.Thelist[listBox3.SelectedIndex].Course + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthday + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthplace);
  294. //File.WriteAllText(saveFileDialog1.FileName, thelist.ToString());
  295. }
  296.  
  297. private void SaveAsPdf(object sender, EventArgs e)
  298. {
  299. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  300. saveFileDialog1.OverwritePrompt = true;
  301. saveFileDialog1.DefaultExt = ".pdf";
  302. saveFileDialog1.Filter = "PDF Files(*.pdf)| *.pdf";
  303. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  304. {
  305. XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
  306. PdfDocument pdf = new PdfDocument();
  307. PdfPage pdfPage = pdf.AddPage();
  308. XGraphics graph = XGraphics.FromPdfPage(pdfPage);
  309. XFont xFont = new XFont("Times New Roman", 9.25, XFontStyle.Bold, options);
  310.  
  311. graph.DrawString(thelist.Thelist[listBox3.SelectedIndex].ToString(), xFont, XBrushes.Black, new XRect(12, 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
  312. //for (int i = 0; i < thelist.Thelist.Count; i++)
  313. // graph.DrawString(thelist.Thelist[i].ToString(), xFont, XBrushes.Black, new XRect(12, i * 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
  314. pdf.Save(saveFileDialog1.FileName);
  315. pdf.Close();
  316. }
  317. }
  318.  
  319. private void SaveAsXls(object sender, EventArgs e)
  320. {
  321. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  322. saveFileDialog1.OverwritePrompt = true;
  323. saveFileDialog1.DefaultExt = ".xls";
  324. saveFileDialog1.Filter = "Excel Files(*.xlsx)|*.xls";
  325. Microsoft.Office.Interop.Excel.Application exelic = new Microsoft.Office.Interop.Excel.Application();
  326. exelic.Workbooks.Add();
  327. Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)exelic.ActiveSheet;
  328. worksheet.Cells[1, "A"] = "ФИО";
  329. worksheet.Cells[1, "B"] = "Курс";
  330. worksheet.Cells[1, "C"] = "Факультет";
  331. worksheet.Cells[1, "D"] = "Дата рождения";
  332. worksheet.Cells[1, "E"] = "Место рождения";
  333. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  334. {
  335. //int n = 0;
  336. worksheet.Cells[2, "A"] = thelist.Thelist[listBox3.SelectedIndex].Name;
  337. worksheet.Cells[2, "B"] = thelist.Thelist[listBox3.SelectedIndex].Faculty;
  338. worksheet.Cells[2, "C"] = thelist.Thelist[listBox3.SelectedIndex].Course;
  339. worksheet.Cells[2, "D"] = thelist.Thelist[listBox3.SelectedIndex].Birthday;
  340. worksheet.Cells[2, "E"] = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
  341. //Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[n, "A"]];
  342. Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[1, "A"]];
  343. wow.ColumnWidth = 30;
  344. //wow= worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[n, "E"]];
  345. wow = worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[1, "E"]];
  346. wow.ColumnWidth = 20;
  347. worksheet.SaveAs(saveFileDialog1.FileName);
  348. exelic.Quit();
  349. }
  350. }
  351. }
  352. }
  353.  
  354.  
  355. //КЛАСС Students
  356. using System;
  357. using System.Collections.Generic;
  358. using System.ComponentModel;
  359. using System.Data;
  360. using System.Drawing;
  361. using System.Linq;
  362. using System.Text;
  363. using System.Threading.Tasks;
  364. using System.IO;
  365. using System.Windows.Forms;
  366. using System.Xml.Serialization;
  367. using System.Xml;
  368. using System.Data.OleDb;
  369. using System.IO.Compression;
  370. using Microsoft.VisualBasic;
  371. using Microsoft.Office.Interop.Excel;
  372. using iTextSharp.text;
  373. using System.Collections;
  374. using Word = Microsoft.Office.Interop.Word;
  375. using PdfSharp.Pdf;
  376. using PdfSharp.Drawing;
  377.  
  378.  
  379. namespace lab5_3sem
  380. {
  381. public partial class Form1 : Form
  382. {
  383. List<Student> students = new List<Student>();
  384. public Form1()
  385. {
  386. this.KeyPreview = true;
  387. thelist = new Students();
  388. Student Student1 = new Student { Name = "", Faculty = "", Course = "", Birthday = "" };
  389. InitializeComponent();
  390. }
  391. string[] FilePath;
  392. private void button1_Click(object sender, EventArgs e) //load
  393. {
  394. listBox1.Items.Clear();
  395. openFileDialog1.Multiselect = true;
  396. openFileDialog1.FileName = "";
  397. openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  398. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  399. {
  400. FilePath = openFileDialog1.FileNames;
  401. var some = from t in FilePath select Path.GetFileNameWithoutExtension(t).ToString();
  402. listBox1.Items.AddRange(some.ToArray());
  403. }
  404. }
  405. private void toolStripMenuItem2_Click(object sender, EventArgs e) //нечетные строки, 1a
  406. {
  407. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  408. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  409. saveFileDialog1.RestoreDirectory = true;
  410. if (saveFileDialog1.ShowDialog() == DialogResult.OK && listBox1.SelectedIndex >= 0)
  411. {
  412. string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  413. string fin = "";
  414. //var tt = (from string t in listBox1.Items select t).Aggregate((str1, str2) => (str1 + Environment.NewLine + str2));
  415. //var s = (from string t in page where () / 2 == 0 select t);
  416. for (int i = 0; i < page.Count(); i += 2)
  417. fin += page[i] + Environment.NewLine;
  418. File.WriteAllText(saveFileDialog1.FileName, fin);
  419. //File.WriteAllText(saveFileDialog1.FileName, s.ToString());
  420. }
  421. }
  422.  
  423. private void toolStripMenuItem3_Click(object sender, EventArgs e) //строки, в которых нет цифр, 1b
  424. {
  425. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  426. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  427. saveFileDialog1.RestoreDirectory = true;
  428. string fin = "";
  429. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  430. {
  431. bool no_num_in_lines = true;
  432. foreach (int index in listBox1.SelectedIndices)
  433. {
  434. string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
  435. for (int i = 0; i < page.Count(); i++)
  436. {
  437. foreach (char item in page[i])
  438. if (char.IsNumber(item))
  439. {
  440. no_num_in_lines = false;
  441. break;
  442. }
  443. if (no_num_in_lines)
  444. fin += page[i] + Environment.NewLine;
  445. no_num_in_lines = true;
  446. }
  447. }
  448. saveFileDialog1.ShowDialog();
  449. string filename = saveFileDialog1.FileName;
  450. File.WriteAllText(filename, fin);
  451. }
  452. }
  453. private void toolStripMenuItem4_Click(object sender, EventArgs e) //все строки кроме пустых, 1с
  454. {
  455. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  456. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  457. saveFileDialog1.RestoreDirectory = true;
  458. string fin = "";
  459. foreach (int index in listBox1.SelectedIndices)
  460. {
  461. string[] page = File.ReadAllLines(FilePath[index], Encoding.GetEncoding(1251));
  462. for (int i = 0; i < page.Count(); i++)
  463. if (page[i].Length != 0)
  464. fin += page[i] + Environment.NewLine;
  465. }
  466. saveFileDialog1.ShowDialog();
  467. string filename = saveFileDialog1.FileName;
  468. File.WriteAllText(filename, fin);
  469. }
  470.  
  471. private void listBox1_SelectedIndexChanged(object sender, EventArgs e) //2b
  472. {
  473. if (listBox1.SelectedIndex >= 0)
  474. {
  475. string[] page = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  476. int fin = 0;
  477. for (int i = 0; i < page.Count(); i += 2)
  478. fin += page[i].Length;
  479. listBox2.Items.Clear();
  480. //string[] a = File.ReadAllLines(FilePath[listBox1.SelectedIndex], Encoding.GetEncoding(1251));
  481. for (int i = 0; i < page.Count(); i++)
  482. listBox2.Items.Add(page[i]);
  483. label1.Text = string.Format("{0}\n{1}\n{2}", fin, listBox2.Items.Count, string.Format("{0}", listBox2.Items[listBox2.Items.Count - 1]).Length);
  484. }
  485. }
  486. private void строкиВОбратномПорядкеToolStripMenuItem_Click(object sender, EventArgs e)
  487. {
  488. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  489. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  490. saveFileDialog1.RestoreDirectory = true;
  491. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  492. {
  493. string fin = "";
  494. foreach (string item in listBox2.Items)
  495. fin = item + Environment.NewLine + fin;
  496. saveFileDialog1.ShowDialog();
  497. string filename = saveFileDialog1.FileName;
  498. File.WriteAllText(filename, fin);
  499. }
  500. }
  501.  
  502. private void сохранитьВыделенныеСтрокиToolStripMenuItem_Click(object sender, EventArgs e)
  503. {
  504. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  505. saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
  506. saveFileDialog1.RestoreDirectory = true;
  507. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  508. {
  509. string fin = "";
  510. foreach (string item in listBox2.SelectedItems)
  511. fin += item + Environment.NewLine;
  512. saveFileDialog1.ShowDialog();
  513. string filename = saveFileDialog1.FileName;
  514. File.WriteAllText(filename, fin);
  515. }
  516. }
  517. private void button4_Click(object sender, EventArgs e) //3c, сохранение файла
  518. {
  519. listBox2.Sorted = true;
  520. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  521. saveFileDialog1.Filter = "txt files (*.txt)|*.txt";
  522. saveFileDialog1.RestoreDirectory = true;
  523. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  524. {
  525. string fin = "";
  526. foreach (string item in listBox2.Items)
  527. fin += item + Environment.NewLine;
  528. saveFileDialog1.ShowDialog();
  529. string filename = saveFileDialog1.FileName;
  530. File.WriteAllText(filename, fin);
  531. }
  532. }
  533. ////////////////////////////////////////// 3 num //////////////////////
  534.  
  535. private Students thelist; //см инициализацию в Form1
  536. bool IsXml;
  537. bool priority;
  538.  
  539. private void button3_Click(object sender, EventArgs e)//открытие txt/xml файла для 2 задания
  540. {
  541. listBox3.Items.Clear();
  542. thelist.Thelist.Clear();
  543. openFileDialog1.Multiselect = true;
  544. openFileDialog1.FileName = "";
  545. openFileDialog1.Filter = "txt files (*.txt)|*.txt|xml files (*xml)|*.xml";
  546. if (openFileDialog1.ShowDialog() == DialogResult.OK)
  547. {
  548. string h = openFileDialog1.FileName;
  549. if (Path.GetExtension(openFileDialog1.FileName) == ".txt")
  550. {
  551. IsXml = false;
  552. string text = File.ReadAllText(openFileDialog1.FileName, Encoding.GetEncoding(1251));
  553. string[] lines = text.Split('\n');
  554. foreach (string line in lines)
  555. {
  556. string[] words = line.Split(' ');
  557. if (words.Length == 7)
  558. {
  559. string fin = words[0] + ' ' + words[1] + ' ' + words[2];
  560. listBox3.Items.Add(fin);
  561. thelist.Add(fin, words[3], words[4], words[5], words[6]);
  562. }
  563. }
  564. }
  565. else
  566. {
  567. IsXml = true;
  568. using (FileStream fs = new FileStream(openFileDialog1.FileName, FileMode.OpenOrCreate))
  569. {
  570. XmlSerializer formatter;
  571. formatter = new XmlSerializer(typeof(Students));
  572. thelist = (Students)formatter.Deserialize(fs);
  573. //var grouped = thelist.Thelist.GroupBy(worker => worker.Name).Select(group => new Student { Name = group.Key });
  574. //listBox3.Items.AddRange(grouped.ToArray());
  575. var som = from t in thelist.Thelist select t.Name.ToString();
  576. listBox3.Items.AddRange(som.ToArray<string>());
  577. }
  578. }
  579. }
  580. }
  581.  
  582. private void listBox3_SelectedIndexChanged(object sender, EventArgs e) //вывод инфы из листбокса3
  583. {
  584. if (listBox3.SelectedIndex != -1)
  585. {
  586. priority = true;
  587. textBox1.Text = thelist.Thelist[listBox3.SelectedIndex].Name;
  588. textBox2.Text = thelist.Thelist[listBox3.SelectedIndex].Faculty;
  589. textBox3.Text = thelist.Thelist[listBox3.SelectedIndex].Course;
  590. textBox4.Text = thelist.Thelist[listBox3.SelectedIndex].Birthday;
  591. textBox5.Text = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
  592. priority = false;
  593. }
  594. }
  595.  
  596. private void SaveFile(bool IsXml, string Path) //сохранение файла после изменения
  597. {
  598. File.Delete(openFileDialog1.FileName);
  599. if (!IsXml)
  600. {
  601. foreach (Student stud in thelist.Thelist)
  602. File.AppendAllText(openFileDialog1.FileName, stud.Name + " " + stud.Faculty + " " + stud.Course + " " + stud.Birthday + " " + stud.Birthplace + Environment.NewLine, Encoding.GetEncoding(1251));
  603. }
  604. else
  605. {
  606. using (FileStream fs = new FileStream(Path, FileMode.OpenOrCreate))
  607. {
  608. XmlSerializer formatter;
  609. formatter = new XmlSerializer(typeof(Students));
  610. formatter.Serialize(fs, thelist);
  611. }
  612. }
  613. }
  614. private void textBox1_TextChanged(object sender, EventArgs e) // редактирование файла
  615. {
  616.  
  617. if (listBox3.SelectedIndex != -1 && priority == false)
  618. {
  619. string h = openFileDialog1.FileName;
  620. h = h.Substring(h.LastIndexOf('.') + 1); // Получаем расширение файла
  621. if (textBox1.Text != "") thelist.Thelist[listBox3.SelectedIndex].Name = textBox1.Text;
  622. if (textBox2.Text != "") thelist.Thelist[listBox3.SelectedIndex].Faculty = textBox2.Text;
  623. if (textBox3.Text != "") thelist.Thelist[listBox3.SelectedIndex].Course = textBox3.Text;
  624. if (textBox4.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthday = textBox4.Text;
  625. if (textBox5.Text != "") thelist.Thelist[listBox3.SelectedIndex].Birthplace = textBox5.Text;
  626. SaveFile(IsXml, openFileDialog1.FileName);
  627. }
  628. }
  629. private void deleting(object sender, EventArgs e)
  630. {
  631. thelist.Thelist.RemoveAt(listBox3.SelectedIndex);
  632. listBox3.Items.RemoveAt(listBox3.SelectedIndex);
  633. }
  634. private void Create(object sender, EventArgs e)
  635. {
  636. listBox3.Items.Add(textBox6.Text);
  637. thelist.Thelist.Add(new Student(textBox6.Text, "факультет", "курс", "дата рождения", "место рождения"));
  638. }
  639.  
  640. private void SaveAsDoc(object sender, EventArgs e)
  641. {
  642. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  643. saveFileDialog1.OverwritePrompt = true;
  644. saveFileDialog1.DefaultExt = ".doc";
  645. saveFileDialog1.Filter = "Word 2007 Documents (*.doc)|*.doc";
  646. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  647. File.WriteAllText(saveFileDialog1.FileName, thelist.Thelist[listBox3.SelectedIndex].Name+' ' + thelist.Thelist[listBox3.SelectedIndex].Faculty + ' ' + thelist.Thelist[listBox3.SelectedIndex].Course + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthday + ' ' + thelist.Thelist[listBox3.SelectedIndex].Birthplace);
  648. //File.WriteAllText(saveFileDialog1.FileName, thelist.ToString());
  649. }
  650.  
  651. private void SaveAsPdf(object sender, EventArgs e)
  652. {
  653. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  654. saveFileDialog1.OverwritePrompt = true;
  655. saveFileDialog1.DefaultExt = ".pdf";
  656. saveFileDialog1.Filter = "PDF Files(*.pdf)| *.pdf";
  657. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  658. {
  659. XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode, PdfFontEmbedding.Always);
  660. PdfDocument pdf = new PdfDocument();
  661. PdfPage pdfPage = pdf.AddPage();
  662. XGraphics graph = XGraphics.FromPdfPage(pdfPage);
  663. XFont xFont = new XFont("Times New Roman", 9.25, XFontStyle.Bold, options);
  664.  
  665. graph.DrawString(thelist.Thelist[listBox3.SelectedIndex].ToString(), xFont, XBrushes.Black, new XRect(12, 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
  666. //for (int i = 0; i < thelist.Thelist.Count; i++)
  667. // graph.DrawString(thelist.Thelist[i].ToString(), xFont, XBrushes.Black, new XRect(12, i * 10, pdfPage.Width.Point, pdfPage.Height.Point), XStringFormats.TopLeft);
  668. pdf.Save(saveFileDialog1.FileName);
  669. pdf.Close();
  670. }
  671. }
  672.  
  673. private void SaveAsXls(object sender, EventArgs e)
  674. {
  675. SaveFileDialog saveFileDialog1 = new SaveFileDialog();
  676. saveFileDialog1.OverwritePrompt = true;
  677. saveFileDialog1.DefaultExt = ".xls";
  678. saveFileDialog1.Filter = "Excel Files(*.xlsx)|*.xls";
  679. Microsoft.Office.Interop.Excel.Application exelic = new Microsoft.Office.Interop.Excel.Application();
  680. exelic.Workbooks.Add();
  681. Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)exelic.ActiveSheet;
  682. worksheet.Cells[1, "A"] = "ФИО";
  683. worksheet.Cells[1, "B"] = "Курс";
  684. worksheet.Cells[1, "C"] = "Факультет";
  685. worksheet.Cells[1, "D"] = "Дата рождения";
  686. worksheet.Cells[1, "E"] = "Место рождения";
  687. if (saveFileDialog1.ShowDialog() == DialogResult.OK)
  688. {
  689. //int n = 0;
  690. worksheet.Cells[2, "A"] = thelist.Thelist[listBox3.SelectedIndex].Name;
  691. worksheet.Cells[2, "B"] = thelist.Thelist[listBox3.SelectedIndex].Faculty;
  692. worksheet.Cells[2, "C"] = thelist.Thelist[listBox3.SelectedIndex].Course;
  693. worksheet.Cells[2, "D"] = thelist.Thelist[listBox3.SelectedIndex].Birthday;
  694. worksheet.Cells[2, "E"] = thelist.Thelist[listBox3.SelectedIndex].Birthplace;
  695. //Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[n, "A"]];
  696. Microsoft.Office.Interop.Excel.Range wow = worksheet.Range[worksheet.Cells[1, "A"], worksheet.Cells[1, "A"]];
  697. wow.ColumnWidth = 30;
  698. //wow= worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[n, "E"]];
  699. wow = worksheet.Range[worksheet.Cells[1, "B"], worksheet.Cells[1, "E"]];
  700. wow.ColumnWidth = 20;
  701. worksheet.SaveAs(saveFileDialog1.FileName);
  702. exelic.Quit();
  703. }
  704. }
  705. }
  706. }
  707.  
  708. //////////////////////////////////////
  709. //КЛАСС Students (контейнер для студентов)
  710. /////////////////////////////////////
  711. using System;
  712. using System.Collections.Generic;
  713. using System.Text;
  714. using System.Windows.Forms;
  715. using System.IO;
  716. using System.Xml;
  717. using System.Xml.Serialization;
  718. using System.IO.Compression;
  719.  
  720. using Microsoft.VisualBasic;
  721.  
  722. namespace lab5_3sem
  723. {
  724. public class Students
  725. {
  726. public List<Student> Thelist { get; set; }
  727. public Students()
  728. {
  729. Thelist = new List<Student>();
  730. }
  731.  
  732. public System.Windows.Forms.ListBox.ObjectCollection oh(object sender)
  733. {
  734. System.Windows.Forms.ListBox.ObjectCollection tt = new System.Windows.Forms.ListBox.ObjectCollection((System.Windows.Forms.ListBox)sender);
  735. for (int i = 0; i < Thelist.Count; i++)
  736. {
  737. tt.Add(Thelist[i].Name);
  738. }
  739. return tt;
  740. }
  741. public void Addxml(List<Student> o)
  742. {
  743. Thelist = o;
  744. }
  745. public void Add(string n, string f, string c, string bday, string bplace)
  746. {
  747. Thelist.Add(new Student (n, f, c, bday, bplace));
  748. }
  749.  
  750.  
  751. }
  752. }
  753.  
  754. ///////////////////////////
  755. КЛАСС Student
  756. //////////////////////////
  757. using System;
  758. using System.Collections.Generic;
  759. using System.ComponentModel;
  760. using System.Data;
  761. using System.Drawing;
  762. using System.Linq;
  763. using System.Text;
  764. using System.Threading.Tasks;
  765. using System.Windows.Forms;
  766. using System.IO;
  767. using System.Xml;
  768. using System.Xml.Serialization;
  769.  
  770. namespace lab5_3sem
  771. {
  772. [Serializable]
  773. public class Student
  774. {
  775. public string Name { get; set; }
  776. public string Faculty { get; set; }
  777. public string Course { get; set; }
  778. public string Birthday { get; set; }
  779. public string Birthplace { get; set; }
  780.  
  781. public Student()
  782. {
  783. }
  784. public Student(string n, string f, string c, string bday, string bplace)
  785. {
  786. Name = n;
  787. Faculty = f;
  788. Course = c;
  789. Birthday = bday;
  790. Birthplace = bplace;
  791. }
  792.  
  793. public override string ToString()
  794. {
  795. string fin = "";
  796. fin += Name + ' ' + Faculty + ' ' + Course + ' ' + Birthday + ' ' + Birthplace + '\n';
  797. return fin;
  798. }
  799. }
  800. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement