FormAddTovar.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Data.SqlClient;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace СУБД_Швейная_фабрика
  14. {
  15. public partial class FormAddTovar : Form
  16. {
  17. public FormAddTovar()
  18. {
  19. InitializeComponent();
  20. }
  21. struct Cloth
  22. {
  23. public string Title, Color, Width, Length, Description;
  24. public Image Photo;
  25. }
  26. List<Cloth> lstCloth = new List<Cloth>();
  27. private void btnAddCloth_Click(object sender, EventArgs e)
  28. {
  29. try
  30. {
  31. if (tbxTitle.Text == "" || int.Parse(tbxLenght.Text) <= 0 || int.Parse(tbxWidth.Text) <= 0 || int.Parse(tbxColor.Text) < 0)
  32. {
  33. MessageBox.Show("Все поля должны быть заполнены(Можно не заполнять поле описания) и больше нуля, цвет должен быть числом", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  34. return;
  35. }
  36. }
  37. catch
  38. {
  39. MessageBox.Show("Все поля должны быть заполнены(Можно не заполнять поле описания) и больше нуля, цвет должен быть числом", "Внимание!", MessageBoxButtons.OK, MessageBoxIcon.Error);
  40. return;
  41. }
  42. Cloth cl = new Cloth();
  43. cl.Title = tbxTitle.Text;
  44. cl.Color = tbxColor.Text;
  45. cl.Width = tbxWidth.Text;
  46. cl.Length = tbxLenght.Text;
  47. cl.Photo = pbxPhotoCloth.Image;
  48. lstCloth.Add(cl);
  49. dgvCloth.Rows.Add(tbxTitle.Text, tbxColor.Text, tbxLenght.Text, tbxWidth.Text, tbxDescr.Text);
  50. }
  51. private void btnLoadPhoto_Click(object sender, EventArgs e)
  52. {
  53. try
  54. {
  55. if (ofdPhoto.ShowDialog() == DialogResult.OK)
  56. {
  57. pbxPhotoCloth.Image = Image.FromFile(ofdPhoto.FileName);
  58. }
  59. }
  60. catch
  61. {
  62. }
  63. }
  64. private void btnClearPhoto_Click(object sender, EventArgs e)
  65. {
  66. if (DialogResult.Yes == MessageBox.Show("Удалить фотографию?","Внимание!",MessageBoxButtons.YesNo))
  67. pbxPhotoCloth.Image = null;
  68. }
  69. private void btnOk_Click(object sender, EventArgs e)
  70. {
  71. SqlConnection con = new SqlConnection(FormAuthorization.StrCon);
  72. for (int i = 0;i < lstCloth.Count;i++)
  73. {
  74. con.Open();
  75. if (lstCloth[i].Photo != null)
  76. {
  77. SqlCommand cmd = new SqlCommand($@"insert into Cloth(Title,IdColor,Weight,Height,Note,Photo)
  78. values('{lstCloth[i].Title}',{lstCloth[i].Color},{lstCloth[i].Width},{lstCloth[i].Length},'{lstCloth[i].Description}',@photo)", con);
  79. MemoryStream ms = new MemoryStream();
  80. lstCloth[i].Photo.Save(ms, ImageFormat.Png);
  81. cmd.Parameters.AddWithValue("@photo", ms.ToArray());
  82. cmd.ExecuteNonQuery();
  83. }else
  84. {
  85. SqlCommand cmd = new SqlCommand($@"insert into Cloth(Title,IdColor,Weight,Height,Note)
  86. values('{lstCloth[i].Title}',{lstCloth[i].Color},{lstCloth[i].Width},{lstCloth[i].Length},{lstCloth[i].Description})", con);
  87. cmd.ExecuteNonQuery();
  88. }
  89. con.Close();
  90. }
  91. MessageBox.Show("Документ принят к учету.", "Внимание!",
  92. MessageBoxButtons.OK, MessageBoxIcon.Information);
  93. this.Close();
  94. }
  95. private void btnDelCloth_Click(object sender, EventArgs e)
  96. {
  97. int NumTkan = dgvCloth.CurrentRow.Index;
  98. if (NumTkan >= 0)
  99. {
  100. DialogResult res = MessageBox.Show("Удалить выделенную ткань?",
  101. "Внимание!", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  102. if (res == DialogResult.Yes)
  103. {
  104. lstCloth.RemoveAt(NumTkan);
  105. dgvCloth.Rows.RemoveAt(NumTkan);
  106. }
  107. }
  108. }
  109. }
  110. }